官术网_书友最值得收藏!

Generating blueprints for objects

Imagine that you want to draw and calculate the areas of four different rectangles. You will end up with four rectangles drawn, with their different widths, heights, and calculated areas. It would be great to have a blueprint to simplify the process of drawing each rectangle with their different widths and heights.

In object-oriented programming, a class is a blueprint or a template definition from which the objects are created. Classes are models that define the state and behavior of an object. After defining 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.

Tip

Objects are also known as instances. For example, we can say each rectangle object is an instance of the rectangle class.

The following image shows four rectangle instances drawn, with their widths and heights specified: Rectangle #1, Rectangle #2, Rectangle #3, and Rectangle #4. We can use a rectangle class as a blueprint to generate the four different rectangle instances. It is very important to understand the difference between a class and the objects or instances generated through its usage. Object-oriented programming allows us to discover the blueprint we used to generate a specific object. Thus, we are able to infer that each object is an instance of the rectangle class.

We recognized four completely different real-world objects from the application's requirements. We need classes to create the objects, and therefore, we require the following four classes:

  • Square
  • Rectangle
  • Circle
  • Ellipse
主站蜘蛛池模板: 乌兰察布市| 文昌市| 云林县| 时尚| 太仓市| 金阳县| 额尔古纳市| 武宣县| 沾益县| 沁源县| 张家口市| 林州市| 色达县| 新乡县| 阿坝县| 右玉县| 垣曲县| 哈密市| 赤城县| 那曲县| 新密市| 林甸县| 广西| 阆中市| 依安县| 彩票| 额济纳旗| 班戈县| 天长市| 延庆县| 淅川县| 白银市| 兴业县| 柳林县| 罗甸县| 高邮市| 儋州市| 宜兰市| 乳山市| 瓮安县| 高安市|