super.displayInfo
时间: 2024-06-16 10:04:08 浏览: 5
super.displayInfo是一个在面向对象编程中常用的语法。它用于调用父类(或超类)中的方法。当子类继承了父类的方法,并且在子类中重写了该方法时,可以使用super.displayInfo来调用父类中的方法。
使用super.displayInfo的好处是可以在子类中扩展父类的方法,而不是完全重写它。这样可以保留父类方法的原有功能,并在子类中添加额外的逻辑。
相关问题
类的继承定义一个computer类在此基础上派生出两个子类(继承与多态绑定)用java语言
好的,下面是一个在Java中实现的示例代码:
```java
class Computer {
private String brand;
private String model;
public Computer(String brand, String model) {
this.brand = brand;
this.model = model;
}
public void displayInfo() {
System.out.println("Brand: " + this.brand);
System.out.println("Model: " + this.model);
}
}
class Laptop extends Computer {
private double weight;
public Laptop(String brand, String model, double weight) {
super(brand, model);
this.weight = weight;
}
@Override
public void displayInfo() {
super.displayInfo();
System.out.println("Weight: " + this.weight);
}
}
class Desktop extends Computer {
private int monitorSize;
public Desktop(String brand, String model, int monitorSize) {
super(brand, model);
this.monitorSize = monitorSize;
}
@Override
public void displayInfo() {
super.displayInfo();
System.out.println("Monitor Size: " + this.monitorSize);
}
}
```
在上面的代码中,我们定义了一个 `Computer` 类,它具有 `brand` 和 `model` 两个私有属性,以及一个 `displayInfo` 方法,用于显示计算机的信息。
然后,我们定义了两个子类 `Laptop` 和 `Desktop`,它们继承自 `Computer`。`Laptop` 类增加了一个 `weight` 属性,并重写了 `displayInfo` 方法,以便能够显示出计算机的重量。`Desktop` 类增加了一个 `monitorSize` 属性,并重写了 `displayInfo` 方法,以便能够显示出计算机的显示器尺寸。
这样,我们就可以创建 `Computer`、`Laptop` 和 `Desktop` 类的对象,并使用它们的方法来显示它们的信息了。具体示例如下:
```java
Computer computer = new Computer("Dell", "OptiPlex 9020");
computer.displayInfo();
Laptop laptop = new Laptop("Lenovo", "ThinkPad X1 Carbon", 1.13);
laptop.displayInfo();
Desktop desktop = new Desktop("HP", "EliteDesk 800 G1", 24);
desktop.displayInfo();
```
输出结果如下:
```
Brand: Dell
Model: OptiPlex 9020
Brand: Lenovo
Model: ThinkPad X1 Carbon
Weight: 1.13
Brand: HP
Model: EliteDesk 800 G1
Monitor Size: 24
```
定义类 Vehlcles:属性包括 size、color、seats,还有一个带参数的构造方法和一个显 示信息的方法。然后从此类中派生 Car 类,其中子类也有构造方法和显示信息的方法。 编写测试类程序,创建父类的对象,再创建子类的对象,用它们的显示信息的方法输 出它们的信息。
好的,您的问题是关于类的继承和对象的创建。以下是您需要的代码:
```java
class Vehicles {
private String size;
private String color;
private int seats;
public Vehicles(String size, String color, int seats) {
this.size = size;
this.color = color;
this.seats = seats;
}
public void displayInfo() {
System.out.println("Size: " + size);
System.out.println("Color: " + color);
System.out.println("Seats: " + seats);
}
}
class Car extends Vehicles {
public Car(String size, String color, int seats) {
super(size, color, seats);
}
public void displayInfo() {
System.out.println("This is a car.");
super.displayInfo();
}
}
public class Test {
public static void main(String[] args) {
Vehicles v1 = new Vehicles("medium", "blue", 5);
Car c1 = new Car("small", "red", 2);
v1.displayInfo();
System.out.println();
c1.displayInfo();
}
}
```
您可以将上述代码粘贴到 Java IDE 中运行,结果将是:
```
Size: medium
Color: blue
Seats: 5
This is a car.
Size: small
Color: red
Seats: 2
```
相关推荐
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)