课程: Learning Java 17
Creating a class in Java
- [Instructor] With an understanding of classes, instances in the constructor, we can begin to write Java code that represents a triangle. First, we need to write code that defines our triangle blueprint and we can do that with a triangle class. In Java we can create a new class file. Alt control click the SRC folder and create a new Java class. We'll name it Triangle. We could have also created that class by going File, New, and Java Class using the navigation bar. Inside the curly braces is where we'll add our attributes and behaviors. In earlier lessons we said a triangle has a base, height, and three different side lengths. We can add these as attributes with double base, double height, and then each side length. These are the attributes of our triangle class. We have five attribute variables but they don't have a value yet. These attribute variables are also called instance variables because they're variables that each instance will have an individual value for. We could assign a default value to these but usually we'll use a constructor to initialize to them. With a constructor we can create a triangle instance with a specific base, height, and side lengths. To create a constructor we'll write public, triangle, and then add the parameters. These will allow us to input data to the class and assign each attribute value. We'll access this triangles base and assign it the value base that we input into the function. We'll access the height and do the same thing for that attribute as well, taking the inputted value from the parameter and assigning it to the value of the attribute. The attribute for the triangle we're creating. You may notice that as we use each attribute it becomes highlighted because it's now in use by the program. It's not just some attribute or variable that's never used. This is a feature of our IDE. Now, if we think of the code in relation to scope the attributes are accessible throughout the triangle class but the constructor's parameters are only accessible inside the constructor. Since the attribute and the constructor parameter have the same name we use the dot operator on this instance, the instance we're creating, to keep them separate. So with this code, we have a constructor that takes in unique values using parameters and then assigns those values to their corresponding attribute. The constructor is just one behavior our class has but we can add as many behaviors as we want. In an earlier lesson we talked about adding a function that finds the area of a triangle to this class. Let's do that. The function we'll be creating will return a double and we'll call it, findArea. We do need a return type double because findArea is not the constructor, it's just a regular behavior of the class. We'll also take in no inputs. Instead we'll use the this keyword to get access to the appropriate attribute values. That's the base and height. Our formula for finding the area of a triangle is base times height divided by two. So we'll return this dot base times this dot height, divided by two. We can access this dot base and this dot height here because the base and height attribute variables are created within the triangle class. We also have parentheses so the multiplication is evaluated before the division, similar to the calculator rules. With this last behavior, our class is defined.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。
内容
-
-
-
-
-
-
-
-
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 秒
-
-