Java实验 子类与继承(续)

本文介绍如何在Java中设计电器类及其子类冰箱、洗衣机和电视,演示了子类如何继承父类的属性和方法,并在Example.java中展示了如何实例化子类并调用其打印方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实验目的

  1. 掌握子类的继承性;
  2. 掌握子类的创建过程;
  3. 成员变量的继承与隐藏;
  4. 方法的继承与重写。

实验内容

题目:

我们计划为一个电器销售公司制作一套系统,公司的主要业务是销售一些家用电器,例如:电冰箱、洗衣机、电视机产品。

类的设计为:

  1. 冰箱类

属性:品牌、型号、颜色、售价、门款式、制冷方式;

方法:打印该冰箱的品牌和售价。

  1. 洗衣机类

属性:品牌、型号、颜色、售价、电机类型、洗涤容量;

方法:打印该洗衣机的品牌和售价。

  1. 电视类

属性:品牌、型号、颜色、售价、屏幕尺寸、分辨率。

方法:打印该电视机的品牌和售价。

要求:

请根据上述类的设计,抽取父类“电器类”,并代码实现父类“电器类”、子类“冰箱类”,“洗衣机类”、“电视类”。然后在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();
		
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值