java课程课后作业04之动手动脑

1.继承条件下的构造方法调用

  先贴出实验需要的代码:

 1 package TestJava;
 2 
 3 class Grandparent 
 4 {
 5     public Grandparent()
 6      {
 7 
 8             System.out.println("GrandParent Created.");
 9     
10 }
11 
12 
13     public Grandparent(String string) 
14     {
15 
16             System.out.println("GrandParent Created.String:" + string);
17     
18  }
19 
20 }
21 
22 
23 
24 class Parent extends Grandparent
25 {
26 
27 
28     public Parent()
29      {
30 
31             //super("Hello.Grandparent.");
32 
33             System.out.println("Parent Created");
34     
35        // super("Hello.Grandparent.");
36 
37       }
38 
39 }
40 
41 
42 
43 class Child extends Parent 
44 {
45 
46 
47     public Child()
48      {
49     
50         System.out.println("Child Created");
51 
52       }
53 
54 }
55 
56 
57 
58 public class TestInherits 
59 {
60 
61 
62     public static void main(String args[])
63      {
64 
65             Child c = new Child();
66     
67   }
68 
69 }

  我们在第一次运行的时候发现输出顺序是:

    GrandParent Created

    Parent Created

    Child Created

  结论1:当我们去运行代码的时候,父类的构造函数会先于子类的构造函数。

  然后,在当我们把子类中的父类的构造方法取消注释,我们会发现同样的结果,然后当我们把父类的构造方法放入子类的构造方法之后,我们会发现,代码无法编译通过,由此得到结论。

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

  结论2的原因:由于父类的构造方法放在了子类的后面,如果一旦可以运行,则违反了结论1,所以编译无法通过。

2.判断对象是否可以转换

  贴出实验代码:

 1 class Mammal{}
 2 class Dog extends Mammal {}
 3 class Cat extends Mammal{}
 4 
 5 public class TestCast
 6 {
 7     public static void main(String args[])
 8     {
 9         Mammal m;
10         Dog d=new Dog();
11         Cat c=new Cat();
12         m=d;
13         //d=m;
14         d=(Dog)m;
15         //d=c;
16         //c=(Cat)m;
17 
18     }
19 }

  在没有注释的代码编译可以通过,当中可以看出父类可以直接对子类进行转换,而子类如果要对父类进行转换的话必须要在父类的前面加上子类的名字来进行强制转换,否则编译无法通过。

3.对于被重写的父类如何被调用

  贴出实验代码:

 1 package TestJava1;
 2 public class ParentChildTest {
 3     public static void main(String[] args) {
 4         Parent parent=new Parent();
 5         parent.printValue();//100
 6         Child child=new Child();
 7         child.printValue();//200
 8         
 9         parent=child;
10         parent.printValue();//200
11         
12         parent.myValue++;
13         parent.printValue();//200
14         
15         ((Child)parent).myValue++;
16         parent.printValue();//201
17         
18     }
19 }
20 
21 class Parent{
22     public int myValue=100;
23     public void printValue() {
24         System.out.println("Parent.printValue(),myValue="+myValue);
25     }
26 }
27 class Child extends Parent{
28     public int myValue=200;
29     public void printValue() {
30         System.out.println("Child.printValue(),myValue="+myValue);
31     }
32 }

  运行结果已经写在了注释的后面。

  分析:parent.myValue++;此行代码的结果是对子类的myValue进行编辑,而parent.printValue();却是对父类的myValue数值进行输出,代码((Child)parent).myValue++;是对父类的的数值编辑,所以再次输出的时候就是201.

  结论1:当子类与父类拥有一样的方法,并且让一个父类变量引用一个子类对象时,到底调用哪个方法,由对象自己的“真实”类型所决定,这就是说:对象是子类型的,它就调用子类型的方法,是父类型的,它就调用父类型的方法。

  结论2:如果子类与父类有相同的字段,则子类中的字段会代替或隐藏父类的字段,子类方法中访问的是子类中的字段(而不是父类中的字段)。如果子类方法确实想访问父类中被隐藏的同名字段,可以用super关键字来访问它。

  结论3:如果子类被当作父类使用,则通过子类访问的字段是父类的!                 就是这段代码-> ((Child)parent).myValue++;

原文地址:https://www.cnblogs.com/heiyang/p/9890601.html