public class Solution {
public static void main(String[] args) {
Cat petya = new Cat("petya",33,21,43);
Cat vaska = new Cat("vaska",343, 23,32);
Cat nosok = new Cat("nosok", 454,454,3);
}
public static class Cat {
protected String name;
protected int age;
protected int weight;
protected int strength;
public Cat(String name, int age, int weight, int strength) {
this.name = name;
this.age = age;
this.weight = weight;
this.strength = strength;
}
public boolean fight(Cat anotherCat) {
int ageAdvantage = this.age > anotherCat.age ? 1 : 0;
int weightAdvantage = this.weight > anotherCat.weight ? 1 : 0;
int strengthAdvantage = this.strength > anotherCat.strength ? 1 : 0;
int score = ageAdvantage + weightAdvantage + strengthAdvantage;
return score > 2;
}
}
}
Не понимаю, у нас есть класс Cat в классе Solution, чтобы создать объект этого класса используется такой код :
Cat nameOfCat = new Cat();
Что такое? Это -
public Cat(String name, int age, int weight, int strength) {
this.name = name;
this.age = age;
this.weight = weight;
this.strength = strength;
}
Это ведь метод этого класса, насколько я понимаю это setter. Тогда почему при объявлении нового экземпляра класса мы используем метод этого класса? Не могу понять.