如何在静态NestedClass中使用非静态的OuterClass变量?

如何在静态NestedClass中使用非静态的OuterClass变量?

问题描述:

我知道静态类我使用静态变量,但如果我想要我在静态NestedClass中使用非静态OuterClass变量。有另一种出路吗?我怎样才能做到这一点?或者我可以这样做吗?如何在静态NestedClass中使用非静态的OuterClass变量?

public class NestedClass05 { 
    int num=1; 

    public static class Test1{ 
     int num=2; 

     public int Method1(int a){     
      return NestedClass05.num+a; // How I can use that 
     } 
    } 

    public class Test2{ 
     int num=3; 

     public int Method1(int a){ 
      return NestedClass05.this.num+a; // I use like this. 
     } 
    } 
} 
+1

你的问题体现了矛盾的条款。 – EJP

+0

请不要污蔑你的问题 –

您需要为静态类指定要从中检索变量的外部类的实例。像:

public class NestedClass05 { 
    int num=1; 

    public static class Test1{ 
      int num=2; 

      public int Method1(NestedClass05 instance, int a){ 
       return instance.num + a; 
      } 
     } 
    }