enum+ polymorphic, I did not say wrong, but Enum is not can be inherited, also can not inherit from others, just can realize interface, what talk about polymorphism?
But let's look at the phenomenon first:
Java code
public enum Fruit {APPLE, PEAR, PEACH, ORANGE; }
The above is a simple enum, about which I would like to add:
fruit is java.lang.Enum.sub-class, exactly, is a subclass of Enum<fruit>, where an inheritance is present, but this inheritance is what the compiler does for us, and we can't do it explicitly. If you don't believe us, we can try to point to an apple with a enum<fruit> reference, which is sure to be fine, and I won't try anymore.
to illustrate this more intuitively, let's look at Fruit's anti-compilation results:
Java code
-
Span style= "color:black;" >
Package test; public final class fruit extends enum { private fruit (String s, Int i) { Super (s, i); } public static fruit[] values () { Fruit afruit[]; int i; fruit afruit1[]; system.arraycopy (afruit = enum$values, 0, Afruit1 = new fruit[i = afruit.length], 0, i); return afruit1; } Public static fruit valueof (string s) { return (Fruit) enum.valueof (test/fruit, s); } public static final Fruit APPLE; public static final Fruit PEAR; public static final Fruit PEACH; public static final Fruit ORANGE; private static final fruit enum$values[]; static { Apple = new fruiT ("APPLE", 0); pear = new fruit ("PEAR", 1); peach = new fruit ( "PEACH", 2); orange = new fruit ("ORANGE", 3); enum$values = (new fruit[] { apple, pear, peach, orange }); } }
Note these lines:
Java code
public static final Fruit APPLE; public static final Fruit PEAR; public static final Fruit PEACH; public static final Fruit ORANGE;
Now you can't continue to derive subclasses from fruit, then what's polymorphic?
or write some more code: &NBSP;
Java code
public enum fruit { apple { public void test () { system.out.println ("I am an apple."); } }, PEAR { public void test () { system.out.println ("i am a pear."); } }, PEACH { public void test () { &nbsP; system.out.println ("I am a peach."); } }, ORANGE; public void Test () { system.out.println ("I am a fruit. "); } }
Of these, only orange does not have the Overide test () method;
We call them in the main function:
Java code
public static void Main (string[] args) {Fruit.APPLE.test (); Fruit.PEAR.test (); Fruit.PEACH.test (); Fruit.ORANGE.test (); }
Output Result:
Reference
I am an apple.
I am a pear.
I am a peach.
I am a fruit.
as you can see, the Apple,pear,peach that redefined the test method overrides the default behavior inherited from the parent class, but Orange, which did not redefine the test method, inherits the behavior of the parent class, and polymorphism is shown here.
so we have just seen Fruit's anti-compilation results, no new classes inherit from fruit, then where do these polymorphic behavior come out? Say it is "polymorphic" is it accurate?
in fact, the fruit class at this time has undergone a subtle change, everything is related to the implementation of the JDK enum, we can now go to the compilation results directory below to see:
How many other seemingly internal classes of class files besides Fruit.class?? Maybe we can get a clue here, but at this point we're going to look at the anti-compilation results and see what the hell it's doing:
Java code
Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov. // Jad home page: http://www.geocities.com/kpdus/jad.html // Decompiler options: packimports (3) // Source File Name: Fruit.java package test; import java.io.printstream; public class fruit extends enum { private fruit (string s, int i) { super (s, i); } public void test () { System.out.println ("I am a fruit."); } public static fruit[ ] values () { Fruit afruit[]; int i; Fruit afruit1[]; system.arraycopy (afruit = enum$values, 0, afruit1 = new fruit[i = afruit.length], 0, i); return afruit1; } public static fruit valueof (string s) { return (Fruit) enum.valueof (test/fruit, s ); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;} fruit (String s, int i, fruit fruit) { this (s, i); } public static final fruit apple; public static final fruit PEAR; public static final Fruit PEACH; public static final Fruit ORANGE; private static final Fruit ENUM$VALUES[]; static { apple = new fruit ("APPLE", 0) { &nbSp; public void test () { system.out.println ("i am an apple."); } }; Pear = new fruit ("PEAR", 1) { public void test () { system.out.println ("i am a pear."); } }; peach = new fruit ("PEACH", 2) { public void test () { System.out.println ("I am a peach."); } }; Orange = new fruit ("ORANGE", 3); ENUM$VALUES = (NEW&NBSP;FRUIT[]&NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp; apple, pear, peach, orange }); } }
Note This code:
Java code
static { Apple = new fruit ("APPLE", 0) { public void test () { system.out.println ("i am an apple."); } }; Pear = new fruit ("PEAR", 1) { public void test () { system.out.println ("i am a pear."); } }; Peach = new fruit ("PEACH", 2) { public void test () { system.out.println ("I am a peach."); } }; orange = new fruit ("ORANGE", 3);
This time Apple,pear,peach has been in the anonymous inner class of the way to fruit overide, the natural manifestation of polymorphism, the more than the three of the suspected inner class file is them! Orange, however, does not rewrite the test method and still appears as a fruit instance.
Why is it that the enum has so much to do with it, then let's consider how valuable it is?
More application possibilities or limitations need to be explored in practical applications.
A brief talk on the polymorphism of Java enum