Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 工厂模式及;构造函数中的可重写方法调用_Java_Factory Pattern - Fatal编程技术网

Java 工厂模式及;构造函数中的可重写方法调用

Java 工厂模式及;构造函数中的可重写方法调用,java,factory-pattern,Java,Factory Pattern,我从这个链接中获取了factory模式示例: 然而,当我将代码复制到自己的IDE中时,我收到一条警告,说我的构造函数中有一个可重写的方法调用。我明白这意味着什么,我只是认为工厂模式应该解决这个问题?教程有缺陷吗?我应该做些不同的事情吗 为了节省粘贴的代码量,我只添加了一种车型: 类别汽车: package FactoryPattern; public abstract class Car { public Car(CarType model){ this.mode

我从这个链接中获取了factory模式示例:

然而,当我将代码复制到自己的IDE中时,我收到一条警告,说我的构造函数中有一个可重写的方法调用。我明白这意味着什么,我只是认为工厂模式应该解决这个问题?教程有缺陷吗?我应该做些不同的事情吗

为了节省粘贴的代码量,我只添加了一种车型:

类别
汽车

package FactoryPattern;

public abstract class Car {



    public Car(CarType model){
        this.model = model;
        arrangeParts();
    }

    private void arrangeParts(){
        //Do one time processing herer
    }

    //Do subclass level processing in this method

    protected abstract void construct();
    private CarType model = null;
    public CarType getModel(){
        return model;
    }

    public void setModel (CarType model){
        this.model = model;
    }


}
类别
汽车工厂

package FactoryPattern;


public class CarFactory {

    public static Car buildCar(CarType model){
        Car car = null;
        switch (model) {
            case SMALL:
                car = new SmallCar();
                break;
            case SEDAN:
                car = new SedanCar();
                break;
            case LUXURY:
                car = new LuxuryCar();
                break;
            default:
                //throw an exception
                break;        
        }
        return car;
    }    
}
类别
FactoryPattern

package FactoryPattern;

public enum CarType {

    SMALL, SEDAN, LUXURY

}

package FactoryPattern;

public class LuxuryCar extends Car {

    LuxuryCar(){
        super(CarType.LUXURY);
        construct();
    }

    @Override
    protected void construct(){
        System.out.println("Building Luxury Car");
    }

}
Class
CarFactoryTest

package FactoryPattern;

public class CarFactoryTest {
    public static void main(String[] args) {

        CarFactory.buildCar(CarType.SMALL);
        CarFactory.buildCar(CarType.SEDAN);
        CarFactory.buildCar(CarType.LUXURY);
    }

}

警告来自这里

LuxuryCar(){
    super(CarType.LUXURY);
    construct();
}
construct()
方法受
保护
,因此可以在
LuxuryCar
的子类中重写。如果一个不知情的开发人员重写该方法并尝试使用父类中尚未初始化的字段,那么很多事情都会变得糟糕。这就是IDE警告您的原因

比如说,

public class LuxuryCar extends Car {
    protected Televesion tv;

    LuxuryCar(){
        super(CarType.LUXURY);
        construct(); // will call the overriden method in the sub class
    }

    @Override
    protected void construct(){
        tv = new Television();
    }
}

public class BetterLuxuryCar extends LuxuryCar {
    BetterLuxuryCar(){
        super();
    }

    @Override
    protected void construct(){
        tv.upgrade(); // oops NullPointerException
    }
}

请注意,像
construct()
这样的方法似乎属于工厂本身。另外,
Car
类不需要
CarType
字段,子类型已经知道它是什么类型。

谢谢,我完全理解为什么会有人警告我使用可重写方法。我只是觉得正确实现的工厂方法不应该有这个警告,所以我想知道如何修改代码使其正确?我是否应该将“building Luxury Car”的println移到工厂中并替换LuxuryCar方法调用?@Killian是的,您通常会希望将做任何准备的方法移到工厂中,并将任何初始化移到构造函数或无法重写的方法中。老实说,请另找一个教程。这一个看起来像是由一个读书人写的,而不是程序员写的。打开枚举是一个糟糕的做法,可能导致脆弱的代码难以维护。倾向于使用枚举常量特定的方法。