10.21动手动脑

1.

 

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

}

  构造一个对象,先调用其构造方法来初始化其成员函数和成员变量。子类拥有父的成员变量和成员方法,必须通过调用从父类继承而来的成员变量和成员方法来正确的初始化。而父类不拥有子类成员,故子父类调用不可反。

2.

 

public class ExplorationJDKSource {

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(new A());
    }

}

class A{}

  

在编译源代码时,当遇到没有父类的类时,编译器会将其指定一个默认的父类(一般为Object),因此,main方法实际上内部调用了String类的valueOf方法。 valueOf方法内部又调用Object.toString方法: 

public String toString() { return getClass().getName() +"@" + Integer.toHexString(hashCode()); }
原文地址:https://www.cnblogs.com/mjhjl/p/14144463.html