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("调用父类的方法\t"+myValue);
    }
}
class Child extends Parent{
    public String myValue="这是子类的属性";
    public void printValue() {
        System.out.println("调用子类的方法\t"+myValue);
    }
}
验证

运行截图:

猜你喜欢

转载自www.cnblogs.com/gothic-death/p/9890321.html