java学习记录——类和对象的练习3

(习题)编写一个类,描述汽车,其中用字符型描述车的牌号,用浮点型描述车的价格。编写一个测试类,其中有一个修改价格的方法,对汽车对象进行操作,根据折扣数修改汽车的价格,最后在main()方法中输出修改后的汽车信息。

class Car{
	public String chePai;
	public double price;
	public Car(String chePai,double price){
		this.chePai = chePai;
		this.price = price;
	}
}
class CarTest extends Car{
	public double discount;
	public CarTest(String chePai,double price,double discount){
		super(chePai,price);
		this.discount = discount;
	}
	public double alterPrice(){
		this.price = (this.price/10) * this.discount;
		return price;
		}
}
public class Test4_1{
	public static void main(String[] args){
		CarTest ct = new CarTest("粤A00000",37.6,8.8);
		System.out.println("车牌号为      :" + ct.chePai);
		System.out.println("汽车原价为    :" + ct.price + "万元");
		System.out.println("折扣为        :" + ct.discount);
		ct.alterPrice();
		System.out.println("优惠后的价格为:" + (int)ct.price + "万元");
	}
}

本人初学,如有疑问或不对之处请指出。欢迎交流。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值