疑问就是代码里注释的那一行,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());
    }
}

解决方案 »

  1.   

    ob是int类型,而不是Integer类型
      

  2.   

       public Object getOb()
        {
            return ob;
        }返回的是Object类型的必须强制转型
      

  3.   


    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());
    }
    }
      

  4.   

    lz再理解理解多态的含义吧
    http://blog.csdn.net/renyuanchunby/article/details/6967860
      

  5.   

    为什么子类能赋值给父类
    Object ob = new Integer(6);
      

  6.   

    你的返回值的Obj类型的 肯定需要强制转换了啊。、
    尽管你进去的时候是Integer但是出来的时候变成了Obj了啊