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

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.

主站蜘蛛池模板: 平昌县| 禹城市| 札达县| 射阳县| 潼关县| 饶平县| 仁化县| 桃园市| 邳州市| 弥勒县| 河西区| 玉树县| 齐河县| 金昌市| 志丹县| 固镇县| 攀枝花市| 宝山区| 衡山县| 斗六市| 于田县| 富顺县| 福州市| 安阳市| 廉江市| 华亭县| 上高县| 胶南市| 永修县| 民乐县| 大方县| 全州县| 惠东县| 襄城县| 巴林左旗| 巴里| 和静县| 同心县| 云浮市| 绥化市| 红河县|