关于匿名内部阶级的冲浪,我对此产生了疑问
这是我下载的原始代码并正在解决它(请参阅下面的代码仅我的问题)。
根据上面的链接,他们说我们不能在Anonymous Inner类中重载和添加其他方法。
但是当我编译下面的内容时,虽然我无法在Inner类之外调用那些公共方法,但它工作正常。
最初我很惊讶为什么我无法访问Inner类之外的公共方法,但后来我意识到Object被“父”类引用所持有,它不知道这样的函数调用。
我可以在下面的代码中进行哪些更改,以调用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); } }I got the below doubt when am surfing about Anonymous inner class
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.
But When I compile the below it was working fine though I was not able to call those public methods outside the Inner class.
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.
What changes Can I make in the below code for making calls to the overloaded method and new Method outside the Inner class?
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)You cannot do this with an anonymous class; it conflicts with Java's static type system. Conceptually, the variable fAnon is of type father, which has no .method(String) or .newMethod methods.
What you want is an ordinary (named) subclass of father:
class fatherSubclass extends father { /* ... */ }and you should declare your new variable
fatherSubclass fAnon = new fatherSubclass(d)更多推荐
发布评论