类变量和构造函数中的参数有什么区别?

类变量和构造函数中的参数有什么区别?

问题描述:

我是在回答此答案.

我使用的是VariableVar- edit 的示例,请注意,我询问在哪里使用VarVariable:

I'm using an example of Variable and Var- edit Pls note I am asking where to use Var or Variable:

Class NewClass {

    private  String Variable = "";

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

OR

    private  String Variable = "";

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

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

Which ones are the class variables, how does this differ from the parameters and which goes where?

修改

我应该添加,因为它在链接的答案中,但是似乎人们没有看过它:

I should add, as this was in the linked answer, but it seems people haven't looked at it:

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

It's particularly confusing because the parameters to the function have exactly the same names as the class variables, but Patient.ptNo is not the same variable as the parameter ptNo. (Actually, Patient.ptNo should be this.ptNo, because it belongs to this particular instance of the class. Patient.ptNo would refer to a single value that's common to all objects of type Patient.)

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

So when people say this.Variable = Variable I am still confused about what is what.

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

The class variable is the one defined by the class as a static field, and the parameter is the one defined by the method. It's just that simple. There are also instance variables (defined at the class level but not static) and local variables (defined within a method, but not as input parameters).

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..

These two variables are completely distinct. They don't even have to have the same type! The only complication in your example is that both variables happen to have the same name. When that happens, the compiler will favor the constructor parameter (or method parameter, or local variable) over the class variable. In order to "force" it to use the class variable, you prefix it with this..

要记住的是,这两个变量是完全独立的,而不管它们的名称是什么.

The thing to keep in mind is that the two variables are totally separate, regardless of their names.

所以这个:

class NewClass {

    private  String Variable = "";

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

与此完全相同:

class NewClass {

    private  String Variable = "";

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

...它也与此完全相同:

... and it's also exactly the same as this:

class NewClass {

    private  String Variable = "";

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

对于参数使用与类变量相同的名称的约定只是意味着您不必在变量名称上提出毫无意义的变体.

The convention of using the same name for a parameter as for the class variable just means you don't have to come up with pointless variants on the variable name.