当前位置:K88软件开发文章中心编程语言PythonPython01 → 文章内容

Python3 面向对象

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-10 11:00:20

。多继承的类定义形如下例:


class DerivedClassName(Base1, Base2, Base3):


<statement-1>...<statement-N>需要注意圆括号中父类的顺序,若是父类中有相同的方法名,而在子类使用时未指定,python从左至右搜索即方法在子类中未找到时,从左到右查找父类中是否包含方法。实例(Python 3.0+)


#!/usr/bin/python3


#类定义class people:





#定义基本属性name = ''age = 0


#定义私有属性,私有属性在类外部无法直接进行访问__weight = 0


#定义构造方法def __init__(self,n,a,w):


self.name = nself.age = aself.__weight = wdef speak(self):


print("%s 说:


我 %d 岁。" %(self.name,self.age))


#单继承示例class student(people):


grade = ''def __init__(self,n,a,w,g):





#调用父类的构函people.__init__(self,n,a,w)self.grade = g


#覆写父类的方法def speak(self):


print("%s 说:


我 %d 岁了,我在读 %d 年级"%(self.name,self.age,self.grade))


#另一个类,多重继承之前的准备class speaker():


topic = ''name = ''def __init__(self,n,t):


self.name = nself.topic = tdef speak(self):


print("我叫 %s,我是一个演说家,我演讲的主题是 %s"%(self.name,self.topic))


#多重继承class sample(speaker,student):


a =''def __init__(self,n,a,w,g,t):


student.__init__(self,n,a,w,g)speaker.__init__(self,n,t)test = sample("Tim",25,80,4,"Python")test.speak()


#方法名同,默认调用的是在括号中排前地父类的方法执行以上程序输出结果为:我叫 Tim,我是一个演说家,我演讲的主题是 Python方法重写如果你的父类方法的功能不能满足你的需求,你可以在子类重写你父类的方法,实例如下:实例(Python 3.0+)


#!/usr/bin/python3class Parent:





# 定义父类def myMethod(self):


print ('调用父类方法')class Child(Parent):





# 定义子类def myMethod(self):


print ('调用子类方法')c = Child()


# 子类实例c.myMethod()


# 子类调用重写方法super(Child,c).myMethod()


#用子类对象调用父类已被覆盖的方法super() 函数是用于调用父类(超类)的一个方法。执行以上程序输出结果为:调用子类方法调用父类方法更多文档: Python 子类继承父类构造函数说明 类属性与方法类的私有属性__private_attrs:两个下划线开头,声明该属性为私有,不能在类的外部被使用或直接访问。在类内部的方法中使用时 self.__private_attrs。类的方法在类地内部,使用 def 关键字来定义一个方法,与一般函数定义不同,类方法必须包含参数 self,且为第一个参数,self 代表的是类的实例。self 的名字并不是规定死的,也可以使用 this,但是最好还是按照约定是用 self。类的私有方法__private_method:两个下划线开头,声明该方法为私有方法,只能在类的内部调用 ,不能在类地外部调用。self.__private_methods。实例类的私有属性实例如下:实例(Python 3.0+)


#!/usr/bin/python3class JustCounter:


__secretCount = 0


# 私有变量publicCount = 0


# 公开变量def count(self):


self.__secretCount += 1self.publicCount += 1print (self.__secretCount)counter = JustCounter()counter.count()counter.count()print (counter.publicCount)print (counter.__secretCount)


# 报错,实例不能访问私有变量执行以上程序输出结果为:122Traceback (most recent call last):


File "test.py", line 16, in <module> print (counter.__secretCount)


# 报错,实例不能访问私有变量AttributeError:


'JustCounter' object has no attribute '__secretCount'类的私有方法实例如下:实例(Python 3.0+)


#!/usr/bin/python3class Site:


def __init__(self, name, url):


self.name = name


# publicself.__url = url


# privatedef who(self):


print('name :


', self.name)print('url :


', self.__url)def __foo(self):





# 私有方法print('这是私有方法')def foo(self):





# 公共方法print('这是公共方法')self.__foo()x = Site('k88.net', 'www.k88.net')x.who()


# 正常输出x.foo()


# 正常输出x.__foo()


# 报错以上实例执行结果:类的专有方法:__init__ :


构造函数,在生成对象时调用__del__ :


析构函数,释放对象时使用__repr__ :


打印,转换__setitem__ :


按照索引赋值__getitem__:


按照索引获取值__len__:


获得长度__cmp__:


比较运算__call__:


函数调用__add__:


加运算__sub__:


减运算__mul__:


乘运算__truediv__:


除运算__mod__:


求余运算__pow__:


乘方运算符重载Python同样支持运算符重载,我们可以对类的专有方法进行重载,实例如下:实例(Python 3.0+)


#!/usr/bin/python3class Vector:


def __init__(self, a, b):


self.a = aself.b = bdef __str__(self):


return 'Vector (%d, %d)' % (self.a, self.b)def __add__(self,other):


return Vector(self.a + other.a, self.b + other.b)v1 = Vector(2,10)v2 = Vector(5,-2)print (v1 + v2)以上代码执行结果如下所示:


Vector(7,8)

上一页  [1] [2] 


Python3 面向对象