父、子类间的Upcasting 及 java时间的修改


import java.util.Calendar;
class Product { //一般商品类
String productName; //名称
double price; //价格
String ID; //编号
String unit; //单价

public Product(String ID,String productName,double price,String unit){ //父类构造函数
this.ID = ID;
this.productName = productName;
this.price = price;
this.unit = unit;
}
public String getProductName(){
return this.productName;
}
public double getPrice(){
return this.price;
}
public String getID(){
return this.ID;
}
public String getUnit(){
return this.unit;
}
public String toString(){
return "商品ID为:"+this.ID+"\t商品名称为:"+this.productName+"价格:"+this.price+"单位:"+this.unit;
}
}
public class DiscountedProduct11 extends Product{
private Calendar startDate;
private Calendar endDate;
private double discount; //折扣
public DiscountedProduct11(String ID,String productName,double price,String unit,
Calendar startDate,Calendar endDate,double discount){ //子类构造函数
super(ID,productName,price,unit); //****************************
this.startDate = startDate;
this.endDate = endDate;
this.discount = discount;
}
public Calendar getStartDate(){
return this.startDate;
}
public Calendar getEndDate(){
return this.endDate;
}
public double getDiscount(){
return this.discount;
}
public String toString(){
return "商品ID为:"+this.ID+"\n商品名称为:"+this.productName+"\n价格:"+this.price+"\n单位:"+this.unit+
"\n开始时间为:"+this.startDate.getTime()+"\n结束时间为:"+this.endDate.getTime()+"\n折扣率为:"+this.discount;
}

public static void main(String args[]){

System.out.println("当前时间为:");
Calendar cal = Calendar.getInstance();
System.out.println(cal.getTime()); //获得当前时间

Calendar cal2=Calendar.getInstance();
cal2.set(Calendar.MONDAY, 10); //修改时间属性

System.out.println("*****************************************");
DiscountedProduct11 DP = new DiscountedProduct11("001","苹果",2.0,"/kg",cal,cal2,0.2);
System.out.println(DP);

System.out.println("*****************************************");
Product p = new DiscountedProduct11("002","香蕉",3.0,"/kg",cal,cal2,0.3); //upcasting
System.out.println(p);

}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值