如何将枚举参数传递给构造函数?

问题描述 投票:0回答:1

我有WingNut类,它继承了InnerThreaded,后者继承了Fastener。当我实例化WingNut时,它看起来像这样

WingNut toAdd = new WingNut(Threads.T1_4_20, Materials.ThreadedMaterials.Stainless_Steel,
                        Finishes.WingNutFinish.Plain, 0.5528, 1);// thread,  material, finish, price/unit, unit size

线程,材料,表面处理分别在不同的类中。

完成

public final class Finishes {

    public enum BoltFinish {Chrome, Hot_Dipped_Galvanized, Plain, Yellow_Zinc, Zinc};

    public enum WingNutFinish {Chrome, Hot_Dipped_Galvanized, Plain, Yellow_Zinc, Zinc};

    public enum CommonNailFinish {Bright, Hot_Dipped_Galvanized};

    public enum ScrewFinish {Crhome, Hot_Dipped_Galvanized, Plain, Yellow_zinc, Zinc, Black_Phosphate, ACQ_1000_Hour, Lubricated};

}

线程

public final class Threads {

    private String thread;

    private Threads(String thread) {this.thread = thread; }

    public static final Threads T8_13 = new Threads("#8-13");
    public static final Threads T8_15 = new Threads("#8-15"); 
    public static final Threads T8_32 = new Threads ("#8-32"); 
    public static final Threads T10_13 = new Threads("#10-13"); 
    public static final Threads T10_24 = new Threads("#10-24"); 
    public static final Threads T10_32 = new Threads("#10-32"); 
    public static final Threads T1_4_20 = new Threads("1/4-20"); 
    public static final Threads T5_16_18 = new Threads("5/16-18"); 
    public static final Threads T3_8_16 = new Threads("3/8-16"); 
    public static final Threads T7_16_14 = new Threads("7/16-14");
    public static final Threads T1_2_13 = new Threads("1/2-13"); 
    public static final Threads T5_8_11 = new Threads("5/8-11"); 
    public static final Threads T3_4_10 = new Threads("3/4-10");

    public String toString() { return thread; }
}

材料

public final class Materials {

    public enum ThreadedMaterials {Brass, Stainless_Steel, Steel};

    public enum NailMaterials {Steel};

}

如何将参数传递给WingNut中的构造函数,然后传递给InnerThreaded,然后传递给Fastener?我尝试了Eclipse建议的构造函数,但是问题是我有InnerThreaded和Fastener的Enum参数。

public WingNut(Threads t, ThreadedMaterials m, WingNutFinish f, double d, int i) {
        // TODO Auto-generated constructor stub
    }

紧固件

public abstract class Fastener {

    private Enum<?> material;
    private Enum<?> finish;
    private double unitPrice;
    private int numPerUnit;

    public Fastener(Enum<?> material, Enum<?> finish, double unitPrice, int numPerUnit) {
        this.material = material;
        this.finish = finish;
        this.unitPrice = unitPrice;
        this.numPerUnit = numPerUnit;
    }

    public Fastener(Enum<?> finish, double unitPrice, int numPerUnit) {
        this.finish = finish;
        this.unitPrice = unitPrice;
        this.numPerUnit = numPerUnit;
    }

}

InnerThreaded

  public class InnerThreaded extends Fastener{

    private Enum<?> t;

    public InnerThreaded(Enum<?> thread, Enum<?> material, Enum<?> finish, double unitPrice, int numPerUnit) {
        super(material, finish, unitPrice, numPerUnit);
        t = thread;
        // TODO Auto-generated constructor stub
    }

}

一般结构对我来说很有意义,但是我不确定是否存在将参数传递给每个构造函数的语法错误。

java enums
1个回答
0
投票

将枚举实例从WingNut的顶级构造函数传递给祖先的构造函数没有问题,这可以编译并运行良好。但是,您首先需要在InnerThreaded祖先中修复此错误:它期望thread参数(和t字段)为枚举类型,但是Threads实际上是代码中的普通类。因此,请相应地更改InnerThreaded定义,或者也将Threads设为枚举。

© www.soinside.com 2019 - 2024. All rights reserved.