课程: 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.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。
内容
-
-
-
-
-
-
-
-
What is a class?4 分钟 10 秒
-
What is a constructor?6 分钟 14 秒
-
Creating a class in Java4 分钟 24 秒
-
Creating an instance in Java5 分钟
-
Instance versus class members3 分钟 25 秒
-
Using instance and class methods in Java3 分钟 14 秒
-
Instance and class variables in Java4 分钟 16 秒
-
Reviewing classes versus instances2 分钟 6 秒
-
Solution: Book name1 分钟 19 秒
-
-