课程: Learning Java 17

What is a constructor? - Java教程

课程: Learning Java 17

What is a constructor?

- [Instructor] In order to use a function, it has to be defined first. The same is true with classes. We have to define a blueprint or a class in order to use it. In the last lesson, we defined the Triangle class but we didn't create any triangles yet. We defined the attributes and behavior in a blueprint. In this lesson, we're going to learn how to create individual Triangle instances in our program. An instance is an object created from the class blueprint. Just like we make buildings from blueprints in real life, we use a class blueprint to create objects or instances in code. Thinking back to our Triangle class, a triangle has a base, height and three side lengths. Another instance of a Triangle could have base, 3, height, 2.598 and side lengths, three, three and three. From our single Triangle class, we can create as many instances as we want. Each instance will have its own base, height and side length attribute values. So how do we create these instances? Well, we use a special method or behavior called a constructor. Every class has a constructor and it's how we construct and initialize each triangle instance. It's how we can create Triangle A with a specific base, height and side length values. Then we can construct Triangle B with different attribute values. We just have to call the special function inside the Triangle class. So what does a constructor look like? Since it's a function, it has a name and that name is always the same name as the class. In this case, that's Triangle. We have open, close parentheses for the inputs to the function and then brackets for the implementation. Right now, they're empty. In the implementation, we won't be using the return keyword either because no matter what we do in the constructor, it will always return an instance of the class, in this case, Triangle. We could set these to an default value but ideally, this constructor would be dynamic and on the fly decide what values to give these attributes. To do this, we can add parameters representing each Triangle attribute to the function. Just like our parameters in other functions, each parameter does not have to have the same name as the attribute their data represents but that's often the case. This might look a little intimidating but stick with me. This was tough for me too when I first got started with Java. With parameters, we have access to the appropriate values we want to assign each attribute. To access the attribute variable for the object we're constructing, we use the this keyword and the dot operator. The this keyword helps our program make a distinction between the attribute variable and the parameter variable. We use the keyword this because we're talking about an attribute of the Triangle we're constructing, rather than the parameter variable. When we use the dot operator on the keyword this, we can access the to-be-created instance's attribute variables. This allows us to access the instance's base, height and side lengths so we can initialize them or give them a value. With the code below, we use the corresponding parameters to give values to the attributes of the new Triangle instance. In the constructor, we set the Triangle's base attribute to be the value given in the base parameter. We initialize the new Triangle's height attribute with the value in the height parameter. We do this for each attribute of the Triangle we're creating. And that's our constructor function defined. How do we use it? Just like any other function, we call it with a set of inputs and it will return a Triangle. The only difference is that because we're creating a new Triangle instance, we have to use the new keyword. In this example, we create two Triangles. One Triangle is triangleA and the other is triangleB. We use the new keyword and call the constructor with the appropriate attribute values we want. Each call returns a triangle that is now stored in the respective Triangle variable. The triangleA instance will have a base with the value 15, height with the value 8, side length 1, 15, 8 and then side length 3 with value 17. The triangleB instance will have a base with the value 3, a height, 2.598 and all side lengths with the value three. Now, you might be thinking Triangle is now a data type? And yes, it is. When we create a class, we're essentially creating a new way to store a group of data. Think about all those double values we're storing. Our class is a particular way we've decided to store and organize data about a triangle. This means when we create a triangle or store it in a variable, or perhaps even use it as a parameter, we must remember to use Triangle as our data type.

内容