简体   繁体   English

这两种方法有什么区别?

[英]What is the difference between those 2 methods?

Method number 1: 方法1:

public void setAge(int newAge){
    age = newAge;
}

Method number 2 方法2

public void setAge (int age){
    this.age = age;
}

In both, it must be assumed that the object has a field called age . 在这两种情况下,都必须假定对象具有一个称为age的字段。

Second Version 第二版

In the second one, there is also a parameter called age . 在第二个中,还有一个名为age的参数。 Since the parameter age has the same name as the field age , it " hides " the field. 由于参数age具有相同的名称作为字段age ,它“ 隐藏 ”的领域。 So, within the second version, anywhere you see age will be referring to the parameter, not the field. 因此,在第二个版本中,您看到age任何地方都将引用该参数,而不是该字段。

Since the field is hidden, you need a way to still access it. 由于该字段是隐藏的,因此您需要一种仍然可以访问它的方法。 You can still get at it by using the this reference. 您仍然可以通过使用this参考资料来了解它。 this refers to "this" object, and this.age accesses the field instead of the parameter. this是指“ this”对象, this.age访问字段而不是参数。 So field this.age is set to parameter age . 因此,将this.age字段设置为参数age

First Version 第一版

The first version just avoids this ambiguity by using a different parameter name so that the field is not hidden , so it still sets field age to the parameter but does not need the this. 第一个版本只是通过使用不同的参数名称来避免这种歧义,从而不会隐藏字段 ,因此它仍将字段age设置为该参数,但不需要this. prefix . 前缀

两者都是setter方法,这是将值分配给类内的私有变量的通用约定,这两种方法具有相同的功能,第一种方法是将值分配给称为age的类字段,而第二种方法也将值分配给相同的变量,但该变量是由年龄参数阴影,因为他们的名字是一样的,所以要改变字段age而不是仅仅在函数内部可用的本地参数,你需要使用this.age这意味着你要改变字段而不是局部变量。

In the first case, it is clear both variables are different, so no problem. 在第一种情况下,很明显两个变量都不同,所以没有问题。

But in the second case which age you are explicitly specifying which age you are referring to and it is a good practice. 但在这第二种情况下age你是明确指定其age你指的是,这是一个很好的做法。 We add this.age so that compiler knows you are referring to class variable not local variable in left side and right side local variable age 我们添加this.age以便编译器知道您在左侧和右侧局部变量 age中引用的是类变量而不是局部变量

Considering that setAge function is a member function of a class that has a member variable age : Both codes will change the value of age to the argument passed to the function. 考虑到setAge函数是具有成员变量age的类的成员函数:两种代码都会将age的值更改为传递给该函数的参数。

Having said that, second method is considered a good practice where you are explicitly specifying that you want to change the member variable of the current object by using this keyword. 话虽这么说,第二种方法被认为是一种好习惯,您可以通过使用this关键字来明确指定要更改当前对象的成员变量。

I think that age is a member of the class you use. 我认为年龄是您使用的课​​程的成员。 In this case in second method the argument got a same name as member class so you need to use this specifier inside a method to specify that you want to assign a new value to a class member. 在这种情况下,在第二种方法中,参数的名称与成员类的名称相同,因此您需要在方法内使用this说明符来指定要为类成员分配新值。

Both methods are the same, will do same thing, but standard is to use 两种方法相同,会做相同的事情,但标准是使用

public void setAge (int age){
     this.age = age;
}

with "this.age" (this-meaning the age of your object) and with the same parameter name as the field. 带有“ this.age”(这表示对象的年龄),并且具有与字段相同的参数名称。

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

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