【问题标题】:how to access members of a static inner class from main method of different class如何从不同类的主要方法访问静态内部类的成员
【发布时间】:2015-06-28 20:17:56
【问题描述】:

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

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
}

【问题讨论】:

  • "堆栈溢出只是想让我在这里写更多。"这是有原因的。您没有解释是什么阻止您编写自己的代码。你有遇到什么问题吗?也就是说你需要描述what have you tried already
  • 没有静态内部类。

标签: java static inner-classes


【解决方案1】:

如果 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();
    }
}

Main.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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-13
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    • 2013-03-28
    • 1970-01-01
    • 2015-01-30
    相关资源
    最近更新 更多