How to call an additional method in transfers?

enum Enum1 { BIG(8), HUGE(10) { public String getName() { return "Huge"; } public String getContry() { return "India"; }//additional Method }, OVERWHELMING(16) { public String getName() { return "OVERWHELMING"; } }; private int ounces; public int getOunes() { return ounces; } public String getName() { return "Ponds"; } Enum1(int ounces1) { ounces = ounces1; } } class EnumAsInnerClass { Enum1 enumInnerClass; public static void main(String[] args) { EnumAsInnerClass big = new EnumAsInnerClass(); big.enumInnerClass = Enum1.BIG; EnumAsInnerClass over = new EnumAsInnerClass(); over.enumInnerClass = Enum1.OVERWHELMING; EnumAsInnerClass huge = new EnumAsInnerClass(); huge.enumInnerClass = Enum1.HUGE; System.out.println(big.enumInnerClass.getName());//Ponds System.out.println(over.enumInnerClass.getName());//OVERWHELMING System.out.println(huge.enumInnerClass.getName());//Huge } } 

Consider the above example. How can I call the getCountry method for HUGE?

If there is no way to call this method, why does Java treat it as legal?

+6
source share
4 answers

(As noted almost 4 years later, my initial answer was incorrect.)

You can't even call Enum1.HUGE.getCountry() with a specific enumeration, which is very little surprising ... but even if you could, you could not do it with an arbitrary value of Enum1 , which would be more general useful.

The solution is to stop it from an additional method - add the getCountry() method to Enum1 , either returning the default value, or possibly throwing an exception. HUGE can then override the method.

It would be nice if you could declare that a certain enumeration value implemented the interface, but there seems to be no syntax for it.

+9
source

enum is a type definition similar to a class, so Enum1 does not have a getCountry() method available in its interface.

edit: Alternative solution

What you can do is define the necessary methods in Enum1 and override them in separate enumeration values.

+3
source

If there is no way to call this method, then why does java process as legal?

You can still call it in determining the value of the enumeration. For example, change the getName() function:

 // ... HUGE(10) { public String getName() { // Call additional Method return "Huge" + getCountry(); } public String getContry() { return "India"; } // additional Method }, // ... 

Otherwise, you need to override it in order to be able to call it.

+2
source

No wonder.

When you define an enum type (in your case Enum1 ) and declare values ​​for it, their reference type will have a specific type ( Enum1 ). When you define methods in your enum declarations, you have just created anonymous subclasses .

 public enum Enum1 { OVERWHELMING(16) { public String getName() { return "OVERWHELMING"; } }; // And the remaining constructors, fields and methods } 

And you cannot just properly invoke methods of an anonymous class outside the definition itself. Similarly, someOtherMethod() also not be accessible externally:

 ActionListener al = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { someOtherMethod(); } public void someOtherMethod() { System.out.println("Some other method!"); } }; 

Such methods, however, are available from the declaration itself, for example as indicated in this answer .

Note: As Joachim Sauer in his comment , this may not be the best design. You must, for example, port your methods to type Enum1 .


There is another way. Access to the method is possible through reflection:

 Class<?> clazz = Enum1.HUGE.getClass(); Method method = clazz.getMethod("getCountry"); System.out.println(method.invoke(Enum1.HUGE)); // Prints "India" 
0
source

Source: https://habr.com/ru/post/887790/


All Articles