各位用户为了找寻关于python类和继承用法实例的资料费劲了很多周折。这里教程网为您整理了关于python类和继承用法实例的相关资料,仅供查阅,以下为您介绍关于python类和继承用法实例的详细内容
本文实例讲述了python类和继承定义与用法。分享给大家供大家参考。具体如下:
? 1 2 3 4 5class
Employee:
pass
lee
=
Employee()
lee.name
=
'leefang'
lee.age
=
28
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class
cAA:
i
=
8
def
__init__(
self
):
self
.__a
=
7
self
.m_a
=
1
self
.m_b
=
2
def
__Method1(
self
):
self
.__a
+
=
1
return
self
.__a
def
Method2(
self
, _x):
print
self
.__Method1(), _x
class
cCC:
def
Method2(
self
,_x):
print
'aaaaaaaaa'
class
cBB(cCC, cAA):
def
kk(
self
, _x):
self
.Method2(_x)
def
tt(
self
):
print
self
.m_a
a
=
cAA()
a.Method2(
3
)
print
a.m_a
b
=
cBB()
b.Method2(
4
)
b.kk(
'ffffff'
)
b.tt()
类继承如下:
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30#!/usr/bin/python
class
Oneclass:
def
setdata(
self
,value):
self
.data
=
value
def
display(
self
):
print
self
.data
class
Twoclass(Oneclass):
def
display(
self
):
print
"current value is %s"
%
self
.data
class
Threeclass(Twoclass):
def
__init__(
self
,value):
self
.data
=
value
def
__add__(
self
,other1):
self
.data
=
self
.data
+
other1
def
__mul__(
self
,other2):
self
.data
=
self
.data
*
other2
object1
=
Oneclass()
object1.setdata(
1234
)
object1.display()
object2
=
Twoclass()
object2.setdata(
1234
)
object2.display()
object3
=
Threeclass(
12
)
object3.display()
object3.setdata(
1234
)
object3.display()
object3
+
22
object3.display()
object3
*
10
object3.display()
希望本文所述对大家的Python程序设计有所帮助。