简体   繁体   English

使用额外的接口实现在Java中实例化匿名内部类

[英]Instantiating anonymous inner classes in Java with additional interface implementation

Let's say I have the following two class/interface definitions: 假设我有以下两个类/接口定义:

public abstract class FooClass {
    public abstract void doFoo();
}

and

public interface BarInterface {
    public void doBar();
}

If I want to make an anonymous inner class that extends/implements both, do I need to do this: 如果我想创建一个扩展/实现两者的匿名内部类,我是否需要这样做:

public abstract class BothClass extends FooClass implements BarInterface {}

...

new BothClass() {
    public void doFoo() {
        System.out.println("Fooooooooo!!!!");
    }

    public void doBar() {
        System.out.println("Baaaaaaaar!!!!");
    }
}.doBar();

Or is there a short-cut that allows me to not define BothClass ? 或者是否有一个捷径允许我不定义BothClass Something like this, maybe: 这样的事情,也许:

new (FooClass implements BarInterface)() {
    public void doFoo() {
        System.out.println("Fooooooooo!!!!");
    }

    public void doBar() {
        System.out.println("Baaaaaaaar!!!!");
    }
}.doBar();

(This idea gives me several errors, none of which are helpful here) (这个想法给了我几个错误,这些都没有帮助)

Let's go to the JLS: 我们去JLS吧:

An anonymous class declaration is automatically derived from a class instance creation expression by the Java compiler. Java编译器自动从类实例创建表达式派生匿名类声明。

where a class instance creation expression is 类实例创建表达式的位置

ClassInstanceCreationExpression:
    new TypeArgumentsopt TypeDeclSpecifier TypeArgumentsOrDiamondopt
                                                            ( ArgumentListopt ) ClassBodyopt
    Primary . new TypeArgumentsopt Identifier TypeArgumentsOrDiamondopt
                                                            ( ArgumentListopt ) ClassBodyopt

TypeArgumentsOrDiamond:
    TypeArguments
    <> 

ArgumentList:
    Expression
    ArgumentList , Expression

So, no, the Java language specification does not allow any shortcuts for making your anonymous class implement more interfaces than the type you're sub-typing. 所以,不,Java语言规范不允许任何快捷方式使您的匿名类实现比您要子类型的类型更多的接口。

So, to determine the type of the anonymous class 因此,要确定匿名类的类型

If the class instance creation expression ends in a class body, then the class being instantiated is an anonymous class. 如果类实例创建表达式在类主体中结束,则实例化的类是匿名类。 Then: 然后:

  • If T denotes an interface, then an anonymous direct subclass of Object that implements the interface named by T is declared. 如果T表示接口,则声明实现由T命名的接口的Object的匿名直接子类。

[...] [...]

  • Let T be the type named by the Identifier and any type arguments. 设T是标识符和任何类型参数命名的类型。 An anonymous direct subclass of the class named by T is declared. 声明了由T命名的类的匿名直接子类。 The body of the subclass is the ClassBody given in the class instance creation expression. 子类的主体是类实例创建表达式中给出的ClassBody。

Your alternative is the way to do it. 您可以选择其他方法。

You can also use local classes. 您也可以使用本地课程。

An anonymous class 一个匿名的课程

FooClass f = new FooClass() {
  public void doFoo() {}
};

is just a convenient shorthand for a local class definition with a generated name 对于具有生成名称的本地类定义,它只是一种方便的简写

class $anon extends FooClass {
  public void doFoo() {}
}
FooClass f = new $anon();

If you want to implement interfaces just write the local class definition explicitly 如果要实现接口,只需显式编写本地类定义

class LocalFoo extends FooClass implements BarInterface {
  // method declarations here
}
LocalFoo lf = new LocalFoo();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM