1

我是 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
4

3 回答 3

3

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

于 2013-03-15T20:16:13.420 回答
1

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

于 2013-03-15T20:15:37.173 回答
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);

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

于 2013-03-15T20:28:33.197 回答