有关泛型……
本人做题的时候有个关于泛型的问题弄不清楚,望各位高手能予以解答,在此先谢谢各位了~
Place code into the class so that it compiles and generates the output answer=42.
Note:Code options may be used more than once.
public class _____{
private _____ object;
public _____ (_____ object){
this.o=object;
}
public _____ getobject(){
return object;
}
public static void main(String[] args) {
gen <String> str=new gen <String> ( "answer ");
gen <Integer> intg=new gen <Integer> (42);
System.out.println(str.getobject()+ "= "+intg.getobject());
}
}
Code Option:Gen <T> ,Gen <?> ,Gen,?,T
------解决方案--------------------public class Gen <T> {
private T object;
public Gen (T object){
this.object=object; //这里应该是this.object
}
public T getobject(){
return object;
}
public static void main(String[] args) {
Gen <String> str=new Gen <String> ( "answer ");
Gen <Integer> intg=new Gen <Integer> (42);
System.out.println(str.getobject()+ "= "+intg.getobject());
}
}