课程: Learning Java 17

What is a class? - Java教程

课程: Learning Java 17

What is a class?

- [Instructor] In this chapter we'll keep discussing program design, and how we can make our programs readable to other software engineers. As you enter the workforce there are often several engineers working on the same program, so it's essential to write code that's easy to read and understand. A lot of times I forget about the code I've written six months ago, and if it's written in a way that's hard to understand I have to take a significant amount of time to understand it before I can add functionality to it. We want to prevent that. One way to make our programs easier to read is by using classes. We've seen classes a little bit before because all of our code has been contained in the Hello World class, but now we're going to dive a little deeper. Many of the things we see, hear, or experience in everyday life can be represented in code. We've represented the on repeat functionality of a music player, the final total that's on a restaurant receipt, and more. How well something is represented is up to us, and what we decide to code. Although we've used functions to represent these so far most of the time we'll want to use classes to represent these things. A class acts as a blueprint for something we want to represent in code. It's a user-defined blueprint with a set of attributes and behaviors. Let's say we wanted to create a class or a blueprint for a triangle. When we think of a triangle it has a base, height, and three sides with various lengths. These are attributes or properties of a triangle, so we can define them as that for our class. Each of these attributes will have a data type, and they'll act as variables in our blueprint. Every triangle we create will have a base, height, and three side lengths. That's what makes a triangle a triangle. The value of every triangle will likely not be the same. Some might have a greater base or a smaller height, but every triangle will have these attributes or properties. These attributes are really variables that will store a particular value for a given triangle. A class can also have behavior. The behaviors are defined as functions that are related to the class. In the debugging chapter we debugged some code that calculated the area of a triangle. We can turn that into a function called findArea, and add that as a behavior to the Triangle class. The functionality would be the same, but it's nice to make it a function so we can reuse it as well as organize it by putting it inside of our Triangle class. We often refer to these behaviors as methods. We could say findArea is a method of the Triangle class. Another behavior we could add is a calculation function. It would determine whether the triangle is equilateral, isosceles, or scalene. If you're familiar with geometry an equilateral triangle has equal side lengths, an isosceles triangle has two side lengths that are equal, and a scalene triangle has all unique side lengths. Using some if statements and some comparisons we could implement a function called calculateTriangleType, and it would return the type of triangle. Now, one could argue that the fact that the triangle is equilateral, isosceles, or scalene is more of an attribute or a property rather than something to be calculated, but this is just one programmatic design. There are a bunch of different ways you could represent a triangle in code. It just depends on what functionality you want, and what data you want to store.

内容