java 将枚举返回到toString和访问器方法中

vmdwslir  于 2022-12-21  发布在  Java
关注(0)|答案(1)|浏览(134)

如下面的代码所示,我尝试让用户在枚举中输入速度(1,2,3),然后将输入返回到toString方法。

private enum speed
{
    SMALL(1), 
    MEDIUM(2), 
    FAST(3);
    
    private int speedValue;
    private speed (int speedValue)
    {
        this.speedValue = speedValue;
    }

    public int getSpeed()
    {
        return speedValue;
    }

    public static Optional<speed> get(int speedValue)
    {
        return Arrays.stream(speed.values()) 
        .filter(spe -> spe.speedValue == speedValue)
        .findFirst();
    }

}

private boolean on;

问题是当我把这个.speed = speed或任何其他东西,速度类将丢失与错误“速度无法解决或不是一个字段”
在toString类中也发生了同样的情况。

public Fan(speed seed, boolean on)
{
    speed.get() = seed; //what shall i put here
    this.on = on;
}

public boolean getOn()
{
    return this.on;
}

public String toString()
{
    return speed; //what shall i put here
}

public static void main(String[] args) {
    
    Scanner sc = new Scanner(System.in);

    System.out.println("Please enter speed");
    int sp = sc.nextInt();
    System.out.println("On/Off");
    boolean on = sc.nextBoolean();

    Optional<speed>spe = speed.get(sp); //getting enum integer values
    System.out.println(spe.get());

    Fan fan = new Fan(sp, on)

有什么解决方案可以让我把enum的整数值返回到public类和toString类中吗?

wtzytmuj

wtzytmuj1#

使用private enum speed {...}时,您声明枚举类型speed,但从未声明此类型的字段。
如果你想拥有一个名为speed的枚举类型的字段,你必须用private speed speed;声明它。
这看起来很混乱,因此我建议您遵循Java命名约定,其中类名以大写字母开头(枚举类型是类)。
这意味着您的枚举类型应该写成

public enum Speed {
        SMALL(1), 
        MEDIUM(2), 
        FAST(3);
    
        private int speedValue;
        private Speed (int speedValue) {
            this.speedValue = speedValue;
        }

        public int getSpeed() {
            return speedValue;
        }

        public static Optional<Speed> get(int speedValue) {
            return Arrays.stream(Speed.values()) 
            .filter(spe -> spe.speedValue == speedValue)
            .findFirst();
        }
    }

您的Fan类需要以下字段:

private boolean on;
    private Speed speed;

构造函数为

public Fan(Speed seed, boolean on) {
        speed = seed;
        this.on = on;
    }

或者,假设参数名称seed是拼写错误,应该改为speed

public Fan(Speed speed, boolean on) {
        this.speed = speed;
        this.on = on;
    }

其他方法:

public boolean getOn() {
        return this.on;
    }

    public String toString() {
        return speed.toString();
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("Please enter speed");
        int sp = sc.nextInt();
        System.out.println("On/Off");
        boolean on = sc.nextBoolean();

        Optional<Speed> spe = Speed.get(sp); //getting enum integer values
        System.out.println(spe.get());

        Fan fan = new Fan(spe.get(), on);
        // note that the above line produces not output. Why should it?
        // if you want to see the result of f.toString() you need to print it out:
        System.out.println(fan.toString());
        // or shorter (since println() calls toString() automatically):
        System.out.println(fan);
    }

注1:我还更改了左大括号({)的位置,以遵循一般的Java约定-对于经验丰富的Java程序员来说,这看起来不那么令人惊讶。
注2:正如Mark Rotteveel正确指出的:Fan类有一个public构造函数,因此Speed枚举也应该声明为公共的。否则Fan类之外的任何人都不能构造一个新的Fan对象。

相关问题