zoukankan      html  css  js  c++  java
  • 03继承与多态 动手动脑

    动手实验:继承条件下的构造方法调用

    运行 TestInherits.java 示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改Parent构造方法的代码,显式调用GrandParent的另一个构造函数,注意这句调用代码是否是第一句,影响重大!

    class Grandparent 
    {
    
    
        public Grandparent()
         {
    
                System.out.println("GrandParent Created.");
        
    }
    
    
        public Grandparent(String string) 
        {
    
                System.out.println("GrandParent Created.String:" + string);
        
     }
    
    }
    
    
    
    class Parent extends Grandparent
    {
    
    
        public Parent()
         {
    
                //super("Hello.Grandparent.");
    
                System.out.println("Parent Created");
        
           // super("Hello.Grandparent.");
    
          }
    
    }
    
    
    
    class Child extends Parent 
    {
    
    
        public Child()
         {
        
            System.out.println("Child Created");
    
          }
    
    }
    
    
    
    public class TestInherits 
    {
    
    
        public static void main(String args[])
         {
    
                Child c = new Child();
        
      }
    
    }
    Testlnherits.java

    通过super调用基类构造方法,必须是子类构造方法中的第一个语句

    思索:

    为什么子类的构造方法在运行之前,必须调用父类的构造方法?能不能反过来?为什么不能反过来?

    构造函数主要是用来在创建对象时初始化对象,即为对象成员变量赋初值。由于子类是继承父类的,所以创建子类对象时,必须先调用父类的构造方法,这样子类才能有父类的属性和方法。不能反过来是因为父类不知道子类有什么变量,而子类也得不到父类的初始化变量。

    请自行编写发代码测试一下特性:
    在子类中若要调用父类中被覆盖的方法,可以使用super关键字

    class Parent{
        public void method(){
            System.out.println("这是父类");
        }
    }
    
    class Child extends Parent{
        public void method(){
            System.out.println("这是子类");
            super.method();
        }
    }
    
    public class Override {
        public static void main(String [] args){
            Child c = new Child();
            c.method();
        }
    }
    Override.java

    运行截图:

    程序运行结果是什么? 你如何解释会得到这样的输出? 计算机是不会出错的,之所以得 到这样的运行结果也是有原因的, 那么从这些运行结果中,你能总结出Java的哪些语法特性?

    请务必动脑总结,然后修改或编写一些代码进行测试,验证自己的想法,最后再看 后面的PPT给出的结论。

    public class ParentChildTest {
        public static void main(String[] args) {
            Parent parent=new Parent();
            parent.printValue();
            Child child=new Child();
            child.printValue();
            
            parent=child;
            parent.printValue();
            
            parent.myValue++;
            parent.printValue();
            
            ((Child)parent).myValue++;
            parent.printValue();
            
        }
    }
    
    class Parent{
        public int myValue=100;
        public void printValue() {
            System.out.println("Parent.printValue(),myValue="+myValue);
        }
    }
    class Child extends Parent{
        public int myValue=200;
        public void printValue() {
            System.out.println("Child.printValue(),myValue="+myValue);
        }
    }
    ParentChildTest.java

    运行截图:

    如果子类与父类有相同的字段,则子类中的字段会代替或隐藏父类的字段,子类方法中访问的是子类中的字段(而不是父类中的字段)。如果子类方法确实想访问父类中被隐藏的同名字段,可以用super关键字来访问它。如果子类被当作父类使用,则通过子类访问的字段是父类的。

    当子类与父类拥有一样的方法,并且让一个父类变量引用一个子类对象时,对象是子类型的,它就调用子类型的方法,是父类型的,它就调用父类型的方法。

    public class ParentChildTest {
        public static void main(String[] args) {
            Parent parent=new Parent();
            parent.printValue();
            Child child=new Child();
            child.printValue();
            
            parent=child;
            parent.printValue();
            
            parent.myValue+="a";
            parent.printValue();
            System.out.println(parent.myValue);
            
            ((Child)parent).myValue+="b";
            parent.printValue();
            System.out.println(((Child)parent).myValue);
            
        }
    }
    
    class Parent{
        public String myValue="这是父类的属性";
        public void printValue() {
            System.out.println("调用父类的方法	"+myValue);
        }
    }
    class Child extends Parent{
        public String myValue="这是子类的属性";
        public void printValue() {
            System.out.println("调用子类的方法	"+myValue);
        }
    }
    验证

    运行截图:

  • 相关阅读:
    【bzoj4591】[Shoi2015]超能粒子炮·改 Lucas定理
    【bzoj1604】[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 旋转坐标系+并查集+Treap/STL-set
    十分钟看懂图像语义分割技术
    命令行执行python模块时提示ImportError: No module named xxx
    python json与字典对象互相转换
    C#中json字符串的序列化和反序列化
    Python当前线程休眠1秒钟
    python之bytes和string
    Win32 基本文件读写操作
    C# 字符串与字节数组相互转换
  • 原文地址:https://www.cnblogs.com/gothic-death/p/9890321.html
Copyright © 2011-2022 走看看