Access methods within local inner classes in Java

久未见 提交于 2019-12-30 03:25:07

问题


Is there any way to access the methods of local inner classes in Java. Following code is the sample code that I tried before. According to that what is the mechanism to access the mInner() method?

class Outer{
    int a=100;

    Object mOuter(){
        class Inner{
            void mInner(){
                int y=200;
                System.out.println("mInner..");
                System.out.println("y : "+y);
            }
        }
        Inner iob=new Inner();  
        return iob;
    }
}   
class Demo{
    public static void main(String args[]){
        Outer t=new Outer();
        Object ob=t.mOuter();
        ob.mInner(); // ?need a solution..
    }
}

回答1:


As ILikeTau's comment says, you can't access a class that you define in a method. You could define it outside the method, but another possibility is to define an interface (or abstract class). Then the code would still be inside your method, and could access final variables and parameters defined in the method (which you couldn't do if you moved the whole class outside). Something like:

class Outer {
    int a = 100;

    public interface AnInterface {
        void mInner();  // automatically "public"
    } 

    AnInterface mOuter() {   // note that the return type is no longer Object
        class Inner implements AnInterface {
            @Override
            public void mInner() {    // must be public
                int y = 200;
                System.out.println("mInner..");
                System.out.println("y : " + y);
            }
        }
        Inner iob = new Inner();  
        return iob;
    }
}   

class Demo {
    public static void main(String[] args) {  // the preferred syntax
        Outer t = new Outer();
        Outer.AnInterface ob = t.mOuter();
        ob.mInner(); 
    }
}

Note: not tested

Note that the return type, and the type of ob, have been changed from Object. That's because in Java, if you declare something to be an Object, you can only access the methods defined for Object. The compiler has to know, at compile time (not at run time) that your object ob has an mInner method, and it can't tell that if the only thing it knows is that it's an Object. By changing it to AnInterface, the compiler now knows that it has an mInner() method.




回答2:


The scoping rules of a local class are pretty much the same as the scoping rules of a variable, that is, it is confined to the enclosing block.

The same way you cannot access variable iob from main, you cannot access local class Inner from main.

Outside the enclosing block, there's no difference between a local class and an anonymous class. Neither can be accessed. The difference is that within the enclosing block, the local class can be accessed by name, especially useful if you need to access it repeatedly, e.g. to create multiple instances.

The only way to interact with a local/anonymous class outside the enclosing block, is through any superclass or interface implemented by the class in question.




回答3:


To access the inner class create an object of inner class..

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

from your example

outer t=new outer();

outer.inner inner1=t.new inner();

Hope this helps you...



来源:https://stackoverflow.com/questions/32409324/access-methods-within-local-inner-classes-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!