testproduct.java

来自「java的源代码」· Java 代码 · 共 41 行

JAVA
41
字号
public class TestProduct{
	public static void main(String[] args){
		Product[] ps=new Product[2];
		ps[0]=new Book(100);
		ps[1]=new Video(100);
		double totalPrice=0;
		double totalRealPrice=0;
		for(int i=0;i<ps.length;i++){
			totalPrice+=ps[i].getPrice();
			totalRealPrice+=ps[i].getRealPrice();
		}
		System.out.println(totalPrice);
		System.out.println(totalRealPrice);
	}
}
abstract class Product{
	private double price;
	public Product(double price){
		this.price=price;
	}
	public double getPrice(){
		return price;
	}
	public abstract double getRealPrice();
}
class Book extends Product{
	public Book(double price){
		super(price);
	}
	public double getRealPrice(){
		return getPrice()*0.8;
	}
}
class Video extends Product{
	public Video(double price){
		super(price);	
	}
	public double getRealPrice(){
		return getPrice()*0.7;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?