问题
In there an internal issue why java anonymous classes cannot implement and subclass at the same time? Or is it just because the syntax?
回答1:
In there an internal issue why java anonymous classes cannot implement and subclass at the same time?
I believe it is 99% due to syntactical reasons. Type parameters even support intersection types (<T extends InterfaceX & InterfaceY>
) so I don't think such feature would introduce any contradictions or complications.
An expression like new (InterfaceX & InterfaceY)() { ... }
could for instance be compiled into something like
interface InterfaceXandY extends InterfaceX, InterfaceY {}
... new InterfaceXandY() { ... }
The reason no such feature has been added is most likely because it's a rare use case for which there is a simple workaround.
On a somewhat related note. You can let a lambda implement for instance Serializable
by doing
Runnable r = (Runnable & Serializable)() -> System.out.println("Serializable!");
See How to serialize a lambda?
回答2:
solely syntax, so called anonymous classes are 100% normal classes. you can possibly achieve a funky results by using java.lang.reflect.Proxy and InvocationHandler, would be the dirtiest way to do it.
Simpler ways include declaring the class in the method and just adding 'implements',
回答3:
Anonymous classes are provided as a way to quickly and succinctly build a "one off" class. Your question suggests that you are attempting to use your anon class in multiple ways (at least one interface, and one extends). In this case, it would be more readable and more maintainable if you promote that anon class to a full class.
This would also stave off unexpected side effects by better managing your classes' encapsulation.
回答4:
Found a hack around this
create a interface at the bottom of your file witch extends all the interfaces you want to implements then create an anonymous class of which implements this.
回答5:
According to James Gossling:
Closures were left out of Java initially more because of time pressures than anything else. In the early days of Java the lack of closures was pretty painful, and so inner classes were born: an uncomfortable compromise that attempted to avoid a number of hard issues. But as is normal in so many design issues, the simplifications didn't really solve any problems, they just moved them.
Interesting, now that lambda expressions and closures are being added to Java, and this time instrumented with something different than inner classes, we could define an object on the fly composed by more than one interface, provided that the second one is marker interface.
interface Bar {
default String getName() { return "Bar"; }
}
Object o = (Comparable<Integer> & Bar) (x,y) -> x > y ? x : y;
Bar b = (Bar) o;
System.out.println(b.getName()); //Bar
This compiles and runs perfectly in my latest build of the JDK 8 (March 12, 2013). So, it would look like they have been giving more thought this time to this problem.
来源:https://stackoverflow.com/questions/4774168/why-an-anonymous-class-cant-implement-multiple-interfaces-directly-simply-beca