- Learning Object-Oriented Programming
- Gastón C. Hillar
- 434字
- 2021-07-16 13:46:03
Recognizing attributes/fields
We already know the information required for each of the shapes. Now, it is time to design the classes to include the necessary attributes that provide the required data to each instance. In other words, we have to make sure that each class has the necessary variables that encapsulate all the data required by the objects to perform all the tasks.
Let's start with the Square class. It is necessary to know the length of side for each instance of this class, that is, for each square
object. Thus, we need an encapsulated variable that allows each instance of this class to specify the value of the length of side.
Tip
The variables defined in a class to encapsulate data for each instance of the class are known as attributes or fields. Each instance has its own independent value for the attributes or fields defined in the class.
The Square
class defines a floating point attribute named LengthOfSide
whose initial value is equal to 0
for any new instance of the class. After you create an instance of the Square
class, it is possible to change the value of the LengthOfSide
attribute.
For example, imagine that you create two instances of the Square
class. One of the instances is named square1, and the other is square2. The instance names allow you to access the encapsulated data for each object, and therefore, you can use them to change the values of the exposed attributes.
Imagine that our object-oriented programming language uses a dot (.
) to allow us to access the attributes of the instances. So, square1.LengthOfSide
provides access to the length of side for the Square
instance named square1
, and square2.LengthOfSide
does the same for the Square
instance named square2
.
You can assign the value 10
to square1.LengthOfSide
and 20
to square2.LengthOfSide
. This way, each Square
instance is going to have a different value for the LengthOfSide
attribute.
Now, let's move to the Rectangle class. We can define two floating-point attributes for this class: Width
and Height
. Their initial values are also going to be 0
. Then, you can create two instances of the Rectangle
class: rectangle1
and rectangle2.
You can assign the value 10
to rectangle1.Width
and 20
to rectangle1.Height
. This way, rectangle1
represents a 10 x 20 rectangle. You can assign the value 30
to rectangle2.Width
and 50
to rectangle2.Height
to make the second Rectangle
instance, which represents a 30 x 50 rectangle.
The following table summarizes the floating-point attributes defined for each class:

The following image shows a UML (Unified Modeling Language) diagram with the four classes and their attributes:

- Hands-On Machine Learning with scikit:learn and Scientific Python Toolkits
- 零基礎學C++程序設計
- Blockly創意趣味編程
- SQL Server 2016數據庫應用與開發
- 編程菜鳥學Python數據分析
- 從零開始學Python網絡爬蟲
- ASP.NET Web API Security Essentials
- 深入實踐DDD:以DSL驅動復雜軟件開發
- 視窗軟件設計和開發自動化:可視化D++語言
- 深度學習入門:基于Python的理論與實現
- Web開發的平民英雄:PHP+MySQL
- Building a Media Center with Raspberry Pi
- Java編程指南:語法基礎、面向對象、函數式編程與項目實戰
- C語言程序設計實驗指導與習題精解
- Learning jqPlot