在匿名类内部定义自定义函数/属性

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

Defining custom function/property inside anonymous class

问题

我想在匿名类中定义我的属性和函数,如下所示:

ExistingExtendableJavaClass aClass = new ExistingExtendableJavaClass() {
    public String someProperty;

    public String getMyProperty() { return someProperty; }
});

但是这些调用不起作用:

aClass.someProperty // 不可访问
aClass.getMyProperty() // 不可访问

我知道这是因为ExistingExtendableJavaClass没有这些属性和方法,但是我的匿名类有这些。我应该如何实现这个?

英文:

I want to define my property and function in anonymous class as under

ExistingExtendableJavaClass aClass = new ExistingExtendableJavaClass() {
         public String someProperty;

         public String getMyProperty() { return someProperty }
});

But then these calls don't work

aClass.someProperty // not accessible
aClass.getMyProperty() // not accessible

I know because ExistingExtendableJavaClass doesn't have these, but then my anonymous has these. How can I achieve this ?

答案1

得分: 5

以下是翻译好的部分:

它们可以很好地访问:

new ExistingExtendable() {
    public void foo() {}
}.foo();

运行得很好。

但如果你写成:

ExistingExtendable x = new ExistingExtendable() {
    public void foo() {}
};
x.foo();

那不起作用。出于同样的原因,这个也不起作用:

Object o = new String();
o.toLowerCase(); // 不行

问题在于你的匿名类没有名称,因此,你无法指定它的类型。我们可以通过将 Object o 替换为 String o 来修复字符串示例,但没有 String 的等价物。

然而,这就是匿名内部类的用途

如果你希望这些可以被标识,那么你不希望使用匿名内部类。问:“我想要一个匿名内部类,但我希望其中声明的新成员可以访问” 就像在问:“我想要一个圆,但是.. 带有角落”。

你可以创建方法局部内部类,现在你有了名称:

public void example(String x) {
    class IAmAMethodLocalClass extends ExistingExtendableJavaClass {
        String someProperty; // 使它们公开没有多大意义。

        String foo() {
            System.out.println(x); // 你可以在这里访问 x。
        }
    }

    IAmAMethodLocalClass hello = new IAmAMethodLocalClass();
    hello.someProperty = "It works!";
}

一个匿名内部类与这个方法局部类的东西是一样的,只是它避免了命名类型。在这种情况下,你 需要 那个名称,因此,你不能使用匿名内部类构造。

英文:

They are accessible just fine:

new ExistingExtendable() {
    public void foo() {}
}.foo();

works great.

But if you write:

ExistingExtendable x = new ExistingExtendable() {
    public void foo() {}
};
x.foo();

That does not work. For the same reason this doesn't work:

Object o = new String();
o.toLowerCase(); // nope

The problem is that your anonymous class has no name, thus, you cannot denote its type. We can fix the string example by replacing Object o with String o, but there is no String equivalent.

However, that is the point of an anonymous inner class.

If you want these to be denotable, then you don't want an anonymous inner class. Asking: "I want an anonymous inner class, but I want the new members I declared in them to be accessible" is like asking: "I want a circle, but.. with corners".

You can make method local inner classes, and now you have names:

public void example(String x) {
    class IAmAMethodLocalClass extends ExistingExtendableJavaClass {
        String someProperty; // making them public is quite useless.

        String foo() {
            System.out.println(x); // you can access x here.
        }
    }

    IAmAMethodLocalClass hello = new IAmAMethodLocalClass();
    hello.someProperty = "It works!";
}

an anonymous inner class is the same as this method local class thing, except it avoids naming the type. In this case, you NEED that name, thus, you can't use the anonymous inner class construct.

huangapple
  • 本文由 发表于 2020年7月28日 18:15:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63131814.html
匿名

发表评论

匿名网友

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

确定