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
    .
    =============结束执行测试=============
    
  • 相关阅读:
    java中获取类资源的不同姿势
    Jdk动态代理与Cglib动态代理
    Spring AOP
    Spring Ioc、DI
    windows 8.1 专业版 visual stuido 2015 安装失败
    win7 共享需要密码问题
    protobuffer .net 序列化
    【转载】SQL Server 2005数据库用户权限管理的设置
    【转载】mongoDB基本使用手册
    【转载】Thread.sleep(0)的意义
  • 原文地址:https://www.cnblogs.com/hghua/p/13398298.html
Copyright © 2011-2022 走看看