package com.shine; public class Entry { public static void main(String[] args) { // Parent mParent = new Parent(); // mParent.thought(); System.out.println("*****"); Parent nParent = new Parent("Dada"); nParent.thought(); System.out.println("*****"); Child mChild = new Child(); mChild.thought(); System.out.println("*****"); Child nChild = new Child("Son"); nChild.thought(); System.out.println("*****"); } } class Parent { public String name; // public Parent() { // System.out.println("Parent constuctor..."); // } // public Parent(String s) { name = s; } public void thought() { System.out.println(name + "thought..."); } } //super的第一种用法:在构造器中使用super();在构造器中使用super()经常用于调用父类构造器 //当父类不存在无参构造器时,子类构造器必须明确显示super调用父类构造器,其参数必须与父类构造器参数一致 //如果子类构造器没有明确显示super调用父类构造器,系统默认使用super()调用父类的无参构造器 class Child extends Parent { public Child() { super (""); System.out.println("Child constuctor..."); } public Child(String s) { super(""); name = s; } //super的第二种用法:在子类方法覆盖中,可以使用super引用父类原有的方法 //此用法可以扩展父类原有的方法 public void thought() { super.thought(); System.out.println(name + "thinking..."); } }