如何编写一个返回抽象类实例的方法?

时间:2013-03-15 20:10:43

标签: java

我是Java的初学者,我试图理解抽象类。 下面是我写的代码;问题是:我如何编写一个返回该类实例的方法。

public abstract class VehicleEngine
{
    protected String name;
    protected double fabricationCons;
    protected double consum;
    protected int mileage;

    public VehicleEngine(String n, double fC)
    {
        name = n;
        fabricationCons = fC;
        mileage = 0;
        consum = 0;
    }

    private void setFabricationCons(double fC)
    {
        fabricationCons = fC;
    }

    public abstract double currentConsum();

    public String toString()
    {
        return name + " : " + fabricationCons + " : " + currentConsum();
    }

    public void addMileage(int km)
    {
        mileage += km;
    }

    public double getFabricationConsum()
    {
        return fabricationCons;
    }

    public String getName()
    {
        return name;
    }

    public int getMileage()
    {
        return mileage;
    }

    //public VehicleEngine get(String name){ 
    //if(getName().equals(name)){
    //return VehicleEngine;
    //}
    //return null;
    //}
}

public class BenzinVehicle extends VehicleEngine
{
    public BenzinVehicle(String n, double fC)
    {
        super(n, fC);
    }

    @Override
    public double currentConsum()
    {
        if (getMileage() >= 75000) {
            consum = getFabricationConsum() + 0.4;
        } else {
            consum = getFabricationConsum();
        }
        return consum;
    }
}

public class DieselVehicle extends VehicleEngine
{
    public DieselVehicle(String n, double fC)
    {
        super(n, fC);
    }

    @Override
    public double currentConsum()
    {
        int cons = 0;
        if (getMileage() < 5000) {
            consum = getFabricationConsum();
        } else {
            consum = getFabricationConsum() + (getFabricationConsum() * (0.01 * (getMileage() / 5000)));
        }
        return consum;
    }
}

这是主要的。

public class Subject2
{
    public static void main(String[] args)
    {
        VehicleEngine c1 = new BenzinVehicle("Ford Focus 1.9", 5.0);
        DieselVehicle c2 = new DieselVehicle("Toyota Yaris 1.4D", 4.0);
        BenzinVehicle c3 = new BenzinVehicle("Citroen C3 1.6",5.2);

        c1.addMileage(30000);
        c1.addMileage(55700);
        c2.addMileage(49500);
        c3.addMileage(35400);

        System.out.println(c1);
        System.out.println(c2);
        System.out.println(VehicleEngine.get("Citroen C3 1.6")); //this is the line with problems
        System.out.println(VehicleEngine.get("Ford Focus "));
    }
}

输出应为:

Ford Focus 1.9 : 5.0 : 5.4
Toyota Yaris 1.4D : 4.0 : 4.36
Citroen C3 1.6 : 5.2 : 5.2
null

3 个答案:

答案 0 :(得分:3)

根据定义,您可以返回抽象类的实例。你可以做的是返回一个扩展它的具体(非抽象)子类的实例。例如,在VehicleEngine内部,您可以创建一个工厂,该工厂返回给定实例类型和预期参数的实例,但这些实例必须是VehicleEngine的具体子类

答案 1 :(得分:1)

查看Factory Method模式。您的具体类将实现一个返回类实例的抽象方法。

答案 2 :(得分:0)

抽象类不保留其实例列表。实际上没有Java类可以做到这一点。如果您真的想这样做,可以像这样添加一个静态地图到VehicleEngine:

private static Map<String, VehicleEngine> instanceMap = new HashMap<String, VehicleEngine>();

并将您的get方法更改为静态方法,如下所示:

public static VehicleEngine get(String name) {
    return instanceMap.get(name);
}

并将此行添加到VehicleEngine构造函数的末尾:

VehicleEngine.instanceMap.put(n, this);

这样,每个创建的新实例都会将自身置于静态地图中。然而,这实际上不是实现这种功能的好方法。您可以尝试使用工厂来创建实例,或者如果您将拥有有限的预定义实例数,则可以考虑将此类转换为枚举。