【问题标题】:How to Access private parameterized method of private inner class with in a static class in JAVA如何在JAVA的静态类中访问私有内部类的私有参数化方法
【发布时间】:2015-11-10 12:58:34
【问题描述】:

如何从 main 方法访问 Inner 类的 getMeSomething 方法?我所需要的只是能够发送一个整数并在 main 方法中获取返回值。

public class OuterClass {
   public static void main(String[] args) {
    //Using Java Reflection...I tried to create an inner class Object
    //created a outer class object
    //and invoke the method without any success
   }

  static class Inner {
     private class Private {
        private int getMeSomething(int num) {
            return (num*2);
        }
     }
  }//end of inner class
} //end of outer class

【问题讨论】:

  • 好吧,你需要一个 Inner.Private 的实例,因为它是一个实例方法......
  • 或将PrivatePrivate.getMeSomething() 设为静态。

标签: java reflection inner-classes


【解决方案1】:

您需要 Inner 和 Private 的实例:

      public static void main(String[] args) {
        //Using Java Reflection...I tried to create an inner class Object
        //created a outer class object
        //and invoke the method without any success
               Private p = new Inner ().new Private ();
           System.out.println(""+p.getMeSomething(21));
       }

【讨论】:

  • 您的答案有效...但是...我的要求还需要创建一个 Object o 并用于保存类 OuterClass.Inner 实例的引用。私人的。有小费吗??对不起,不清楚的问题。
猜你喜欢
  • 2022-11-20
  • 1970-01-01
  • 2014-08-31
  • 1970-01-01
  • 2013-03-28
  • 1970-01-01
  • 2017-05-12
  • 1970-01-01
  • 2011-02-17
相关资源
最近更新 更多