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

Java 父类中匿名内部类的调用方法,java,overloading,anonymous-class,Java,Overloading,Anonymous Class,当我浏览匿名的内部班级时,我得到了以下疑问 这是我下载的代码,我正在解决它(请参考下面的代码,只为我的问题) 根据上面的链接,他们说我们不能重载&在匿名内部类中添加额外的方法 但是当我编译下面的代码时,它工作得很好,尽管我无法在内部类之外调用这些公共方法 起初我很惊讶为什么我不能访问内部类之外的公共方法,但后来我意识到对象被“父”类引用持有,而父类引用不知道这样的函数调用 对于调用重载方法和内部类外的新方法,我可以在下面的代码中做哪些更改 class TestAnonymous {

当我浏览匿名的内部班级时,我得到了以下疑问

这是我下载的代码,我正在解决它(请参考下面的代码,只为我的问题)

根据上面的链接,他们说我们不能重载&在匿名内部类中添加额外的方法

但是当我编译下面的代码时,它工作得很好,尽管我无法在内部类之外调用这些公共方法

起初我很惊讶为什么我不能访问内部类之外的公共方法,但后来我意识到对象被“父”类引用持有,而父类引用不知道这样的函数调用

对于调用重载方法和内部类外的新方法,我可以在下面的代码中做哪些更改

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);
    }     
}  

您不能对匿名类执行此操作;它与Java的静态类型系统冲突从概念上讲,变量
fAnon
属于
father
类型,它没有
.method(String)
.newMethod
方法。

您需要的是
father
的普通(命名)子类:

class fatherSubclass extends father
{ /* ... */ }
你应该声明你的新变量

fatherSubclass fAnon = new fatherSubclass(d)
对于调用重载方法和内部类外的新方法,我可以在下面的代码中做哪些更改

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);
    }     
}  
不要让它成为匿名类。您可以在方法中声明类:

public static void main(String[] args) {
    class Foo extends Father {
        ...
    }
    Foo foo = new Foo();
    foo.method("Hello");
}
。。。但我可能会建议将其作为一个单独的类,必要时嵌套在外部类中,或者只是一个新的顶级类


一旦您开始想要对匿名类执行任何复杂的操作,通常最好将其分解为一个完全成熟的命名类。

您不能从匿名类之外调用“重载”和新方法。您可以在匿名类内调用它们,但不能从外部调用。外界根本不知道他们。没有任何类或接口规范包含关于它们的信息。

Ya我在发布此问题时就知道这种行为。@Sudhaker:那为什么要问这个问题?我想从外部给他们打电话,所以要求解决问题,Snail和Jon的回答正是我所期望的。。。