转型

大纲:

大纲讲解:转型原因就是父类有的子类都有,子类有的父类不一定有.

//转型
    //向上转型==>子转父
    Father f1=new Son();
    System.out.println("名字是:"+s.getName());
    f1.work();//显示子类的work,如果被重写,调用子类方法,
    //f1.sing  转型后会损失子类特有的方法
    
    
    
    //向下转型==>父转子
    System.out.println("这是向下转型");
    //Son s1=(Son) new Father("");
    Son s1=(Son)f1;//向下转型前,必须要有一个向上转型作为前提条件
    s1.work();
    
    System.out.println();
    Father f2=(Father)Son.getData(1);
    f2.work();
    
转型上课讲义

 

posted on 2016-03-08 21:38  一全  阅读(158)  评论(0)    收藏  举报

导航