接口的应用

来源:互联网 发布:软件是怎么设计的 编辑:程序博客网 时间:2024/05/02 06:09
package com.test4;




interface Car
{
String getName();
int getPrice();
}


class BMW implements Car
{
public String getName()
{
return "BMW";
}


public int getPrice() {
// TODO Auto-generated method stub
return 300000;
}
}




class CheryQQ implements Car
{


public String getName() {
// TODO Auto-generated method stub
return "CheryQQ";
}


public int getPrice() {
// TODO Auto-generated method stub
return 20000;
}

}


//汽车出售店
public class Demo1
{
private int money=0;
public void sellCar(Car car)
{
System.out.println("车型:"+car.getName()+" 单价:"+car.getPrice());
money+=car.getPrice();
}

public int getMoney()
{
return money;
}

public static void main(String[] args)
{
Demo1 demo1=new Demo1();
demo1.sellCar(new BMW());
demo1.sellCar(new CheryQQ());
System.out.println("总收入:"+demo1.getMoney());
}
}











0 0