首页 > 编程语言 > 详细

Python面向对象-继承和多态

时间:2020-07-29 18:06:12      阅读:104      评论:0      收藏:0      [点我收藏+]

继承

class Animal(object):
    def run(self):
        print(‘Animal is running...‘)

class Dog(Animal):
    pass

class Cat(Animal):
    pass

Dog().run()
Cat().run()

运行结果:
Animal is running...
An animal is running...

多态(重写)

class Animal(object):
    def run(self):
        print(‘Animal is running...‘)

class Dog(Animal):

    def run(self):
     print(‘Dog is running...‘)

class Cat(Animal):

    def run(self):
     print(‘Cat is running...‘)

Dog().run()
Cat().run()

运行结果:
Dog is running...
Cat is running...

多继承

import unittest
from selenium import webdriver

class MyTest(unittest.TestCase):
    def setUp(self):
        print("=============开始执行测试=============")
        self.driver=webdriver.Chrome()
        self.driver.get(‘https://www.baidu.com‘)
        self.driver.implicitly_wait(5)
        self.driver.maximize_window()

    def tearDown(self):
        self.driver.quit()
        print("=============结束执行测试=============")

class A(object):

    def getName(self):
        print("name is A")

class C(MyTest,unittest.TestCase):
    def test_name(self):
        print("name is C")

class D(C,unittest.TestCase):
    def test_home(self):
        print("name is D")
unittest.main()

运行结果:
=============开始执行测试=============
name is C
.=============结束执行测试=============
=============开始执行测试=============
name is D
.=============结束执行测试=============
=============开始执行测试=============
name is C
.
=============结束执行测试=============

Python面向对象-继承和多态

原文:https://www.cnblogs.com/hghua/p/13398298.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!