java005向上转型 向下转型

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Birdmotianlun/article/details/50202515

函数的转型
对象向上转型–子类的对象赋值给父类
student s=new student();
person p=s;//向上转型
———-111111111111————————

 class person{
  String name;
  int age;
  void shuchu(){
   System.out.pringln(name+age);
  }
 }

———22222222————-

   class student extends person{
   String address;
   //复写父类方法
   void shuchu(){
   super.shuchu(); //System.out.pringln(name+age);//
   System.out.pringln(address);
   }

   }

——3333333333333333333—————

   class test{
   public static void main(String args[]){
   student s=new student();//25 25可写person p=new student();
   person p=s;//向上转型
   p.name="zhangsan";
   p.shuchu();//调用的是student的shuchu()

   }
  }

对象向下转型 –将父类对象赋值给子类

student s=new student();//向下转型首先进行向上转型
person p=s;

student s2=(student)p;//向下转型

——3333333333333333333—————

   class test{
   public static void main(String args[]){
   person p=new student();
   person p=s;//向上转型
   student s2=(student)p;//向下转型


   }
  }

猜你喜欢

转载自blog.csdn.net/Birdmotianlun/article/details/50202515