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

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.

主站蜘蛛池模板: 富平县| 南部县| 光泽县| 阳信县| 锡林浩特市| 建昌县| 吕梁市| 大理市| 德安县| 哈巴河县| 麻城市| 台中县| 闸北区| 永德县| 寻乌县| 嵩明县| 凌源市| 界首市| 松潘县| 平泉县| 揭东县| 铜川市| 晋中市| 淮南市| 新余市| 奉贤区| 泗阳县| 罗城| 胶州市| 周口市| 自贡市| 象州县| 广州市| 宾阳县| 呼玛县| 二连浩特市| 武川县| 克山县| 辽源市| 康保县| 南宁市|