您的位置:首页 > 编程语言 > Java开发

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

2017-11-06 13:01 232 查看
(习题)编写一个类,描述汽车,其中用字符型描述车的牌号,用浮点型描述车的价格。编写一个测试类,其中有一个修改价格的方法,对汽车对象进行操作,根据折扣数修改汽车的价格,最后在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 + "万元");
}
}
本人初学,如有疑问或不对之处请指出。欢迎交流。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息