java java内部类方法访问

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15561368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:03:54  来源:igfitidea点击:

java inner classes method access

java

提问by user1917209

I have an outer class. I also have a private inner class that extends JPanel. This is the design of the code.

我有一个外部课程。我还有一个扩展 JPanel 的私有内部类。这是代码的设计。

public class Outer{
    private class Inner extends JPanel{
        public void doSomeWork(){}
    }

    public Outer(){
        Inner inner = new Inner();
        inner.doSomeWork();
    }

    public static void main(String args[]){
        Outer outer = new Outer();
    }
}

I am not being able to access the doSomeWork() method of the inner class from the outer class. Please help.

我无法从外部类访问内部类的 doSomeWork() 方法。请帮忙。

回答by chuthan20

Here is how you make an object of inner and access its variables...

这是创建内部对象并访问其变量的方法...

Outer outer = new Outer(); 
Outer.Inner inner = outer.new Inner(); 
inner.doSomeWork();

Example code from Oracle is here...

来自 Oracle 的示例代码在这里...