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

yipeiwu_com5年前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程序设计有所帮助。

相关文章

python3学生名片管理v2.0版

python3学生名片管理v2.0版

python学生名片管理vv2.0是在1.0的基础上增加部分功能,实现将数据存入文件保存,以便于程序停止后还能再次取到数据。具体实现请看如下部分: card_main.py impo...

python3人脸识别的两种方法

python3人脸识别的两种方法

本文实例为大家分享了python3实现人脸识别的具体代码,供大家参考,具体内容如下 第一种: import cv2 import numpy as np filename = 't...

Tensorflow实现AlexNet卷积神经网络及运算时间评测

本文实例为大家分享了Tensorflow实现AlexNet卷积神经网络的具体实现代码,供大家参考,具体内容如下 之前已经介绍过了AlexNet的网络构建了,这次主要不是为了训练数据,而是...

对python的输出和输出格式详解

对python的输出和输出格式详解

输出 1. 普通的输出 # 打印提示 print('hello world') 用print()在括号中加上字符串,就可以向屏幕上输出指定的文字。比如输出'hello, world...

python 实现检验33品种数据是否是正态分布

我就废话不多说了,直接上代码吧! # -*- coding: utf-8 -*- """ Created on Thu Jun 22 17:03:16 2017 @author: y...