• Python面向对象-继承和多态


    继承

    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
    .
    =============结束执行测试=============
    
  • 相关阅读:
    java 线程状态和转化
    初学Spring
    unittest --- 单元测试
    Python单例模式的两种实现方式
    python records操作数据库
    数据可视化之——matplotlib基础学习
    python使用list维护成一个队列
    Python将list列表维护成一个栈
    Python使用rsa模块实现非对称加密与解密
    python +OpenCV实现rtmp视频流媒体的播放
  • 原文地址:https://www.cnblogs.com/hghua/p/13398298.html
Copyright © 2020-2023  润新知