动手动脑11.1

 

运行 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();

    }

}

 

结论:当super("Hello.Grandparent.");语句是第一句的时候可以正常运行,当super("Hello.Grandparent.");不是第一句的时候会报错,所以通过 super 调用的基类构造方法必须是子类构造方法中的第一个语句。

 

 

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

构造函数(constructor)是一种特殊的方法 。主要用来在创建对象时初始化对象, 即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中 。
特别的一个类可以有多个构造函数 ,可根据其参数个数的不同或参数类型的不同来区分它们 即构造函数的重载。构造函数的功能主要用于在类的对象创建时定义初始化的状态。
构造一个对象,先调用其构造方法,来初始化其成员函数和成员变量。
子类拥有父的成员变量和成员方法,如果不调用,则从父类继承而来的成员变量和成员方法得不到正确的初始化。
不能反过来调用也是这个原因,因为父类根本不知道子类有什么变量而且这样一来子类也得不到初始化的父类变量,导致程序运行出错!







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);
}
}

 

运行结果:

Parent.printValue(),myValue=100
Child.printValue(),myValue=200
Child.printValue(),myValue=200
Child.printValue(),myValue=200
Child.printValue(),myValue=201

 

 

  1. 程序原因分析:当子类与父类拥有一样的方法,并且让一个父类变量引用一个子类对象时,到底调用哪个方法,由对象自己的“真实”类型所决定,这就是说:对象是子类型的,它就调用子类型的方法,是父类型的,它就调用父类型的方法。
  2. 对于多态,可以总结它为:

      一、使用父类类型的引用指向子类的对象;

      二、该引用只能调用父类中定义的方法和变量;

      三、如果子类中重写了父类中的一个方法,那么在调用这个方法的时候,将会调用子类中的这个方法;(动态连接、动态调用)

      四、变量不能被重写(覆盖),”重写“的概念只针对方法,如果在子类中”重写“了父类中的变量,那么在编译时会报错。




posted on 2018-11-01 17:15  哈萨K  阅读(100)  评论(0编辑  收藏  举报