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);
}
}
父、子类间的Upcasting 及 java时间的修改
最新推荐文章于 2024-07-03 00:03:03 发布