저는 Java를 통해 OOP를 배우는 1 학년 학생입니다.
제네릭 형식을 이해하려고하는데 다음 예제에서는 disguisedAs()가 개체 인스턴스에 대한 포인터를 반환한다는 인상하에 있습니다. 그렇지 않다.
왜 내 코드가 작동하지 않으며 어떻게 컴파일하고 실행할 수 있습니까?
미리 감사드립니다.Java의 제네릭 유형에서 객체 변수에 액세스
public class GenericTest {
public static void main(String[] args) {
Animal tux = new Penguin();
DisguisedPerson<Animal> dave = new DisguisedPerson<Animal>(tux, "Dave");
dave.disguisedAs().call();
dave.reveal();
}
}
interface Disguised <T> {
T disguisedAs();
}
class Person {
String name;
Person(String name) {
this.name = name;
}
}
class DisguisedPerson<U> extends Person implements Disguised<U> {
U resembles;
DisguisedPerson(U costume, String name) {
super(name);
resembles = costume;
}
public U disguisedAs() {
return resembles;
}
void reveal() {
System.out.println(name + " was dressed up as a " + disguisedAs().species); // returns error: cannot find symbol!
}
}
abstract class Animal {
String species;
String call;
Animal(String c) {
species = this.getClass().getName();
this.call = c;
}
void call() {
System.out.println(this.call + "! ImA " + this.species);
}
}
class Penguin extends Animal {
Penguin() {
super("Pip");
}
}
이미 뭔가를 시도한 후에 실제 코드가있는 실제 질문에 +1. 최근에 "코드 제공"질문을하는 사용자가 너무 많습니다. \ – amit