방법 체인 + 상속은 함께 잘 재생되지 않습니까?
-
21-08-2019 - |
문제
이 질문은 C ++ 맥락에서 묻습니다. 그러나 나는 Java에 대해 궁금합니다. 가상 방법에 대한 우려는 적용되지 않지만이 상황이있는 경우 :
abstract class Pet
{
private String name;
public Pet setName(String name) { this.name = name; return this; }
}
class Cat extends Pet
{
public Cat catchMice() {
System.out.println("I caught a mouse!");
return this;
}
}
class Dog extends Pet
{
public Dog catchFrisbee() {
System.out.println("I caught a frisbee!");
return this;
}
}
class Bird extends Pet
{
public Bird layEgg() {
...
return this;
}
}
{
Cat c = new Cat();
c.setName("Morris").catchMice(); // error! setName returns Pet, not Cat
Dog d = new Dog();
d.setName("Snoopy").catchFrisbee(); // error! setName returns Pet, not Dog
Bird b = new Bird();
b.setName("Tweety").layEgg(); // error! setName returns Pet, not Bird
}
이런 종류의 클래스 계층 구조에는 반환 방법이 있습니까? this
객체 유형을 (효과적으로) 향상시키지 않는 방식으로?
해결책
컴파일러에서 확인되지 않은 캐스트 경고를 피하려면 (@suppresswarnings ( "Checked")를 원하지 않으려면 다음을 조금 더해야합니다.
우선, 애완 동물에 대한 정의는 애완 동물이 항상 일반적인 유형이기 때문에 자기 참조이어야합니다.
abstract class Pet <T extends Pet<T>>
둘째, (T) this
SetName의 캐스트도 확인되지 않았습니다. 이를 피하려면 우수한 "TheThis"기술을 사용하십시오. Angelika Langer의 제네릭 FAQ:
"getThis"트릭은이 참조의 정확한 유형을 복구하는 방법을 제공합니다.
이로 인해 아래 코드가 발생하여 경고없이 컴파일하고 실행됩니다. 서브 클래스를 확장하려면 기술이 여전히 유지됩니다 (중간 클래스를 제네릭해야 할 것입니다).
결과 코드는 다음과 같습니다.
public class TestClass {
static abstract class Pet <T extends Pet<T>> {
private String name;
protected abstract T getThis();
public T setName(String name) {
this.name = name;
return getThis(); }
}
static class Cat extends Pet<Cat> {
@Override protected Cat getThis() { return this; }
public Cat catchMice() {
System.out.println("I caught a mouse!");
return getThis();
}
}
static class Dog extends Pet<Dog> {
@Override protected Dog getThis() { return this; }
public Dog catchFrisbee() {
System.out.println("I caught a frisbee!");
return getThis();
}
}
public static void main(String[] args) {
Cat c = new Cat();
c.setName("Morris").catchMice();
Dog d = new Dog();
d.setName("Snoopy").catchFrisbee();
}
}
다른 팁
이 오래된 트릭은 어떻습니까 :
abstract class Pet<T extends Pet>
{
private String name;
public T setName(String name) { this.name = name; return (T) this; }
}
class Cat extends Pet<Cat>
{
/* ... */
}
class Dog extends Pet<Dog>
{
/* ... */
}
아니 정말. 공분산 반환 유형을 사용하여 작업을 수행 할 수 있습니다 (올바른 이름에 대한 McDowell에게 감사) :
@Override
public Cat setName(String name) {
super.setName(name);
return this;
}
(공분산 반환 유형은 Java 5 이상으로 만 있습니다.
약간 복잡하지만 제네릭 으로이 작업을 수행 할 수 있습니다.
abstract class Pet< T extends Pet > {
private String name;
public T setName( String name ) {
this.name = name;
return (T)this;
}
public static class Cat extends Pet< Cat > {
public Cat catchMice() {
System.out.println( "I caught a mouse!" );
return this;
}
}
public static class Dog extends Pet< Dog > {
public Dog catchFrisbee() {
System.out.println( "I caught a frisbee!" );
return this;
}
}
public static void main (String[] args){
Cat c = new Cat();
c.setName( "Morris" ).catchMice(); // error! setName returns Pet, not Cat
Dog d = new Dog();
d.setName( "Snoopy" ).catchFrisbee(); // error! setName returns Pet, not Dog
}
}
public class Pet<AnimalType extends Pet> {
private String name;
public AnimalType setName(String name) {
this.name = name; return (AnimalType)this;
}
}
그리고
public class Cat extends Pet<Cat> {
public Cat catchMice() {return this;}
public static void main(String[] args) {
Cat c = new Cat().setName("bob").catchMice();
}
}
제휴하지 않습니다 StackOverflow