Python类的继承、多态及获取对象信息操作详解

yipeiwu_com6年前Python基础

本文实例讲述了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方法!

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python面向对象程序设计入门与进阶教程》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python编码操作技巧总结》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

Python logging设置和logger解析

Python logging设置和logger解析

一、logging模块讲解 1.函数:logging.basicConfig() 参数讲解: (1)level代表高于或者等于这个值时,那么我们才会记录这条日志 (2)filename代...

python+openCV调用摄像头拍摄和处理图片的实现

在深度学习过程中想做手势识别相关应用,需要大量采集手势图片进行训练,作为一个懒人当然希望飞快的连续采集图片并且采集到的图片就已经被处理成统一格式的啦。。于是使用python+openCV...

Python3基于sax解析xml操作示例

Python3基于sax解析xml操作示例

本文实例讲述了Python3基于sax解析xml操作。分享给大家供大家参考,具体如下: python使用SAX解析xml SAX是一种基于事件驱动的API。 利用SAX解析XML文档牵涉...

Python pandas RFM模型应用实例详解

Python pandas RFM模型应用实例详解

本文实例讲述了Python pandas RFM模型应用。分享给大家供大家参考,具体如下: 什么是RFM模型 根据美国数据库营销研究所Arthur Hughes的研究,客户数据库中有3个...

简单介绍Python的轻便web框架Bottle

基本映射 映射使用在根据不同URLs请求来产生相对应的返回内容.Bottle使用route() 修饰器来实现映射. from bottle import route, run@rou...