07-接口与继承课后作业

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

运行结果截图:

程序源代码:


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.");
     super("1234567890");
        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 调用基类构造方法,必须是子类构造方法中的第一个语句。若不是第一个将会出现编译错误。

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

构造函数的主要作用:构造函数是类的一个特殊方法,这个方法用来生成实例时由系统自动调用,程序员无法直接调用。构造函数方法名同类名相同且参数为空。子类继承父类后默认继承父类的构造函数,即:子类存在隐含方法:super(),如果子类重写构造函数则子类也隐含调用super()。子类继承父类后默认继承父类的构造函数,所以不能反过来,因为这个方法由系统直接调用。

3.探索技术的奥秘

参看ExplorationJDKSource.java示例 此示例中定义了一个类A,它没有任何成员: class A { } 示例直接输出这个类所创建的对象 public static void main(String[] args) { System.out.println(new A()); }我们得到了一个奇特的运行结果: A@1c5f743

程序源代码:


public class ExplorationJDKSource {

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

}

class A{}

运行结果截图:

结论:前面示例中,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();

4.神奇的加号

程序源代码:

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

运行结果截图:

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

5.请自行编写代码测试以下特性(动手动脑): 在子类中,若要调用父类中被覆盖的方法,可以使用super关键字。

程序源代码:

public class Father {
public static void main(String[] args) {

Child c = new Child();
c.showMessage();
}
}

class Parent
{
public void showMessage()
{
System.out.println("parent!");
}
}

class Child extends Parent
{
public void showMessage()
{
System.out.println("child!");
super.showMessage();
}
}

运行结果截图:

原文地址:https://www.cnblogs.com/kangy123/p/6053908.html