课程: Java Essential Training: Syntax and Structure

今天就学习课程吧!

今天就开通帐号,24,600 门业界名师课程任您挑!

Code Solution: Create a car object

Code Solution: Create a car object

(upbeat music) - [Instructor] In this challenge, your task is to build a car. You were provided with the car class, which serves as a blueprint containing the attributes of a car. In the buildsCar method, this method accepts details about a specific car, allowing you to construct an instance of the car class. In fact, let's go ahead and do that now. So we create an object of type car, and we can name it Car as well. And to create an instance of the car class, we'll use the new keyword, and we'll call the car constructor, which accepts the make and the model. So now we have a car, and we have the make and the model specified. However, we also need to include the year and the color. We can do that by calling the car setter methods. So we can say car.setYear, and we'll pass in the year, and then car.setColor, and we'll pass in the color. Finally, we'll go ahead and return the car object. And let's go ahead and test this. Great, it worked.

内容