File tree Expand file tree Collapse file tree 10 files changed +242
-0
lines changed Expand file tree Collapse file tree 10 files changed +242
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ 定义类与示例
3
+ """
4
+
5
+
6
+ class Bird :
7
+ """
8
+ 鸟类
9
+ """
10
+ kind = '动物'
11
+ name = "鸟"
12
+
13
+ def __init__ (self , name , wings ):
14
+ self .name = name
15
+ self .wings = wings
16
+
17
+ def can_fly (self , can ):
18
+ print ("%s有 %s 的翅膀,%s飞翔" % (self .name , self .wings , '能' if can else '不能' ))
19
+
20
+
21
+ if __name__ == '__main__' :
22
+ eagle = Bird ("老鹰" , "长而宽阔" )
23
+ duck = Bird ("鸭子" , "较短" )
24
+ eagle .can_fly (True )
25
+ duck .can_fly (False )
26
+
27
+ print ("类访问类变量:%s" % Bird .kind )
28
+ print ("类访问变类量:%s" % Bird .name )
29
+ print ("实例访问类变量:%s" % eagle .kind )
30
+ print ("实例访问同名变量:%s" % duck .name )
Original file line number Diff line number Diff line change
1
+ """
2
+ 组合代码示例
3
+ """
4
+
5
+
6
+ class Book :
7
+ def __init__ (self , num ):
8
+ self .num = num
9
+
10
+
11
+ class Phone :
12
+ def __init__ (self , num ):
13
+ self .num = num
14
+
15
+
16
+ class Wallet :
17
+ def __init__ (self , num ):
18
+ self .num = num
19
+
20
+
21
+ class Bag :
22
+ def __init__ (self , x , y , z ):
23
+ self .book = Book (x )
24
+ self .phone = Phone (y )
25
+ self .wallet = Wallet (z )
26
+
27
+ def show_bag (self ):
28
+ print ("您的背包里有:【书本】* %d 【手机】* %d 【钱包】* %d" %
29
+ (self .book .num , self .phone .num , self .wallet .num ))
30
+
31
+
32
+ if __name__ == '__main__' :
33
+ bag = Bag (3 , 2 , 1 )
34
+ bag .show_bag ()
Original file line number Diff line number Diff line change
1
+ """
2
+ 类函数示例
3
+ """
4
+
5
+
6
+ class A :
7
+ @classmethod
8
+ def fun_a (cls ):
9
+ print (type (cls ), cls )
10
+
11
+
12
+ if __name__ == '__main__' :
13
+ A .fun_a ()
14
+ a = A ()
15
+ a .fun_a ()
Original file line number Diff line number Diff line change
1
+ """
2
+ 成员函数代码示例
3
+ """
4
+
5
+
6
+ class B :
7
+ def fun_b (self ):
8
+ print ("Call fun_b()" )
9
+
10
+ if __name__ == '__main__' :
11
+ b = B ()
12
+ b .fun_b ()
13
+ B .fun_b (b )
Original file line number Diff line number Diff line change
1
+ """
2
+ 静态函数示例
3
+ """
4
+ class C :
5
+ @staticmethod
6
+ def fun_c ():
7
+ print ("Call fun_c()" )
8
+
9
+ if __name__ == '__main__' :
10
+ C .fun_c ()
11
+ c = C ()
12
+ c .fun_c ()
Original file line number Diff line number Diff line change
1
+ """
2
+ 访问控制代码示例
3
+ """
4
+
5
+
6
+ class People :
7
+ sex = 1 # 类属性
8
+ __skill = "敲代码" # 私有类属性,只能类内部访问,外部无法访问
9
+ _hello = 1
10
+
11
+ def speak (self ):
12
+ print ("我是一个人,技能是:%s" % self .__skill , end = '\t ' )
13
+
14
+
15
+ people = People ()
16
+ people .speak ()
17
+ people .sex = - 1
18
+ print ("性别:" + ("男" if people .sex == 1 else "女" ))
19
+ print ("访问私有属性:%s" % people ._People__skill )
Original file line number Diff line number Diff line change
1
+ """
2
+ 类动态绑定属性与函数示例
3
+ """
4
+
5
+
6
+ class A :
7
+ def __init__ (self , id_ ):
8
+ self .id_ = id_
9
+
10
+
11
+ # 定义一个用于动态绑定的函数
12
+ def set_name (self , name ):
13
+ print ("调用了动态绑定的函数" )
14
+ self .name = name
15
+
16
+
17
+ if __name__ == '__main__' :
18
+ # 动态绑定一个属性
19
+ A .kind = "人类"
20
+ # 动态绑定一个函数
21
+ A .set_name = set_name
22
+ a = A (1 )
23
+ # 类访问动态绑定的属性
24
+ print (A .kind )
25
+ # 实例访问动态绑定的属性
26
+ print (a .kind )
27
+ # 类访问动态绑定的函数
28
+ A .set_name (a ,'123' )
29
+ # 实例访问动态绑定的函数
30
+ a .set_name ('321' )
Original file line number Diff line number Diff line change
1
+ """
2
+ 类实例动态绑定属性与函数示例
3
+ """
4
+ from types import MethodType
5
+
6
+
7
+ class B :
8
+ def __init__ (self , id_ ):
9
+ self .id_ = id_
10
+
11
+
12
+ # 定义一个用于动态绑定的函数
13
+ def set_name (self , name ):
14
+ print ("调用了动态绑定的函数" )
15
+ self .name = name
16
+
17
+
18
+ if __name__ == '__main__' :
19
+ b_1 = B ('1' )
20
+ # 动态为实例1绑定一个属性
21
+ b_1 .kind = "人类"
22
+ # 动态为实例1绑定一个函数
23
+ b_1 .set_name = MethodType (set_name , b_1 )
24
+ # 实例1设置动态绑定的属性与函数
25
+ print (b_1 .kind )
26
+ b_1 .set_name ('123' )
27
+
28
+ # 另一个类实例调用动态绑定的属性
29
+ b_2 = B ('2' )
30
+ print (b_2 .kind )
Original file line number Diff line number Diff line change
1
+ """
2
+ 单继承代码示例
3
+ """
4
+
5
+
6
+ class Bird :
7
+ def __init__ (self , name ):
8
+ self .name = name
9
+
10
+ def can_fly (self , can ):
11
+ self .can = can
12
+
13
+ def __str__ (self ):
14
+ return self .name + ("能够" if self .can == True else "不能够" ) + "飞翔。"
15
+
16
+
17
+ class Duck (Bird ):
18
+ # 子类扩展父类中的方法
19
+ def set_color (self , color ):
20
+ self .color = color
21
+
22
+ # 重写父类中里的方法
23
+ def __str__ (self ):
24
+ return self .color + "的" + self .name + ("能够" if self .can == True else "不能够" ) + "飞翔," + "会游泳。"
25
+
26
+ if __name__ == '__main__' :
27
+ duck = Duck ("小鸭子" )
28
+ duck .can_fly (False )
29
+ duck .set_color ("黄色" )
30
+ print (duck )
31
+ # # 实例化一个父类对象
32
+ bird = Bird ('鸟' )
33
+ bird .can_swin ()
Original file line number Diff line number Diff line change
1
+ """
2
+ 多继承代码示例
3
+ """
4
+
5
+
6
+ class A :
7
+ def show_A (self ):
8
+ print ('父类A' )
9
+
10
+
11
+ class B :
12
+ def show_B (self ):
13
+ print ('父类B' )
14
+
15
+
16
+ # 定义一个子类,继承A和B类
17
+ class C (A , B ):
18
+ def show_C (self ):
19
+ print ('子类C' )
20
+
21
+ if __name__ == '__main__' :
22
+ c = C ()
23
+ c .show_A ()
24
+ c .show_B ()
25
+ c .show_C ()
26
+ print (C .__mro__ )
You can’t perform that action at this time.
0 commit comments