Java面向对象编程:构造函数链与接口详解
立即解锁
发布时间: 2025-08-17 02:35:33 阅读量: 16 订阅数: 22 


Java编程基础与SCJP认证指南
### Java面向对象编程:构造函数链与接口详解
#### 构造函数链:this()和super()的使用
在Java面向对象编程中,构造函数起着初始化对象的重要作用。构造函数不能被继承或重写,但可以在同一个类中进行重载。当为一个类定义多个构造函数时,由于构造函数的名称总是与类名相同,因此每个构造函数的参数列表必须不同。
##### this()构造函数调用
`this()`构造函数调用用于在同一个类中实现构造函数的局部链式调用。以下是一个示例:
```java
class Light {
// 字段
private int noOfWatts; // 瓦数
private boolean indicator; // 开关状态
private String location; // 位置
// 构造函数
Light() {
this(0, false);
System.out.println("Returning from default constructor no. 1.");
}
Light(int watt, boolean ind) {
this(watt, ind, "X");
System.out.println("Returning from non-default constructor no. 2.");
}
Light(int noOfWatts, boolean indicator, String location) {
this.noOfWatts = noOfWatts;
this.indicator = indicator;
this.location = location;
System.out.println("Returning from non-default constructor no. 3.");
}
}
public class DemoThisCall {
public static void main(String[] args) {
System.out.println("Creating Light object no. 1.");
Light light1 = new Light();
System.out.println("Creating Light object no. 2.");
Light light2 = new Light(250, true);
System.out.println("Creating Light object no. 3.");
Light light3 = new Light(250, true, "attic");
}
}
```
在上述代码中,`Light`类有三个构造函数。默认构造函数`Light()`调用了`this(0, false)`,而`Light(int watt, boolean ind)`调用了`this(watt, ind, "X")`。当创建`Light`对象时,构造函数按照逆序执行,即先执行最深层的构造函数,最后执行最初调用的构造函数。
Java要求`this()`调用必须作为构造函数的第一条语句,这是为了处理子类对象创建时父类构造函数的调用机制。
以下是程序的输出:
```plaintext
Creating Light object no. 1.
Returning from non-default constructor no. 3.
Returning from non-default constructor no. 2.
Returning from default constructor no. 1.
Creating Light object no. 2.
Returning from non-default constructor no. 3.
Returning from non-default constructor no. 2.
Creating Light object no. 3.
Returning from non-default constructor no. 3.
```
##### super()构造函数调用
`super()`构造函数调用用于在子类构造函数中调用直接父类的构造函数。这使得子类在创建对象时能够影响其继承状态的初始化。以下是一个示例:
```java
class Light {
// 字段
private int noOfWatts;
private boolean indicator;
private String location;
// 构造函数
Light() {
this(0, false);
System.out.println("Returning from default constructor no. 1 in class Light");
}
Light(int watt, boolean ind) {
this(watt, ind, "X");
System.out.println("Returning from non-default constructor no. 2 in class Light");
}
Light(int noOfWatts, boolean indicator, String location) {
super();
this.noOfWatts = noOfWatts;
this.indicator = indicator;
this.location = location;
System.out.println("Returning from non-default constructor no. 3 in class Light");
}
}
class TubeLight extends Light {
// 实例变量
private int tubeLength;
private int colorNo;
// 构造函数
TubeLight(int tubeLength, int colorNo) {
this(tubeLength, colorNo, 100, true, "Unknown");
System.out.println("Returning from non-default constructor no. 1 in class TubeLight");
}
TubeLight(int tubeLength, int colorNo, int noOfWatts, boolean indicator, String location) {
super(noOfWatts, indicator, location);
this.tubeLength = tubeLength;
this.colorNo = colorNo;
System.out.println("Returning from non-default constructor no. 2 in class TubeLight");
}
}
public class Chaining {
public static void main(String[] args) {
System.out.println("Creating a TubeLight object.");
TubeLight tubeLightRef = new TubeLight(20, 5);
}
}
```
在上述代码中,`TubeLight`类是`Light`类的子类。`TubeLight`的构造函数通过`super()`调用了`Light`类的相应构造函数。
以下是程序的输出:
```plaintext
Creating a TubeLight object.
Returning from non-default constructor no. 3 in class Light
Returning from non-default constructor no. 2 in class TubeLight
Returning from non-default constructor no. 1 in class
```
0
0
复制全文
相关推荐









