Вопрос Java. Как использовать переменную с модификатором protected?

Регистрация
24 Мар 2013
Сообщения
97
Репутация
0
Спасибо
0
Монет
0
/* Провести три боя попарно между котами
Создать три кота используя класс Cat.
Провести три боя попарно между котами.
Класс Cat создавать не надо. Для боя использовать метод boolean fight(Cat anotherCat).
Результат каждого боя вывести на экран.
*/

public class Solution {
public static void main(String[] args) {

Cat cat=new Cat(name="Murzik",age=2,);// Как здесь использовать переменные класса Cat???
Cat cat1=new Cat();
Cat cat2=new Cat();
}

public static class Cat {

public static int count = 0;
public static int fightCount = 0;

protected String name;
protected int age;
protected int weight;
protected int strength;

public Cat(String name, int age, int weight, int strength) {
count++;

this.name = name;
this.age = age;
this.weight = weight;
this.strength = strength;
}

public boolean fight(Cat anotherCat) {
fightCount++;

int agePlus = this.age > anotherCat.age? 1 : 0;
int weightPlus = this.weight > anotherCat.weight? 1 : 0;
int strengthPlus = this.strength > anotherCat.strength? 1 : 0;

int score = agePlus + weightPlus + strengthPlus;
return score > 2; // return score > 2? true : false;
}
}
}
 
public static void main(String[] args) { Cat barsik = new Cat("Барсик", 7, 12, 119); // Создание объекта класса Cat на основе конструктора barsik.strength = 159; // Обращение к переменной strength объекта barsik } На Javarush учитесь? ))
 
Назад
Сверху