1.电器类 一个电器销售公司想开发一套管理系統,该公司的主要业务是销售一些家用电器,倒如:电冰箱、洗衣机、电視机产品。 经调查,各种电器的属性如下: 冰箱类:品牌、型号、颜色、售价、门款式、制冷方式。 洗衣机美:品牌、型号、颜色、售价、电机类型、洗涤容量。 电視类:品牌、型号、颜色、售价、屏幕尺寸、分辦率。 不同的电器有不同的显示信息的方式。 要求:用封装、继承(包括this、super等) 等知识点编程实现。
时间: 2023-07-03 22:13:41 浏览: 95
以下是一个基于Java语言的电器销售公司管理系统的代码示例,其中使用了封装和继承等知识点:
```
// 定义电器类
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;
}
// getter和setter方法
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 class Refrigerator extends ElectricalAppliance {
private String doorStyle; // 门款式
private String coolingMethod; // 制冷方式
public Refrigerator(String brand, String model, String color, double price, String doorStyle, String coolingMethod) {
super(brand, model, color, price);
this.doorStyle = doorStyle;
this.coolingMethod = coolingMethod;
}
// getter和setter方法
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 class WashingMachine extends ElectricalAppliance {
private String motorType; // 电机类型
private int washingCapacity; // 洗涤容量
public WashingMachine(String brand, String model, String color, double price, String motorType, int washingCapacity) {
super(brand, model, color, price);
this.motorType = motorType;
this.washingCapacity = washingCapacity;
}
// getter和setter方法
public String getMotorType() {
return motorType;
}
public void setMotorType(String motorType) {
this.motorType = motorType;
}
public int getWashingCapacity() {
return washingCapacity;
}
public void setWashingCapacity(int washingCapacity) {
this.washingCapacity = washingCapacity;
}
}
// 定义电视类
public class Television extends ElectricalAppliance {
private int screenSize; // 屏幕尺寸
private String resolution; // 分辦率
public Television(String brand, String model, String color, double price, int screenSize, String resolution) {
super(brand, model, color, price);
this.screenSize = screenSize;
this.resolution = resolution;
}
// getter和setter方法
public int getScreenSize() {
return screenSize;
}
public void setScreenSize(int screenSize) {
this.screenSize = screenSize;
}
public String getResolution() {
return resolution;
}
public void setResolution(String resolution) {
this.resolution = resolution;
}
}
// 定义电器销售公司管理系统类
public class ElectricalApplianceManagementSystem {
public static void main(String[] args) {
Refrigerator r = new Refrigerator("Haier", "BCD-123", "White", 1999.99, "Double Door", "Direct Cooling");
System.out.println(r.getBrand() + " " + r.getModel() + " " + r.getColor() + " " + r.getPrice() + " " + r.getDoorStyle() + " " + r.getCoolingMethod());
WashingMachine w = new WashingMachine("LG", "WTG8520", "Silver", 2999.99, "Inverter Motor", 8);
System.out.println(w.getBrand() + " " + w.getModel() + " " + w.getColor() + " " + w.getPrice() + " " + w.getMotorType() + " " + w.getWashingCapacity());
Television t = new Television("Samsung", "UA55RU7100", "Black", 4999.99, 55, "3840x2160");
System.out.println(t.getBrand() + " " + t.getModel() + " " + t.getColor() + " " + t.getPrice() + " " + t.getScreenSize() + " " + t.getResolution());
}
}
```
以上代码示例中,`ElectricalAppliance`类是所有电器的基类,`Refrigerator`、`WashingMachine`和`Television`类都继承自`ElectricalAppliance`类,并且添加了各自特有的属性和方法。在`ElectricalApplianceManagementSystem`类中,我们创建了一个冰箱对象、一个洗衣机对象和一个电视对象,并打印出它们的属性。
阅读全文