(习题)编写一个类,描述汽车,其中用字符型描述车的牌号,用浮点型描述车的价格。编写一个测试类,其中有一个修改价格的方法,对汽车对象进行操作,根据折扣数修改汽车的价格,最后在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 + "万元");
}
}
本人初学,如有疑问或不对之处请指出。欢迎交流。