如何正确使用 Generic?
How I can make proper use of Generic?
public class GenericClass {
public static void main(String... args) {
Hat<Integer> marks = new Hat<Integer>();
marks.tell(4);
Hat<String> apple = new Hat<String>();
apple.tell("apple");
}
}
class Hat<T> {
void tell(T an) {
System.out.println(an + " is good");
}
void tell(String fu) {
System.out.println(fu + " is healthy");
}
}
apple.tell("apple")
怎么会模棱两可呢?我正在尝试检查我可以使用 Generic 的多少种不同方式。
apple.tell("apple")
is an String and
void tell(T an) {
System.out.println(an + " is good");
}
T Become String for "apple" so compiler encounter Two String & Overloading is also there so it is ambiguous
public class GenericClass {
public static void main(String... args) {
Hat<Integer> marks = new Hat<Integer>();
marks.tell(4);
Hat<String> apple = new Hat<String>();
apple.tell("apple");
}
}
class Hat<T> {
void tell(T an) {
System.out.println(an + " is good");
}
void tell(String fu) {
System.out.println(fu + " is healthy");
}
}
apple.tell("apple")
怎么会模棱两可呢?我正在尝试检查我可以使用 Generic 的多少种不同方式。
apple.tell("apple")
is an String and
void tell(T an) {
System.out.println(an + " is good");
}
T Become String for "apple" so compiler encounter Two String & Overloading is also there so it is ambiguous