zoukankan      html  css  js  c++  java
  • 父类引用指向子类的对象;

    public class Tuzi {         //父类

        public String name;  

      public void show(){

          System.out.println(name+"是个笨蛋");

           }

         public Tuzi(){     }  public Tuzi(String name){   this.name=name;   }  

        public void setname(String name){   this.name=name;  }

         public String getname(){   return name;  }

     }

    1. 通过代码看内存分配
    2. 从对象的角度看问题
      1. 那什么是动态链接呢
    3. 小结
    public class Tuzi0 extends Tuzi{        //子类
     public String weight;
     public Tuzi0(){
      super();
     }
     public Tuzi0(String name,String weight){
      super(name);
      this.weight=weight;
     }
     public void main(){
      System.out.println(name+"说,他承认自己是个笨蛋,并向你发出一拨666");
     }
     }
    public class Rabbit {
     public void showinfo(Tuzi q){
      q.show();
     }
     public void maininfo(Tuzi0 a){
      a.main();
     }
    }
     
     public class Test2 {
      public static void main(String[] args) {
       Tuzi q=new Tuzi("xx");
    // 父类的引用指向子类的对象
       Tuzi p=new Tuzi0();
       Tuzi0 a=new Tuzi0("xx","");
       Rabbit b=new Rabbit();
       b.showinfo(q);
       b.maininfo(a);
      } 

     

     
  • 相关阅读:
    练习12
    练习11
    练习10(图片题)
    练习9(第九章习题)
    练习8(图片题)
    练习5
    练习4
    对象的赋值与比较
    静态方法
    静态变量
  • 原文地址:https://www.cnblogs.com/hljj/p/6880655.html
Copyright © 2011-2022 走看看