不好意思再請問,為什麼下面高亮的 (String) 一定要寫?
public class Counter<T> {
private T mEmber;
public Counter<T> setMember(T member) {
mEmber = member;
System.out.println("setMember(): " + mEmber.getClass().getName());
return this;
}
@SuppressWarnings("unchecked")
public T getMember() {
System.out.println("getMember(): " + mEmber.getClass().getName());
System.out.println("getMember(): "
+ ((Class<T>) mEmber.getClass()).getName());
return (T) mEmber;
}
public static void eatString(String s) {
System.out.println("eatString(): " + s);
}
public static void main(String[] args) {
new Counter<String>().setMember("Java");
eatString((String) new Counter<String>().setMember("Java")
.getMember());
}
}