Java枚举的缺失能力?

huangapple go评论61阅读模式
英文:

Missing capability of Java enums?

问题

以下示例看起来很酷,但是像这样调用枚举方法是行不通的。为什么?你能指引我到相关的参考文档或解决方法吗?

class Scratch {
    enum MyEnum {
        ONE{
            void test1 (String v1, String v2){}
        },
        TWO{
            void test2(int v3){}
        }
    }

    public static void main(String[] args) {
        MyEnum.ONE.test1("a","b");
        MyEnum.TWO.test2(0);
    }
}
英文:

The below example looks cool, but invoking enum methods like that does not work. Why? Can you point me to relevant reference docs, workarounds?

class Scratch {
	enum MyEnum {
		ONE{
			void test1 (String v1, String v2){}
		},
		TWO{
			void test2(int v3){}
		}
	}

	public static void main(String[] args) {
		MyEnum.ONE.test1("a","b");
		MyEnum.TWO.test2(0);
	}
}

答案1

得分: 3

这些方法是每个单独的枚举实例的方法,类似于在匿名类中声明的方法,就像匿名类一样,这些方法对于父类型的变量是不可见的。由于ONE和TWO是MyEnum枚举的实例,它们能够看到的方法仅限于在枚举本身中声明的方法,当然还包括从所有枚举的父类java.lang.Enum继承的方法。

要使方法可见,必须将其声明为枚举本身的方法:

class Scratch {
    enum MyEnum {
        ONE {
            public void test1(String v1, String v2) { }
        },
        TWO {
            public void test2(int v3) { }
        };

        public void test1(String v1, String v2) { }

        public void test2(int v3) { }
    }

    public static void main(String[] args) {
        MyEnum.ONE.test1("a", "b");
        MyEnum.ONE.test2(0);

        // 请记住,枚举变量可以重新赋值,因此所有可见的方法应该是MyEnum类型的方法
        MyEnum foo = MyEnum.ONE;
        foo = MyEnum.TWO;
    }
}

请记住,枚举变量可以重新赋值,任何枚举变量都应该能够潜在地调用相同的方法。

英文:

Those methods are methods of each individual enum instance, similar to a method declared within an anonymous class, and just like an anonymous class, the methods are not visible to variables of the parent type. Since ONE and TWO are instances of the MyEnum enum, the only methods visible to them are the ones declared in the enum itself, and also, of course, any inherited methods from the parent of all enums, java.lang.Enum.

To make the method visible, it must be declared as a method of the enum itself:

class Scratch {
    enum MyEnum {
        ONE {
            public void test1(String v1, String v2) { }
        },
        TWO {
            public void test2(int v3) { }
        };

        public void test1(String v1, String v2) { }

        public void test2(int v3) { }
    }

    public static void main(String[] args) {
        MyEnum.ONE.test1("a", "b");
        MyEnum.ONE.test2(0);

		// remember that an enum variable can be re-assigned, and so 
        // all visible methods should be methods of the MyEnum type
		MyEnum foo = MyEnum.ONE;
		foo = MyEnum.TWO;
    }
}

Remember that an enum variable can be re-assigned, and any enum variable should be able to potentially call the same methods.

huangapple
  • 本文由 发表于 2023年7月28日 01:19:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782109.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定