【问题标题】:Java: Modifying variables without argumentsJava:修改不带参数的变量
【发布时间】:2011-11-08 08:47:56
【问题描述】:

我刚刚开始学习 Java,我对学习 Java 的作用域规则感到非常沮丧。你看我希望创建一个不使用参数/参数的方法。

在 JavaScript 中,我可以使用函数轻松地做到这一点:

/** Function to increase 2 vars.
/** **/
function modifyNow(){
 a++;
 b++;
} // END

var a = 5;
var b = 10;

modifyNow();
// By this part, a and b would be 6 and 11, respectively.

现在这节省了我很多时间,而且很简单,因为每当调用函数时,var a 和 b 就已经存在了。

那么,有没有一种方法可以在 Java 中做到这一点,而无需像我在 JavaScript 中那样做参数?还是有其他方法可以解决这个问题?

谢谢您,感谢您的帮助... ^^

【问题讨论】:

  • Java 没有这样的全局作用域,无论哪种方式编写可维护的代码都是一种糟糕的做法。
  • Javascript 不是 Java,虽然等价物基本上确实存在(方法范围内的静态变量),但至少在 java 中它的编码风格确实很糟糕,所以最好学习 java 习语。从长远来看,会产生更好的代码。

标签: java class methods scope call


【解决方案1】:

ab 作为类中的私有 变量并在modifyNow() 中递增它们有什么问题?顺便说一句,Java 中的所有内容都必须属于 。你不能让全局代码乱七八糟...

【讨论】:

    【解决方案2】:

    您可以对类字段执行此操作,而不是对局部变量。

    class Clazz {
    
        int a = 5;
        int b = 10;
    
        public void modifyNow() {
            a++;
            b++;
        }
    }
    
    // ...
    
    Clazz c = new Clazz();
    c.modifyNow();
    

    现在,每次调用 modifyNow 后,这些字段都会更新。

    【讨论】:

      【解决方案3】:

      全局变量不好。在不知道您的意图的情况下,我会说您需要将变量作为成员添加到您的类中。然后可以从任何成员函数访问它们。

      【讨论】:

      • Java 中没有全局变量这种东西。全局变量还不错,它们有很多用途,但你需要小心。有些语言只有全局变量!
      【解决方案4】:

      你看我希望创建一个方法而不使用 参数/参数。

      最好的答案是:不要!

      如果您仍然想要这个,请查找 public static 变量。

      【讨论】:

        【解决方案5】:
        class Foo {
        
          int a = 5;
          int b = 10;
        
          public void modifyNow(){
            a++;
            b++;
          }
        
        }
        

        【讨论】:

          【解决方案6】:

          您需要使用所谓的member 变量,它们与您班级的instance 相关联。

          public class MyClass
          { 
          
            // these are Class variables, they are tied to the Class
            // and shared by all instances of the class.
            // They are referenced like MyClass.X
            // By convention all static variables are all UPPER_CASE
            private static int X;
            private static int Y
          
            // these are instance variables that are tied to 
            // instances of the class
            private int a;
            private int b;
          
            /** this the default no arg constructor */
            public MyClass() { this.a = 5; this.b = 10; }
          
            /** this is a Constructor that lets you set the starting values
                for each instance */
            public MyClass(final int a, final int b) { this.a = a; this.b = b; }
          
            public modifyNow() { this.a++; this.b++; }
          
            /** this is an accessor to retrieve the value of a */
            public int getA() { return this.a; }
          
            public int getB() { return this.b; }
           }
          
           final MyClass myInstanceA = new MyClass();
           myInstance.modifyNow();
           // a = 6, b = 11
          
           final MyClass myInstanceB = new MyClass(1, 2);
           myInstance.modifyNow();
           // a = 2, b = 3
          

          每次您执行new MyClass() 时,您都会得到一个与MyClass 不同的新instance,它与其他instance 不同。

          在您学习 JavaScript 时,无论如何都与 Java 无关。他们出于营销原因选择了这个名字,这是一个可怕的讽刺。

          【讨论】:

          • 谢谢!! ^^啊!!这就是你使用“构造函数”的方式..!啊哈哈,你不仅回答了我的问题,还回答了另一个问题……非常感谢非常感谢……^^
          猜你喜欢
          • 2020-11-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-13
          • 2012-09-13
          • 2016-05-24
          • 2015-04-07
          • 2016-04-17
          相关资源
          最近更新 更多