1. 电器类 一个电器销售公司想开发一套管理系统,该公司的主要业务是销售一些家用电器,例如:电冰箱、洗衣机、电视机产品。 经调查,各种电器的属性如下: 冰箱类:品牌、型号、顾色、售价、门款式、制冷方式。 洗衣机类:品牌、型号、顾色、售价、电机类型、洗涤容量。 电视类:品牌、型号、颜色、售价、屏幕尺才、分辨率。 不同的电器有不同的显示信息的方式。 要求:用封装、继承(包括this、super等) 等知识点编程实现。
时间: 2023-07-04 22:11:51 浏览: 90
下面是一个简单的实现,其中包括一个父类ElectricalAppliance和三个子类Fridge、WashingMachine和Television:
```
public class ElectricalAppliance {
private String brand;
private String model;
private String color;
private double price;
public ElectricalAppliance(String brand, String model, String color, double price) {
this.brand = brand;
this.model = model;
this.color = color;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public void showInfo() {
System.out.println("品牌:" + brand);
System.out.println("型号:" + model);
System.out.println("颜色:" + color);
System.out.println("售价:" + price);
}
}
public class Fridge extends ElectricalAppliance {
private String doorStyle;
private String coolingMethod;
public Fridge(String brand, String model, String color, double price, String doorStyle, String coolingMethod) {
super(brand, model, color, price);
this.doorStyle = doorStyle;
this.coolingMethod = coolingMethod;
}
public String getDoorStyle() {
return doorStyle;
}
public void setDoorStyle(String doorStyle) {
this.doorStyle = doorStyle;
}
public String getCoolingMethod() {
return coolingMethod;
}
public void setCoolingMethod(String coolingMethod) {
this.coolingMethod = coolingMethod;
}
public void showInfo() {
super.showInfo();
System.out.println("门款式:" + doorStyle);
System.out.println("制冷方式:" + coolingMethod);
}
}
public class WashingMachine extends ElectricalAppliance {
private String motorType;
private double washingCapacity;
public WashingMachine(String brand, String model, String color, double price, String motorType, double washingCapacity) {
super(brand, model, color, price);
this.motorType = motorType;
this.washingCapacity = washingCapacity;
}
public String getMotorType() {
return motorType;
}
public void setMotorType(String motorType) {
this.motorType = motorType;
}
public double getWashingCapacity() {
return washingCapacity;
}
public void setWashingCapacity(double washingCapacity) {
this.washingCapacity = washingCapacity;
}
public void showInfo() {
super.showInfo();
System.out.println("电机类型:" + motorType);
System.out.println("洗涤容量:" + washingCapacity);
}
}
public class Television extends ElectricalAppliance {
private String screenSize;
private String resolution;
public Television(String brand, String model, String color, double price, String screenSize, String resolution) {
super(brand, model, color, price);
this.screenSize = screenSize;
this.resolution = resolution;
}
public String getScreenSize() {
return screenSize;
}
public void setScreenSize(String screenSize) {
this.screenSize = screenSize;
}
public String getResolution() {
return resolution;
}
public void setResolution(String resolution) {
this.resolution = resolution;
}
public void showInfo() {
super.showInfo();
System.out.println("屏幕尺寸:" + screenSize);
System.out.println("分辨率:" + resolution);
}
}
```
在这个实现中,ElectricalAppliance是所有电器的父类,其中包含一些共同的属性和方法。每个电器类都继承了ElectricalAppliance,并添加了自己特有的属性和方法。每个电器类中都重写了showInfo()方法,以便根据该类的属性显示该电器的信息。
阅读全文