【问题标题】:What's the difference between a class variable and a parameter in a constructor?构造函数中的类变量和参数有什么区别?
【发布时间】:2013-11-04 07:13:28
【问题描述】:

我问这个是为了回复this answer

我正在使用VariableVar 的示例- 编辑 请注意我在问在哪里使用VarVariable

Class NewClass {

    private  String Variable = "";

    Public Class (String Var)
    {
        NewClass.Var = Variable;
    }
}

    private  String Variable = "";

    Public Class (String Variable)
    {
        NewClass.Variable = Var; // OR WHATEVER OTHER COMBINATIONS IT MAY BE.
    }
}

哪些是类变量,这与参数有何不同,哪些去哪里?

编辑

我应该补充一下,因为这是在链接的答案中,但似乎人们没有看过它:

这特别令人困惑,因为函数的参数 具有与类变量完全相同的名称,但 Patient.ptNo 与参数 ptNo 不是同一个变量。 (实际上, Patient.ptNo 应该是 this.ptNo,因为它属于 this 类的特定实例。 Patient.ptNo 将指单个 Patient 类型的所有对象共有的值。)

所以当人们说this.Variable = Variable时,我仍然对什么是什么感到困惑。

【问题讨论】:

  • 我喜欢你的新用户名 -- 或者我应该说用户短语 :-)
  • 如需尽快获得更好的帮助,请发帖SSCCE
  • 变量名无所谓:VarVariable对编译器来说是一样的。唯一的例外是如果它是 Java 中的保留字(VarvarVariablevariable 或任何其他大写变体都不是),在这种情况下它根本不会编译。
  • @yshavit 请注意,OP 已将变量固定为实例变量的名称,并询问构造函数中的变化。

标签: java parameters constructor class-variables


【解决方案1】:

使用变量作为构造函数参数没有问题,因为它是构造函数的局部变量。

class NewClass {

    private  String Variable = "";

    Public NewClass (String Variable)
    {
        this.Variable = Variable;
    }
}

                       or

class NewClass {

    private  String Variable = "";

    Public NewClass (String Var)
    {
        this.Variable = Var;
    }
}

两者都很好。

this.Variable表示当前对象变量。

尝试阅读命名约定。

【讨论】:

    【解决方案2】:

    类变量是类定义为static字段的变量,参数是方法定义的变量。就是这么简单。还有实例变量(在类级别定义,但不是静态的)和局部变量(在方法中定义,但不是作为输入参数)。

    public class Foo {
        private static String someName; // this is a class variable
        private String someOtherName; // this is an instance variable
    
        public Foo(String anotherName) { // anotherName is a constructor parameter
            int yetAnother = 1; // yetAnother is a local variable
            someOtherName = "foo"; // assign a value to someOtherName
        }
    

    这两个变量完全不同。他们甚至不必具有相同的类型!您的示例中唯一的复杂之处是两个变量恰好具有相同的名称。发生这种情况时,编译器将优先考虑构造函数参数(或方法参数,或局部变量)而不是类变量。为了“强制”它使用类变量,你需要在它前面加上 this.

    要记住的是,这两个变量是完全独立的,无论它们的名称如何。

    所以这个:

    class NewClass {
    
        private  String Variable = "";
    
        Public NewClass (String Variable)
        {
            NewClass.Variable = Variable;
        }
    }
    

    和这个完全一样:

    class NewClass {
    
        private  String Variable = "";
    
        Public NewClass (String someOtherVariableName)
        {
            NewClass.Variable = someOtherVariableName;
        }
    }
    

    ...也和这个一模一样:

    class NewClass {
    
        private  String Variable = "";
    
        Public NewClass (String Var)
        {
            NewClass.Variable = Var;
        }
    }
    

    对参数使用与类变量相同的名称的惯例只是意味着您不必在变量名称上想出无意义的变体。

    【讨论】:

    • NewClass.Variable = Var; 编译器将兼容,因为Variable 不是静态的
    【解决方案3】:

    this.Variable 将引用类的 Variable 字段,而 Variable 将是传递给构造函数的参数。

    private String Variable = ""; - 这不是静态的

    因此你的构造函数看起来很糟糕

    public NewClass(String Var/iable) {
        this.Variable = Variable; // this must be used, where this.Variable is the class variable and just Variable is the constructor parameter.
    }
    

    注意:您使用了ClassName.fieldName,它只允许用于类的静态字段。如果Variable 不是static,它应该是this.fieldName。此外,Public 不应大写,构造函数名称应为类名称。关键字class 应该小而不是Class

    如果您想使用您在代码中输入的方式,类中的Variable 应该是静态的。像这样的

    private static String Variable = "";
    public NewClass(String Var/iable) {
        NewClass.Variable = Variable; // Since Variable is static now, you use the classname to access the static field.
    }
    

    编辑: 使用相同的名称是绝对可以的,只要你能区分它们。在这种情况下,this.Variable 表示类变量,而 Variable 表示在创建此类的实例时传递给构造函数的参数。

    class Variable = Variable passed to the constructor; // This is what the below statement means
    this.Variable = Variable;
    

    【讨论】:

      【解决方案4】:
      class NewClass {
      
      private  String variable = "";
      
      public NewClass (String variable)
      {
          this.variable = variable;
      }
      }
      

      this.variable 引用当前对象实例。 请遵循命名约定

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-03
        • 2019-08-30
        • 1970-01-01
        • 2011-04-16
        相关资源
        最近更新 更多