动手动脑(继承与接口)

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放在首行的结果:

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

         super的用法1)调用父类的构造方法

                            2)操作被隐藏的成员变量和被覆盖的成员方法

        语法格式为:super.成员变量名    

                         super.成员方法名([参数列表])

 

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

分析:必须调用,不能反过来,构造函数的作用就是初始化子类继承了父类的各种属性,而构造方法则相当于把父类给实例化出来所以一定会先调用父类的构造方法。

 

3.参看ExplorationJDKSource.java示例

此示例中定义了一个类A它没有任何成员:

class A { }

示例直接输出这个类所创建的对象

public static void main(String[] args)    {

System.out.println(new 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.我们来看一段代码(示例Fruit.java ):

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关键字。

1)覆盖方法的允许访问范围不能小于原方法。

2)覆盖方法所抛出的异常不能比原方法更多。

3)声明为final方法不许覆盖

     例如,Object的getClass()方法不能覆盖。

(4)不能覆盖静态方法。

 

package TestInherits;

public class Person {

String name;

    int age;

    double score;

    void introduce()

    {

     String name;

        int age;

        double score;

System.out.println("我的名字是:"+name+",我的年龄是:"+age+",我的成绩是:"+score);

}

}

package TestInherits;

public class Student extends Person {

void introduce()

    {

     System.out.println("我的名字是:"+name+",我的年龄是:"+age+",我的成绩是:"+score);     

    }

}

 

package TestInherits;

public class Fruit {

 public static void main(String[] args){

 String name;

    int age;

    double score;

        // 生成子类对象,调用子类的方法

        Student a=new Student();

        a.name="hsn";

        a.age=10;

        a.score=99.0;

        a.introduce();

        // 生成父类对象,调用父类的方法

        Person b=new Person();

        b.name="hhh";

        b.age=20;

        b.score=120;

        b.introduce();

    }

}

 

结果:

 

原文地址:https://www.cnblogs.com/sunqw/p/6053767.html