public boolean fight(Cat anotherCat) { // что значит надпись в скобках?
int agePlus = this.age > anotherCat.age ? 1 : 0; // это значит тоже что if(this.age > anotherCat.age) 1 else 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;
}
Ивaн
7 уровень
Вопрос по методу
Решен
Комментарии (3)
- популярные
- новые
- старые
Для того, чтобы оставить комментарий Вы должны авторизоваться
George Skrydlov
9 мая 2018, 08:56решение
1) в метод fight передается переменная anotherCat типа Cat - то есть объект (экземпляр) класса Cat. У этого объекта доступны такие же поля, как и у любого другого объекта класса Cat. Подобный подход позволяет сравнивать значения полей текущего объекта и переданного в метод fight - как это делать уже вам написал Dzhin.
2) Вы писали: "int agePlus = this.age > anotherCat.age ? 1 : 0; // это значит тоже что if(this.age > anotherCat.age) 1 else 0"
Не совсем корректно. Вот так будет аналогичная запись:
if(this.age > anotherCat.age) agePlus = 1 else agePlus = 0;
в первой строчке используется тернарный оператор.
+6
Ивaн
9 мая 2018, 09:39
Спасибо, очень доходчиво!
0
Dzhin
8 мая 2018, 15:41решение
1) это значит что в параметр метода передаешь ссылку на кота с которым ты будешь сравнивать текущего кота. Текущий кот - this . Кот с которым сравниваешь - anotherCat.
2) да это идентичные записи
+2