2 u013167422 u013167422 于 2013.12.15 21:40 提问

java类中的返回语句提取

interface Car{
String getName();
int getPrice();
int getQuan();
int getIndividualSales();
}
class CarShop{
private int money_a=0;
private int money_b=0;
public void saleCar(Car car){
System.out.println("品牌"+car.getName()+";售价:"+car.getPrice()+";售出数量:"+car.getQuan()+";品牌销售额:"+car.getIndividualSales());
money_a=car.getPrice()*car.getQuan();
money_b+=car.getPrice()*car.getQuan();

}
public int getMoney_a(){
return money_a;
}
public int getMoney_b(){
return money_b;
}
}
class BMW implements Car{
public String getName(){
return"宝马";
}
public int getPrice (){
return 600000;
}
public int getQuan (){
return 30;
}
public int getIndividualSales(){
return (getPrice()*getQuan());
}
}
class QQ implements Car{
public String getName(){
return"奇瑞QQ";
}
public int getPrice(){
return 4000;
}
public int getQuan (){
return 23;
}
public int getIndividualSales(){
return (getPrice()*getQuan());
}
}
class VW implements Car{
public String getName(){
return"上海大众";
}
public int getPrice(){
return 200000;
}
public int getQuan (){
return 13;
}
public int getIndividualSales(){
return (getPrice()*getQuan());
}
}
public class Demo56_A{
public static void main(String arge[]){
CarShop shopCar=new CarShop();
shopCar.saleCar(new BMW());
shopCar.saleCar(new QQ());
shopCar.saleCar(new VW());
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
System.out.println("销售总额:"+shopCar.getMoney_b());
}
}

通过比较,可以看出QQ、BMW、VM这三个类的返回语句都有一个共同点都含有“
public String getName();
public int getPrice();
public int getQuan();
public int getIndividualSales();”.在每个类中都写上这样的代码我感觉很麻烦,有没有简洁的方法呀?比如将QQ、BMW、VM三个类中的这四条返回语句提取出来建立一个新类。(我是新手,希望各位前辈多多帮助,谢谢了)

3个回答

olinchy
olinchy   2013.12.16 12:27

public class Test
{
public static void main(String arge[])
{
CarShop shopCar = new CarShop();
shopCar.saleCar(new CarSale(1, new VW()));
shopCar.saleCar(new CarSale(10, new QQ()));
shopCar.saleCar(new CarSale(2, new BMW()));

    System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
    System.out.println("销售总额:" + shopCar.getSum());
}

}

abstract class Car
{
String getName()
{
String className = this.getClass().getName();
return className.substring(className.lastIndexOf("."), className.length() - 1);
}

int countPrice(int quan)
{
    return this.price() * quan;
}

abstract protected int price();

}

interface Formatter
{
void print(HashMap keyValues);
}

class AllInfoFormatter implements Formatter
{

@Override
public void print(HashMap<String, String> keyValues)
{
    //        System.out.println("品牌" + car.getName() + ";售价:" + car.getPrice() + ";售出数量:"
    //                + car.getQuan() + ";品牌销售额:" + car.getIndividualSales());
    StringBuilder builder = new StringBuilder();
    for (Entry<String, String> entry : keyValues.entrySet())
    {
        builder.append(entry.getKey() + ":" + entry.getValue() + " ");
    }

    System.out.println(builder.toString());
}

}

class CarSale
{

private final int amount;
private final Car car;

CarSale(int amount, Car car)
{
    this.amount = amount;
    this.car = car;
}

void print(Formatter formatter)
{
    HashMap<String, String> keyValues = new HashMap<String, String>();
    keyValues.put("Brand", car.getName());
    keyValues.put("Price", String.valueOf(car.price()));
    keyValues.put("Amount", String.valueOf(amount));
    keyValues.put("Sum", String.valueOf(car.countPrice(amount)));

    formatter.print(keyValues);
}

public int sumPrize()
{
    return this.car.countPrice(amount);
}

}

class CarShop
{
public int getOneTimeSale()
{
return oneTimeSale;
}

public int getSum()
{
    return sum;
}

private int oneTimeSale = 0;
private int sum = 0;

public void saleCar(CarSale sale)
{

    //        System.out.println("品牌" + car.getName() + ";售价:" + car.getPrice() + ";售出数量:"
    //                + car.getQuan() + ";品牌销售额:" + car.getIndividualSales());
    sale.print(new AllInfoFormatter());

    oneTimeSale = sale.sumPrize();
    sum += oneTimeSale;

}

}

class BMW extends Car
{

@Override
protected int price()
{
    return 600000;
}

}

class QQ extends Car
{

@Override
protected int price()
{
    return 4000;
}

}

class VW extends Car
{
@Override
protected int price()
{
return 200000;
}
}

按照你的需求做了下修改,这样每增加一种车型就只需要完成定价就可以了

你看下可以再增加一些统计功能

u013167422
u013167422 谢谢您
大约 4 年之前 回复
a_liujian
a_liujian   2013.12.16 13:57

楼主既然用到了多态实现,如果再接着往下考虑到动态创建类的实例(通过专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类)哪么就是一个完整的静态工厂设计模式的例子了,就如 sasuke38 所说,已经是ok的了.有空可以去看看工厂、命令等等模式的例子。

分析:
如果父类Car的4个属性方法,它的实现就如楼主哪样,只是简单的运算返回,如你写的哪样只是一些常量值,哪么建议最好是提取出来,如果是每种车的不同属性都去定义一个接口方法,确实太多,没必要!(提取出来可以参考枚举,可以为不同的车绑定各自的变量) 反之,如果接口里面定义的是几种有不同共性的方法,哪么建议还是按你目前哪样写。

sasuke38
sasuke38   2013.12.16 10:14

这样子感觉没什么 而且这样做是不错的

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!