实验目的
- 掌握子类的继承性;
- 掌握子类的创建过程;
- 成员变量的继承与隐藏;
- 方法的继承与重写。
实验内容
题目:
我们计划为一个电器销售公司制作一套系统,公司的主要业务是销售一些家用电器,例如:电冰箱、洗衣机、电视机产品。
类的设计为:
- 冰箱类
属性:品牌、型号、颜色、售价、门款式、制冷方式;
方法:打印该冰箱的品牌和售价。
- 洗衣机类
属性:品牌、型号、颜色、售价、电机类型、洗涤容量;
方法:打印该洗衣机的品牌和售价。
- 电视类
属性:品牌、型号、颜色、售价、屏幕尺寸、分辨率。
方法:打印该电视机的品牌和售价。
要求:
请根据上述类的设计,抽取父类“电器类”,并代码实现父类“电器类”、子类“冰箱类”,“洗衣机类”、“电视类”。然后在Example.java中实现主函数,实例化子类,并分别调用打印方法。
package practice;
#电器类
public class ElectricalAppliance {
String brandName;
String model;
String color;
float price;
void show() {
System.out.println("brandName"+brandName);
System.out.println("model"+model);
System.out.println("color"+color);
System.out.println("price"+price);
}
}
package practice;
#冰箱类
public class Refrigerator extends ElectricalAppliance {
String doorStyle;
String refrigerationMethod;
void show() {
System.out.println("Refrigerator brandName: " + brandName);
System.out.println("Refrigerator price" + price);
}
}
package practice;
#洗衣机类
public class WashingMachine extends ElectricalAppliance {
String electricMachinery;
String washingCApacity;
void show() {
System.out.println("WashingMachine brandName: "+brandName);
System.out.println("WashingMachine price"+price);
}
}
package practice;
#电视类
public class Tv extends ElectricalAppliance {
String screenSize;
String resolution;
void show() {
System.out.println("Tv brandName: "+brandName);
System.out.println("Tv price"+price);
}
}
package practice;
public class Example {
public static void main(String[] args) {
Refrigerator geli = new Refrigerator();
geli.brandName ="geli";
geli.price = 3500f;
geli.show();
WashingMachine haier = new WashingMachine();
haier.brandName = "haier";
haier.price = 2500f;
haier.show();
Tv xiaomi = new Tv();
xiaomi.brandName="xiaomi";
xiaomi.price=5000f;
xiaomi.show();
}
}