1. 程式人生 > >一個經典的多型Demo

一個經典的多型Demo

class Car{
	static int num;
	int price;
	String name;
	Car(int price,String name){
		this.price = price;
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public int getPrice() {
		return price;
	}
	public void sell(Car car){
		System.out.println("賣"+car.getName()+"車得到了"+car.getPrice());
	}
}
class BMW extends Car{
	public BMW(int a ,String name) {
		super(a, name);
		
	}
	public String getName() {
		return name;
	}
	public int getPrice() {
		return price;
	}
}
class bentian extends Car{

	bentian(int price, String name) {
		super(price, name);
	}
	public String getName() {
		return name;
	}
	public int getPrice() {
		return price;
	}
	
}

public class dtDemo {

	
	public static void main(String[] args) {
		Car car = new Car(100, "s");
		car.sell(new BMW(200, "BWM"));
		car.sell(new bentian(300, "Bentian"));
	}

}