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

Polymorphism with classes (abstract classes)

There may be occasions in which we would want to define a template of a class in terms of what that class must do, but not in terms of how it should do it – we would want to leave that to the implementation of the class. This is where we can make use of abstract classes. Let's create a script called Poly_class.py and add the following code:

It can be seen that we have an abstract class called Shape, which has an area method. The area method is not implemented in this class, but it would be implemented in the child class. The Square and Circle child classes override the area method. The area method is polymorphic, which means that if a square overrides it, it implements the area of a square and when a Circle class overrides it, it implements the area of a circle.

主站蜘蛛池模板: 宝应县| 南岸区| 泽州县| 松阳县| 瑞金市| 瑞金市| 普兰店市| 江门市| 小金县| 黄浦区| 婺源县| 仁怀市| 靖边县| 邵东县| 巴彦县| 永登县| 遵义县| 突泉县| 枣强县| 思茅市| 尉犁县| 太仆寺旗| 绵竹市| 左贡县| 余姚市| 诸城市| 沙田区| 九龙县| 墨脱县| 廉江市| 隆昌县| 临漳县| 大丰市| 五河县| 淮北市| 珲春市| 通许县| 南雄市| 新化县| 玉门市| 会宁县|