• python类的继承和多态,获取对象信息


    继承


    类的继承机制使得子类可以继承父类中定义的方法,拥有父类的财产,比如有一个Animal的类作为父类,它有一个eat方法:

    class Animal(object):
        
        def __init__(self):
            print("Animal 构造函数调用!")
            
        def eat(self):
            print("Animal is eatting!")

    写两个子类,Cat和Dog类,继承自Animal类,声明方法是在定义子类的时候在子类的括号内写上父类Animal:


    class Animal(object):
        
        def __init__(self):
            print("Animal 构造函数调用!")
            
        def eat(self):
            print("Animal is eatting!")
            
    class Cat(Animal):
        
        def __init__(self):
            print("Cat 构造函数调用!")  
            
    class Dog(Animal):
        
        def __init__(self,age):
            self.age=age
            print("Dog 构造函数调用!")

    两个子类中并没有声明任何方法,但是会自动继承父类中的eat方法:


    cat=Cat()
    dog=Dog(3)
    
    cat.eat()
    dog.eat()

    声明两个对象,调用eat方法,运行输出:


    Cat 构造函数调用!
    Dog 构造函数调用!
    Animal is eatting!
    Animal is eatting!

    一般把一些共有的方法定义在基类中,其他继承自该基类的子类就可以自动拥有这个方法。


    多态


    在继承的基础上,就引入了类的另外一个重要的特性——多态。

    考虑一个问题,子类可以继承父类的方法,那子类是否可以实现自己的这个方法呢,答案是可以的。


    class Animal(object):
        
        def __init__(self):
            print("Animal 构造函数调用!")
            
        def eat(self):
            print("Animal is eatting!")
            
    class Cat(Animal):
        
        def __init__(self):
            print("Cat 构造函数调用!")
            
        def eat(self):
            print("Cat is eatting!")
            
    class Dog(Animal):
        
        def __init__(self,age):
            self.age=age
            print("Dog 构造函数调用!")
            
        def eat(self):
            print("年龄是"+str(self.age)+"岁的Dog is eatting!")
            
    cat =Cat()
    cat.eat()
    
    dog=Dog(3)
    dog.eat()

    子类如果也定义了自己的实现,就会优先调用自己的实现,上边cat和dog调用eat方法就分别是自己的实现,运行输出:

    Cat 构造函数调用!
    Cat is eatting!
    Dog 构造函数调用!
    年龄是3岁的Dog is eatting!

    多态意味着一个接口,多种实现,另一个可以体现类的多态这种特性的例子:


    def eat(animal):
        if hasattr(animal,'eat'):
            animal.eat()
        if hasattr(animal,'age'):
            a=getattr(animal,'age')
            print('animal的年龄是'+str(a)+'岁')
                
    eat(dog)

    这里定义了一个普通函数eat,函数的入参是类的对象,具体实现是调用传入的对象的eat方法,传入不同的对象,就有不同的输出,调用的时候只要调用这个接口就可以了,而不用管具体的细节。

    运行输出:

    年龄是3岁的Dog is eatting!
    animal的年龄是3岁


    获取对象信息


    hasattr(object , 'name')


    说明:判断对象object是否包含名为name的属性或方法,如果有则返回True,没有则返回False


    getattr( object, 'name')


    说明:获取对象object中名称为name的属性,返回name的值。


    对类中方法的调用,可以先用hasattr判断是否存在该方法,然后再调用这个方法,避免异常:


    class Animal(object):
        
        def __init__(self):
            print("Animal 构造函数调用!")
            
        def eat(self):
            print("Animal is eatting!")
     
    def eat(animal):
        
        if hasattr(animal,'eat'):
            animal.eat()
        if hasattr(animal,'age'):
            a=getattr(animal,'age')
            print('animal的年龄是'+str(a)+'岁')
            
        if hasattr(animal, 'sleep'):
            animal.sleep()
        else:
            print('animal类中不含有sleep方法!')
    
    animal=Animal()        
    eat(animal)

    运行输出:

    Animal 构造函数调用!
    Animal is eatting!
    animal类中不含有sleep方法!

  • 相关阅读:
    GraphX学习笔记——Programming Guide
    GraphX学习笔记——可视化
    Gephi学习笔记
    Ubuntu16.04安装apache-airflow
    Centos7.0下MySQL的安装
    同时安装anaconda2和anaconda3
    Hive学习笔记——安装和内部表CRUD
    Python爬虫学习——布隆过滤器
    Ubuntu下安装和使用zookeeper和kafka
    Ubuntu16.04安装xgboost
  • 原文地址:https://www.cnblogs.com/mtcnn/p/9411834.html
Copyright © 2020-2023  润新知