英文:
Java Constructors - how to create them
问题
E.g.1
public class Dog {
String breed;
boolean hasOwner;
int age;
public Dog(String dogBreed, boolean dogOwned, int dogYears) {
breed = dogBreed;
hasOwner = dogOwned;
age = dogYears;
}
E.g.2
public class Dog {
String breed;
boolean hasOwner;
int age;
public Dog(String dogBreed, boolean dogOwned, int dogYears) {
this.breed = dogBreed;
this.hasOwner = dogOwned;
this.age = dogYears;
}
英文:
I am currently learning Java and I had a question with constructors in Java. When learning, I am sometimes seeing constructors written differently, shown in my 2 examples. Can you please explain what is the difference between these 2 and why you would use one over the other. One is using "this." and the other isn't, this has been confusing me. is there a proper way to write this.
E.g.1
public class Dog {
String breed;
boolean hasOwner;
int age;
public Dog(String dogBreed, boolean dogOwned, int dogYears) {
breed = dogBreed;
hasOwner = dogOwned;
age = dogYears;
E.g.2
public class Dog {
String breed;
boolean hasOwner;
int age;
public Dog(String dogBreed, boolean dogOwned, int dogYears) {
this.breed = dogBreed;
this.hasOwner = dogOwned;
this.age = dogYears;
答案1
得分: 0
虽然上面的两个例子都实现了相同的功能,但是最好还是用 this
关键字来引用实例变量。
this
实际上是指向类的自身对象的引用。希望你已经对Java的基础和面向对象的原则有所了解,在Java中,一切都被视为对象。即使你尝试访问变量,也必须通过创建实例(简单来说就是对象)来进行。回到你的例子,因为你正在设置实例变量,所以应该使用 this
关键字进行引用。换句话说,你在明确地说明应该使用传递的参数来设置这个类的实例变量。
我还想指出一件事情。我注意到在示例2中,你改变了构造函数的参数,只是为了确保Java不会混淆实例变量和参数。但在这样做的同时,你改变了参数的含义或描述意图。可以通过以下方式避免这种情况:
public class Dog {
String breed;
boolean hasOwner;
int age;
public Dog(String breed, boolean hasOwner, int age) {
this.breed = breed;
this.hasOwner = hasOwner;
this.age = age;
// 任何带有 this. 的变量都是实例变量,没有 this. 的则是参数。
}
}
我希望你现在理解了 this
关键字的用途。请记住,this
不仅局限于变量,还可以与方法一起使用。只要记住 this
实际上是自引用的关键字,你就不会感到困惑。
英文:
Although both of them (examples above) does the same thing but it's always good to refer the instance variables with the this
keyword.
this
is nothing but reference to the self object of the class. Hoping that you are already aware of the basics of Java & Object Oriented principles, in Java everything is referred as Objects. Even if you try to access variables you have to do it by objects by creating an instance (i.e. object in simple terms). Coming back to your examples, since you are setting the instance variables you should use by referring this
keyword. In other words you are explicitly saying that the instance variables of this class should be set with the data being passed as parameters.
One more thing I would like to point out. I see that in the Example 2, you have changed the parameter of the constructor just to make sure that Java is not confused with the instance variable and the parameters. But while doing that you changed the meaning or descriptive intent of the parameters. This could have been avoided something like below -
public class Dog {
String breed;
boolean hasOwner;
int age;
public Dog(String breed, boolean hasOwner, int age) {
this.breed = breed;
this.hasOwner = hasOwner;
this.age = age;
// Any this.variables are referred as the instance variables and the ones
// without the this are referred as the parameters.
}
}
I hope that you have now understood purpose of the this
keyword. Remember this
is not restricted only to variables but can also be used with methods. As long as you remember the that this
is nothing but reference to self, you will never get confused.
答案2
得分: 0
这很基础,但为了做出正确的决定,您可能需要了解“this”关键字的确切含义。
在您给出的示例中,两者都可以,使用或不使用“this”,但第二个示例由于以下原因过于复杂:
我们使用“this”关键字来引用我们正在创建的对象。在您的第二个示例中,“this”和“Dog”可以在该上下文中视为相同的事物。因此,在类Dog内部,使用“this”与“chien”或您创建的任何其他“Dog”对象是相同的。
那么为什么要使用“this”?基本原因之一是构造函数或方法的参数,如果您将它们与属性命名相同。
例如,您可以这样做:
public class Dog {
String breed;
boolean hasOwner;
int age;
public Dog(String breed, boolean hasOwner, int age) {
this.breed = breed;
this.hasOwner = hasOwner;
this.age = age;
这与您的示例1一样有效。使用“this”可以指示要为Dog的属性赋值,该值作为参数提供。否则,您避免编写“breed = breed”之类的模棱两可的语句。
总之,如果您的参数名称与属性名称不同,则无需使用“this”,因此示例1和2都可以,但在示例2中无需使用它,示例1就足够了。然而,对于Java中的进一步概念,习惯于将“this”用作对象的自我引用是一种良好的做法。这可能有助于您更好地理解未来在Java中有用的事物。
英文:
This is pretty basic, but for proper decision you may consider knowing the poper meaning of this
.
In your given examples, both are fine, with and without the use of this
but the 2. example is overkill due to the following reason:
We use this
to refer to the Object we are creating. In your example 2 this
and Dog we can say they are the same thing inside that context. So say: Dog chien = new Dog(...);
inside the class Dog the use of this
would be the same as chien
or any other Dog
object you have created.
So why using this
? Ones of the basic reasons are Parameter given to the constructor or to the methods, in case you name them the same as the atributes.
For example you can do:
public class Dog {
String breed;
boolean hasOwner;
int age;
public Dog(String breed, boolean hasOwner, int age) {
this.breed = breed;
this.hasOwner = hasOwner;
this.age = age;
This is valid as well as your exaple 1. With this
you indicate that to the Dog's attribute you want to asign a value, wich is given as a parameter. Otherwise you avoid writing such ambiguity like breed = breed
.
All in all, if the name of your parameters are different from the name of your attributes, then you don't need to use this
, so no need both examples 1 and 2 are fine, but no need for it at 2., example 1 is just fine. However for further concepts in java, it is a good practise to get used to this
as self reference the the object. That may help you get better understanding of future useful thigs in java.
答案3
得分: -1
在Java中,构造函数是一块类似于方法的代码。它在创建类的实例时被调用。在调用构造函数时,会在内存中为对象分配内存。
它是一种特殊类型的方法,用于初始化对象。
每次使用new()
关键字创建对象时,至少会调用一个构造函数。
关键字this
是Java中的一个引用变量,它引用当前对象。当我们在构造函数中使用与已定义变量相似的参数时,会在构造函数中使用这个关键字。
public class Dog {
String breed;
boolean hasOwner;
int age;
public Dog(String dogBreed, boolean dogOwned, int dogYears) {
this.breed = dogBreed;
this.hasOwner = dogOwned;
this.age = dogYears;
英文:
In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling constructor, memory for the object is allocated in the memory.
It is a special type of method which is used to initialize the object.
Every time an object is created using the new() keyword, at least one constructor is called.
Keyword this
is a reference variable in Java that refers to the current object. When we use the same parameters in the constructor that similar to variables that defined we use this keyword in the constructor.
public class Dog {
String breed;
boolean hasOwner;
int age;
public Dog(String dogBreed, boolean dogOwned, int dogYears) {
this.breed = dogBreed;
this.hasOwner = dogOwned;
this.age = dogYears;
答案4
得分: -1
当你使用 this 时,赋予参数的值。
public class Dog{
String breed;
boolean hasOwner;
int age;
public Dog(String dogBreed, boolean dogOwned, int dogYears){
this.breed = dogBreed;
this.hasOwner = dogOwned;
this.age = dogYears;
}
}
因此现在,如果你不使用 Java 的 "this" 关键字,编译器无法理解当你尝试使用局部变量时,你的输出会显示为零或取决于情况。
英文:
When you use this assign the value of parameter.
public class Dog{
String breed;
boolean hasOwner;
int age;
public Dog(String dogBreed, boolean dogOwned, int dogYears){
this.breed = dogBreed;
this.hasOwner = dogOwned;
this.age = dogYears;
}
}
So now, if you don't use "this" keyword of java, the compiler can't understand when you trying use your local variable and you output show zero or depend.
专注分享java语言的经验与见解,让所有开发者获益!
评论