⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testproduct.java

📁 java的源代码
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -