Java多态的体现之接口

/**
 * 
 * @author Administrator
 * 功能:接口体现多态
 */
package com.test4;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CarShop aShop = new CarShop();
        aShop.sellCar(new BMW());
        aShop.sellCar(new CherryQQ());
        System.out.println("总收入:"+aShop.getMoney());    
        
    }

}

interface Car
{
    //汽车名称
    String getName();
    //获得汽车售价
    int getPrice();
}

//宝马
class BMW implements Car
{

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

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

//奇瑞QQ
class CherryQQ implements Car{

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

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

//汽车出售店
class CarShop
{
    //售车收入
    private int money = 0;
    
    //卖出一部车
    public void sellCar(Car car)
    {
        System.out.println("车型:"+car.getName()+" 单价"+car.getPrice());
        //增加卖出 车售价的收入
        money += car.getPrice();
    }
    
    //售车总收入
    public int getMoney()
    {
        return this.money;        
    }
    
}

 

posted on 2016-03-31 22:40  让编程成为一种习惯  阅读(291)  评论(0编辑  收藏  举报