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

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.

主站蜘蛛池模板: 玉田县| 巴东县| 双柏县| 宣威市| 双桥区| 永春县| 谢通门县| 云安县| 台湾省| 唐河县| 海原县| 海宁市| 宜兴市| 井研县| 台湾省| 乐东| 喜德县| 泾川县| 喀喇沁旗| 万宁市| 威远县| 蒙山县| 长沙县| 福鼎市| 枞阳县| 望江县| 沁源县| 崇礼县| 陕西省| 福鼎市| 苏尼特左旗| 海城市| 方城县| 页游| 侯马市| 宣城市| 通化市| 中宁县| 广州市| 滨海县| 馆陶县|