简体   繁体   English

在父类中调用匿名内部类的方法

[英]Calling Methods of Anonymous Inner Class in the parent class

I got the below doubt when am surfing about Anonymous inner class 关于Anonymous内心阶级的冲浪,我对此产生了疑问

Here is the Original code I downloaded and was working around it (please refer to the below code only for my questions). 这是我下载的原始代码并正在解决它(请参阅以下代码仅用于我的问题)。

As per the link above they say we cannot overload & add additional methods in a Anonymous Inner class. 根据上面的链接,他们说我们不能在Anonymous Inner类中重载和添加其他方法。

But When I compile the below it was working fine though I was not able to call those public methods outside the Inner class. 但是当我编译下面的它工作正常,虽然我无法在Inner类之外调用那些公共方法。

Initially I was surprised why I could not access the public methods outside the Inner class but then I realized the Object is being held by "father" class reference which does not know such function call. 最初我很惊讶为什么我无法访问Inner类之外的公共方法,但后来我意识到Object被“父”类引用所持有,它不知道这样的函数调用。

What changes Can I make in the below code for making calls to the overloaded method and new Method outside the Inner class? 我可以在下面的代码中进行哪些更改,以调用Inner类之外的重载方法和新方法?

class TestAnonymous
{    

    public static void main(String[] args)
    {      
        final int d = 10;

        father f = new father(d);

        father fAnon = new father(d){                
        // override method in superclass father
        void method(int x){     
            System.out.println("Anonymous: " + x);
            method("Anonymous: " + x); //This Compiles and executes fine.
            newMethod(); //This Compiles and executes fine.
        }

        // overload method in superclass father
        public void method(String str) {
            System.out.println("Anonymous: " + str);
        }

        // adding a new method
        public void newMethod() {
            System.out.println("New method in Anonymous");
            someOtherMethod(); //This Compiles and executes too.
        }

        };

        //fAnon.method("New number");  // compile error
        //fAnon.newMethod();         // compile error - Cannot find Symbol

    }

    public static final void someOtherMethod()
    {
        System.out.println("This is in Some other Method.");
    }

} // end of ParentClass

class father
{   
    static int y;

    father(int x){ 
        y = x;
        this.method(y);
    }

    void method(int x){
        System.out.println("integer in inner class is: " +x);
    }     
}  

You cannot do this with an anonymous class; 你不能用匿名类做到这一点; it conflicts with Java's static type system. 它与Java的静态类型系统冲突。 Conceptually, the variable fAnon is of type father , which has no .method(String) or .newMethod methods. 从概念上讲,变量fAnon的类型为father ,它没有.method(String).newMethod方法。

What you want is an ordinary (named) subclass of father : 你想要的是father的普通(命名)子类:

class fatherSubclass extends father
{ /* ... */ }

and you should declare your new variable 你应该声明你的新变量

fatherSubclass fAnon = new fatherSubclass(d)

What changes Can I make in the below code for making calls to the overloaded method and new Method outside the Inner class? 我可以在下面的代码中进行哪些更改,以调用Inner类之外的重载方法和新方法?

Just make it not an anonymous class. 只是让它不是一个匿名的类。 You could declare the class within the method: 可以在方法中声明该类:

public static void main(String[] args) {
    class Foo extends Father {
        ...
    }
    Foo foo = new Foo();
    foo.method("Hello");
}

... but I'd probably recommend making it a separate class, either nested within the outer class if necessary, or just a new top-level class. ...但我可能会建议将它作为一个单独的类,或者在必要时嵌套在外部类中,或者只是一个新的顶级类。

As soon as you start wanting to do anything complicated with anonymous classes, it's generally better to break it into a fully-fledged named class. 一旦你开始想要做任何复杂的匿名类,通常最好将它分解为一个完全成熟的命名类。

You can not call the 'overloaded' and new method from outside the anonymous class. 您无法从匿名类外部调用“重载”和新方法。 You can call them inside your anonymous class, but never from outside. 您可以在匿名课程中调用它们,但绝不能从外部调用它们。 The outside world simply does not know of them. 外面的世界根本不知道它们。 There is no class or interface specification that has information about them. 没有关于它们的信息的类或接口规范。

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

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