- Java 9 with JShell
- Gastón C. Hillar
- 515字
- 2021-07-09 18:46:46
Generating classes to create objects
Imagine that we have to draw and calculate the perimeters and areas of three different rectangles. You will end up with three rectangles drawn with their widths and height values and their calculated perimeters and areas. It would be great to have a blueprint to simplify the process of drawing each rectangle with their different width and height values.
In object-oriented programming, a class is a template definition or blueprint from which objects are created. Classes are models that define the state and behavior of an object. After declaring a class that defines the state and behavior of a rectangle, we can use it to generate objects that represent the state and behavior of each real-world rectangle.
Note
Objects are also known as instances. For example, we can say each rectangle
object is an instance of the Rectangle
class.
The following picture shows two rectangle instances named rectangle1
and rectangle2
. These instances are drawn with their width and height values specified. We can use a Rectangle
class as a blueprint to generate the two different Rectangle
instances. Note that rectangle1
has the width and height values of 36
and 20
, and rectangle2
has the width and height values of 22
and 41
. Each instance has different values for their width and height. It is very important to understand the difference between a class and the objects or instances generated through its usage. The object-oriented programming features supported in Java 9 allow us to discover which blueprint we used to generate a specific object. We will use these features in many examples in the upcoming chapters. Thus, we can determine whether each object is an instance of the Rectangle
class or not.

The following picture shows two regular pentagon instances named pentagon1
and pentagon2
. These instances are drawn with their length of side values specified. We can use a RegularPentagon
class as a blueprint to generate the two different RegularPentagon
instances. Note that pentagon1
has the length of a side value of 20
, and pentagon2
has the length of a side value of 16
. Each instance has different values for its length of a side.

The following picture shows four ellipse instances named ellipse1
, ellipse2
, ellipse3
, and ellipse4
. These instances are drawn with their semimajor axis and semiminor axis values specified. We can use an Ellipse
class as a blueprint to generate the four different Ellipse
instances. Note that each ellipse has its own specific values for the semimajor and semiminor axes.

We recognized nine completely different real-world objects from the Web Service requirements, and therefore, we can generate the following nine classes to create the necessary objects:
Circle
Ellipse
EquilateralTriangle
Square
Rectangle
RegularPentagon
RegularHexagon
RegularOctagon
RegularDecagon
Tip
Note the usage of Pascal case for class names. Pascal case means that the first letter of each word that composes the name is capitalized, while the other letters are in lowercase. This is a coding convention in Java. For example, we use the EquilateralTriangle
name for the class that will be the blueprint that will allow us to generate multiple equilateral triangles.
- Web應用系統開發實踐(C#)
- ASP.NET Core 5.0開發入門與實戰
- Linux核心技術從小白到大牛
- Java應用開發與實踐
- NumPy Essentials
- Python數據可視化之Matplotlib與Pyecharts實戰
- Java Web程序設計任務教程
- Mastering Business Intelligence with MicroStrategy
- LabVIEW虛擬儀器程序設計從入門到精通(第二版)
- 智能手機故障檢測與維修從入門到精通
- Java EE 7 with GlassFish 4 Application Server
- Unity 5.X從入門到精通
- jQuery Mobile Web Development Essentials(Second Edition)
- Scratch編程從入門到精通
- 輕松學Scratch 3.0 少兒編程(全彩)