简体   繁体   English

如何通过方法更改类内部的静态变量

[英]How to change static variable inside in a class by a method

I have three classes: 我有三节课:

Class One 一等

public class One {
   private static Two object;

   public static void set_up(Two object) {
       int y = object.get();
       System.out.println(y);
   }

   public static void prn () {
       System.out.println(object.get());
   }

}  

Class Two 第二课

public class Two {
   private int x;


   public int get() {
       return x;
   }

   Two(int n){
       x = n;
   }
 }

Class Three 第三课

public class Three {
   public static void main( String[] argv ) {
       One st = new One();
       Two two = new Two(2);

       st.set_up(two);

       st.prn();
   }
}

I want to change the static variable object in class Two by method set_up(Two object) . 我想通过method set_up(Two object)Two的静态变量object The problem is that static variable inside the class has the same name as the arguments in the method. 问题在于类中的静态变量与方法中的参数具有相同的名称。 How can I modify set_up(Two object) so I copy values from given argument to static object? 如何修改set_up(Two object)以便将值从给定参数复制到静态对象?

You can qualify it by using the class' name: 您可以使用班级名称对其进行限定:

public static void set_up(Two object) {
    One.object = object;
}

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

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