Answer the question
In order to leave comments, you need to log in
How to make chains in inherited java classes?
Good afternoon.
Initial data: there is a desire to use Method chainig, because it is often convenient.
Let's take "animals" as an example.
Let's have a parent class for all animals that supports chains:
class Animal {
public Animal setWeight(int newWeight) {
return this;
}
public Animal setName(String newName){
return this;
}
}
// Как работает?
Animal animal = new Animal().setName("Murzik").setWeight(20);
// Удобно!
class Cat extends Animal {
public Cat setMeowVolume(int volume) {
return this;
}
}
setWeight
and setName
return Animal
and the following constructions will not work:Cat cat1 = new Cat().setName("Murzik").setWeight(20).setMeowVolume(100); // Ошибка: setMeowVolume - не найден
Cat cat2 = new Cat().setMeowVolume(100).setName("Murzik").setWeight(20); // Ошибка: setWeight не возвращает Cat
Cat cat1 = ((Cat)new Cat().setName("Murzik").setWeight(20)).setMeowVolume(100);
Cat cat2 = (Cat)new Cat().setMeowVolume(100).setName("Murzik").setWeight(20);
class Animal {
public Animal setWeight(int newWeight) {
return this;
}
public Animal setName(String newName){
return this;
}
}
class Cat extends Animal {
@Override
public Cat setWeight (int newWeight) {
super.setWeight(newWeight);
return this;
}
@Override
public Cat setName (String newName) {
super.setName(newName);
return this;
}
public Cat setMeowVolume(int volume) {
return this;
}
}
// Работает замечательно
Cat cat1 = new Cat().setName("Murzik").setWeight(20).setMeowVolume(100);
Cat cat2 = new Cat().setMeowVolume(100).setName("Murzik").setWeight(20);
// Проблемы:
// 1. Увеличение кода
// 2. Наследование методов там, где не оно не требуется.
class Animal <Return extends Animal<Return>> {
public Return setWeight(int newWeight) {
return (Return)this; // Unchecked cast warning
}
public Return setName(String newName){
return (Return)this; // Unchecked cast warning
}
}
class Cat<Return extends Cat<Return>> extends Animal<Return> {
public Return setMeowVolume(int volume) {
return (Return)this; // Unchecked cast warning
}
}
// Работает:
Cat cat1 = new Cat<>().setName("Murzik").setWeight(20).setMeowVolume(100);
Cat cat2 = new Cat<>().setMeowVolume(100).setName("Murzik").setWeight(20);
// Проблемы:
// 1. Куча предупреждений Unchecked cast.
// 2. Класс Cat становится generic-ом со всеми вытекающими последствиями и предупреждениями. (Например, конструктор необходимо указывать со скобками: new Cat<>()).
// 3. Грамозкое и малопонятное объявление классов.
class Animal {
public this setWeight(int newWeight) {
}
public this setName(String newName){
}
}
class Cat extends Animal {
this setMeowVolume(int volume) {
}
}
Answer the question
In order to leave comments, you need to log in
It is written to you: the magic method __toString () should not throw an exception, and at you - throws out. You are probably accessing something somewhere in this method that you haven't checked for existence.
it's hard to understand without the entity code, judging by the message that toasting cannot throw an exception - this is php
7
public function __toString()
{
return $this->name ? $this->name : 'New';
}
To avoid unchecked warning, you can also do this:
class Animal <Return extends Animal<Return>> {
public Return setWeight(int newWeight) {
return self();
}
public Return setName(String newName){
return self();
}
@SuppressWarnings("unchecked")
protected Return self() {
return (Return) this;
}
}
class Cat<Return extends Cat<Return>> extends Animal<Return> {
public Return setMeowVolume(int volume) {
return self();
}
}
I did not encounter similar tasks, and I never used chains. Of course, I'm far from such subtleties and problems as in your case, but maybe it's worth trying to write a silver bullet through annotation \ reflections?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question