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

  • 运行 TestInherits.java示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改Parent构造方法的代码,显式调用GrandParent的另一个构造函数,注意这句调用代码是否是第一句,影响重大!

 

1.没有super的调用

 1 class Grandparent
 2 {
 3
 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

2.有super的构造调用,且在第一行。

 1 class Grandparent
 2 {
 3
 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.String:Hello.Grandparent.
Parent Created
Child Created

3.super方法在输出语句的后面

 1 class Grandparent
 2 {
 3
 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 }

程序报错!

结论:

通过super调用基类的构造方法,必须是子类的构造方法中的第一个语句.(顺序不能打乱)

1.以final 开头的class类不允许继承

2.以final声明的方法不允许覆盖

3.以final声明的变量不允许更改

4.利用final,可以设计出一种特殊的“只读”的“不可变类”

原文地址:https://www.cnblogs.com/cxy0210/p/11728319.html

时间: 2024-08-28 23:04:03

java 继承条件下的构造方法调用的相关文章

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

运行TestInherits.java示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改Parent构造方法的代码,显式调用GrandParent的另一个构造函数,注意这句调用代码是否是第一句,影响重大! 程序: class Grandparent { public Grandparent() { System.out.println("GrandParent Created."); } public Grandparent(String string) { System.o

课堂例子验证—继承条件下的构造方法调用

代码一: 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

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

程序源代码: package homework;class Grandparent{ public Grandparent() { System.out.println("GrandParent Created."); }public Grandparent(String string){ System.out.println("GrandParent Created.String:"+string); }}class Parent extends Grandpar

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{p

JAVA继承关系中构造方法的调用次序详解

/*定义父类Base1*/ public class Base1 { int a;// 定义一个变量 /* 父类构造方法 */ public Base1(int a) { this.a = a; System.out.println("调用了父类的有参构造方法!"); } public Base1() { a = 1; System.out.println("调用了父类的无参构造方法!!"); ; } public static void main(String[]

Java 继承中的构造方法

Java 继承中的构造方法 子类可以继承父类的构造方法,并遵循以下原则: 子类构造的构成中必须调用其基类的构造方法.    2.子类可以在自己的构造方法中使用super()调用基类的构造方法. 使用this()调用本类的另外的构造方法. 如果调用super(),必须写在子类构造方法的第一行. 3.如果子类的构造方法中没有显示的调用基类构造方法,则系统默认调用基类无参的构造方法.    4.如果子类的构造方法中既没有显示的调用基类的构造方法,而基类中又没有无参的构造方法,则编译会出错. 1 cla

Java 继承中构造方法的执行顺序问题

在Java中,如果一个类没有任何显式创建的构造器则该类默认会有一个无参构造器:如果显式创建了有参构造器则该类就不再有默认无参构造器. 在Java继承中,构造器并不能被继承,而是被显示或隐式调用. 1.子类的构造方法中必须调用其基类的构造方法(显示或隐式) 1.1.若是显示调用,则可以通过 super(argument_list) 来调用,且super调用必须在首行以保证子类对象从所有直接或间接父类中继承的实例变量都被正确地初始化(this关键字可以调用本类中的其他构造器,也必须在首句,因此thi

在linux下sh批处理文件调用java的方法

解密 java -classpath collection-impl-0.0.1.jar com.ai.toptea.collection.message.DESEncrypt 1EFE46638952F577 dec 加密 java -classpath collection-impl-0.0.1.jar com.ai.toptea.collection.message.DESEncrypt 123456 enc 本文主要介绍在linux下sh批处理文件调用java的方法. shell编程的代

Java继承,多态,组合应用

继承:  面向对象的三大特征之一:    是类和类之间的一种拓展关系,是一种从一般到特殊的关系;    格式: sub   extends Super,  我们把sub称为子类或者拓展类, 把super称为父类或者基类(超类)   泛化: 把子类中的共性抽取到父类的过程; 特化: 子类在父类的基础之上,拓展自己特有的状态和特征;    Object: 是所有类的直接父类后间接父类;      class  Student{} 等价于  class  Student extends Object{