繁体   English   中英

如何从不同类的主方法访问静态内部类的成员

[英]how to access members of a static inner class from main method of different class

堆栈溢出只是想让我在这里写更多。

Class A{
    static Class B{
         int i,j;
   }
    B method(int x){
        // how to return object of type B
    }
}

Class Main(){
   // how do i call method B here
}

如果MainA在同一个包中,这应该已经按如下方式工作:

A.java:

class A {
    static Class B{
        int i,j;
    }
    B method(int x){
        // how to return object of type B
        return new B();
    }
}

主.java:

public class Main {
   public static void main(String[] args) {
       // how do i call method B here
       A a = new A();
       A.B b = a.method();
    }
}

如果它们在单独的包中,您需要将类 A 和 B 声明为 public。

暂无
暂无

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

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