课程: Learning Java 17

Solution: Book name - Java教程

课程: Learning Java 17

Solution: Book name

- [Instructor] Let's implement a book class and initialize the book name. The first thing we'll do is set up the constructor. We're already passing in the name we want the book to have, so we just have to set our instance variable to it. We'll set our instances name or this.name, to the name passed into the function. That's name. We only have one instance variable, so that's it for our constructor. Next, we'll implement the get name function. This should return our book's name. We can access it with this.name. We access this instance in the instance variable we want, here that's name. Then we return it from the function. Let's run it. The code works as expected. While this might seem simple, it's really important to know where to put certain pieces of code. Here we navigated putting code in different scopes and within different functions, so the class as a whole would work as expected.

内容