2. 海尔(Haier)、TCL、海信(Hisense)都是家电制造商,它们都生产电视机(Television)、空调(Air Conditioner)、冰箱(Refrigeratory)。现需要设计一个系统,描述这些家电制造商以及它们所制造的电器,要求绘制类图并用代码模拟实现。
时间: 2024-06-18 18:03:26 浏览: 29
根据题目要求,我们需要设计一个系统来描述家电制造商和它们所制造的电器。我们可以先列出以下几个类:
1. 家电制造商(Manufacturer):该类用于描述家电制造商,包括制造商的名称(name)、所生产的电器列表(products)等属性,以及向产品列表中添加电器(addProduct)、从产品列表中删除电器(deleteProduct)等方法。
2. 电器(Appliance):该类用于描述电器,包括电器的名称(name)、品牌(brand)、型号(model)、价格(price)等属性,以及打开电器(turnOn)、关闭电器(turnOff)等方法。
3. 电视机(Television):该类继承自电器类,包括电视机的屏幕尺寸(screenSize)、分辨率(resolution)等属性,以及调节音量(adjustVolume)、切换频道(changeChannel)等方法。
4. 空调(AirConditioner):该类继承自电器类,包括空调的制冷能力(coolingCapacity)、制热能力(heatingCapacity)等属性,以及调节温度(adjustTemperature)、调节风速(adjustWindSpeed)等方法。
5. 冰箱(Refrigerator):该类继承自电器类,包括冰箱的容积(volume)、制冷方式(coolingMethod)等属性,以及设置温度(setTemperature)、打开冰箱门(openDoor)等方法。
下面是代码实现:
```
class Manufacturer {
private String name;
private List<Appliance> products;
public Manufacturer(String name) {
this.name = name;
products = new ArrayList<>();
}
public void addProduct(Appliance appliance) {
products.add(appliance);
}
public void deleteProduct(Appliance appliance) {
products.remove(appliance);
}
}
class Appliance {
protected String name;
protected String brand;
protected String model;
protected double price;
protected boolean isOn;
public Appliance(String name, String brand, String model, double price) {
this.name = name;
this.brand = brand;
this.model = model;
this.price = price;
isOn = false;
}
public void turnOn() {
isOn = true;
System.out.println(name + " is turned on.");
}
public void turnOff() {
isOn = false;
System.out.println(name + " is turned off.");
}
}
class Television extends Appliance {
private double screenSize;
private String resolution;
public Television(String name, String brand, String model, double price, double screenSize, String resolution) {
super(name, brand, model, price);
this.screenSize = screenSize;
this.resolution = resolution;
}
public void adjustVolume(int volumeLevel) {
System.out.println("Volume level is adjusted to " + volumeLevel);
}
public void changeChannel(int channelNumber) {
System.out.println("Channel is switched to " + channelNumber);
}
}
class AirConditioner extends Appliance {
private int coolingCapacity;
private int heatingCapacity;
public AirConditioner(String name, String brand, String model, double price, int coolingCapacity, int heatingCapacity) {
super(name, brand, model, price);
this.coolingCapacity = coolingCapacity;
this.heatingCapacity = heatingCapacity;
}
public void adjustTemperature(int temperature) {
System.out.println("Temperature is adjusted to " + temperature);
}
public void adjustWindSpeed(int windSpeedLevel) {
System.out.println("Wind speed is adjusted to " + windSpeedLevel);
}
}
class Refrigerator extends Appliance {
private int volume;
private String coolingMethod;
public Refrigerator(String name, String brand, String model, double price, int volume, String coolingMethod) {
super(name, brand, model, price);
this.volume = volume;
this.coolingMethod = coolingMethod;
}
public void setTemperature(int temperature) {
System.out.println("Temperature is set to " + temperature);
}
public void openDoor() {
System.out.println("Door is opened.");
}
}
```
下面是几个相关问题:
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)