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

Understanding classes and instances

In the previous chapter, you learned some of the basics of the object-oriented paradigm, including classes and objects, also known as instances. Now, when you pe deep into the programming languages, the class is always going to be the type and the blueprint. The object is the working instance of the class, and one or more variables can hold a reference to an instance.

Let's move to the world of our best friends, the dogs. If we want to model an object-oriented application that has to work with dogs and about a dozen dog breeds, we will definitely have a Dog abstract class. Each dog breed required in our application will be a subclass of the Dog superclass. For example, let's assume that we have the following subclasses of Dog:

  • TibetanSpaniel: This is a blueprint for the dogs that belong to the Tibetan Spaniel breed
  • SmoothFoxTerrier: This is a blueprint for the dogs that belong to the Smooth Fox Terrier breed

So, each dog breed will become a subclass of Dog and a type in the programming language. Each dog breed is a blueprint that we will be able to use to create instances. Brian and Merlin are two dogs. Brian belongs to the Tibetan Spaniel breed, and Merlin belongs to the Smooth Fox Terrier breed. In our application, Brian will be an instance of the TibetanSpaniel subclass, and Merlin will be an instance of the SmoothFoxTerrier subclass.

As both Brian and Merlin are dogs, they will share many attributes. Some of these attributes will be initialized by the class, because the dog breed they belong to determines some features, for example, the area of origin, the average size, and the watchdog ability. However, other attributes will be specific to the instance, such as the name, weight, age, and hair color.

主站蜘蛛池模板: 南陵县| 东安县| 岑溪市| 六安市| 高邑县| 宁远县| 汉阴县| 武夷山市| 南溪县| 铁岭市| 北流市| 东安县| 永登县| 罗城| 萝北县| 汉川市| 崇信县| 涞源县| 阿鲁科尔沁旗| 阿瓦提县| 瑞金市| 公主岭市| 黄梅县| 石景山区| 调兵山市| 扎赉特旗| 上虞市| 乌拉特中旗| 乐平市| 精河县| 蕉岭县| 嘉定区| 和硕县| 剑阁县| 武平县| 连云港市| 徐州市| 德江县| 佛坪县| 福安市| 贵阳市|