疑问就是代码里注释的那一行,ob已经是Integer类型了,为什么还要转换一下public class GenDemo2
{
public static void main(String[] args)
{
Gen2 intOb = new Gen2(new Integer(88));
intOb.showType();
//Integer i = intOb.getOb(); 如果不做强制类型转化就会报错,可是多态不就能解释的通可以不用强制类型转换。
Integer i = (Integer)intOb.getOb();
}
}class Gen2
{
private Object ob;
public Gen2(Object ob)
{
this.ob = ob;
}
public Object getOb()
{
return ob;
}
public void setOb(Object ob)
{
this.ob = ob;
}
public void showType()
{
System.out.println("T的实际类型是:"+ ob.getClass().getName());
}
}
{
public static void main(String[] args)
{
Gen2 intOb = new Gen2(new Integer(88));
intOb.showType();
//Integer i = intOb.getOb(); 如果不做强制类型转化就会报错,可是多态不就能解释的通可以不用强制类型转换。
Integer i = (Integer)intOb.getOb();
}
}class Gen2
{
private Object ob;
public Gen2(Object ob)
{
this.ob = ob;
}
public Object getOb()
{
return ob;
}
public void setOb(Object ob)
{
this.ob = ob;
}
public void showType()
{
System.out.println("T的实际类型是:"+ ob.getClass().getName());
}
}
{
return ob;
}返回的是Object类型的必须强制转型
class GenDemo2 {
public static void main(String[] args) {
Gen2<Integer> intOb = new Gen2<Integer>(new Integer(88));
intOb.showType();
Integer i = intOb.getOb();
}
}class Gen2<T> {
private T ob; public Gen2(T ob) {
this.ob = ob;
} public T getOb() {
return ob;
} public void setOb(T ob) {
this.ob = ob;
} public void showType() {
System.out.println("T的实际类型是:" + ob.getClass().getName());
}
}
http://blog.csdn.net/renyuanchunby/article/details/6967860
Object ob = new Integer(6);
尽管你进去的时候是Integer但是出来的时候变成了Obj了啊