GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — python_test/test18.py at master · Hanlen520/python_test Expand file tree/ Copy pathtest18.pyMore file actions 42 lines (30 loc) · 894 Bytes / Copy pathtest18.pyFile metadata and controls42 lines (30 loc) · 894 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 #!/usr/bin/env python3 # -*- coding: utf-8 -*- '练习面向对象的继承和多态' __author__ = 'sergiojune' class Animal(object): def run(self): print('animal is running') # 继承Animal类 class Dog(Animal): # 重载父类的方法 def run(self): print('Dog is running') class Cat(Animal): def run(self): print('cat is running') def run_twich(animal): animal.run() animal.run() animal = Animal() dog = Dog() cat = Cat() # 类继承了Animal,所以类中不需要实现任何东西都可以有父类方法 animal.run() dog.run() cat.run() # 这个方法只要是有run方法都可以运行,这就是多态的实现 # 看起来像鸭子,走起路来像鸭子,那这个就是鸭子,这就是鸭子类型,动态语言的鸭子类型 run_twich(animal) run_twich(dog) run_twich(cat) |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |