java继承与接口课后作业

一.运行 TestInherits.java 示例,观察输出。

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.结果

3.

 

 
 

 

 

 

结论

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

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

·不能反过来。因为子类是通过父类继承过来的,所以子类拥有父类的属性和方法。如果不调用父类的构造方法,就不能初始化父类中定义的属性,也就不能给父类的属性分配内存空间 ,如果父类的属性没有分配内存空间,那么当子类访问父类的属性时,系统必然会报错。

二.探索技术的奥秘

1.代码

public class ExplorationJDKSource {

 

/**

 * @param args

 */

public static void main(String[] args) {

System.out.println(new A());

}

}

class A{}

2.结果

3.

 

 
 

 

 

 

真相:

·前面示例中,main方法实际上调用的是:

public void println(Object x),这一方法内部调用了String类的valueOf方法。

valueOf方法内部又调用Object.toString方法:

public String toString() {

return getClass().getName() +"@" +

Integer.toHexString(hashCode());

}

hashCode方法是本地方法,由JVM设计者实现:

public  native int hashCode();

三.动手动脑

·在子类中,若要调用父类中被覆盖的方法,可以使用super关键字。

public class Super

{

public static void main(String args[])

{

Child c=new Child();

c.show();

}

 

}

class Parent

{

public void show()

{

System.out.println("parent");

}

}

class Child extends Parent

{

public void show()

{

System.out.println("child");

super.show();

}

}

四.方法覆盖(override

public class Fruit

{

 

public String toString()

{

return "Fruit toString.";

}

 

public static void main(String args[])

{

Fruit f=new Fruit();

System.out.println("f="+f);

// System.out.println("f="+f.toString());

}

}

·结果

 
 

 

 

 

 

·结论:在“+”运算中,当任何一个对象与一个String对象,连接时,会隐式地调用其toString()方法,默认情况下,此方法返回“类名 @ + hashCode”。为了返回有意义的信息,子类可以重写toString()方法。

·要点:方法覆盖要求子类与父类的方法一模一样,否则就是方法重载(overload)!

 

posted @ 2016-11-11 11:27  发酸的丶米饭  阅读(202)  评论(0编辑  收藏  举报