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

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.

主站蜘蛛池模板: 元谋县| 辽阳市| 房山区| 海安县| 伊宁县| 宁明县| 桃江县| 通辽市| 理塘县| 南靖县| 察隅县| 田东县| 资阳市| 许昌县| 墨竹工卡县| 台东县| 徐州市| 桦南县| 西充县| 青河县| 滁州市| 微山县| 长宁县| 西丰县| 通化县| 两当县| 大姚县| 商城县| 西林县| 东山县| 依兰县| 仙居县| 辽宁省| 丹阳市| 南阳市| 连南| 温州市| 潮安县| 高唐县| 临洮县| 湖北省|