zoukankan      html  css  js  c++  java
  • 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
    .
    =============结束执行测试=============
    
  • 相关阅读:
    jFinal基于maven简单的demo
    quartz定时任务
    cors解决跨越问题
    poi导出excel
    layui富文本编译器后台获取图片路径
    HttpClient短信接口
    js分页
    El表达式日期处理
    【传智播客】Libevent学习笔记(二):创建event_base
    【传智播客】Libevent学习笔记(一):简介和安装
  • 原文地址:https://www.cnblogs.com/hghua/p/13398298.html
Copyright © 2011-2022 走看看