`

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

    博客分类:
  • 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);
		 
	 }
 }



分享到:
评论

相关推荐

    countdownlatch-example-sourcecode.zip

    countdownlatch-example-sourcecode.zip

    java编程第八讲----多态

    1、再谈向上类型转换(upcasting) 忘记对象的类型 2、多态机理 方法绑定(method binding) 产生正确的行为 可扩展性 陷阱:置换私有方法 3、抽象类与抽象方法 4、构造方法与多态 构造方法的...

    Thinking in Java 4th Edition

    Java SE5 and SE6 .................. 2 Java SE6 ......................................... 2 The 4th edition........................ 2 Changes .......................................... 3 Note on the ...

    多态.docx

    多态 ->1.多态概念 ->2.类型转换(upcasting&downcasting) ->3.动态绑定 ->4.抽象类 ->5.实例讲解

    考研大纲c++

    继承包括继承中的存取控制、构造、析构的调用顺序、私有继承、运算符重载与继承、upcasting等。 八、多态性与虚函数 多态性是C++语言中的一种机制,它允许一个对象在运行时确定其类型。多态性包括 早/晚绑定、虚...

Global site tag (gtag.js) - Google Analytics