Python面向对象编程(二)

第17,18个:面向对象三大特性之多态
    

1 def func(arg):
2     print(arg)
3 
4 #python支持多种形态(类型)
5 #优点:不需要先声明是什么字符,数字类型
6 #缺点:看查源码的时候不知道是什么类型
7 func(1)
8 func("hk")
9 func([11,22,33,44])


第19,20,21个:类成员之静态字段和普通字段
    

 1 class foo:
 2     #字段(静态字段)属于类
 3     cc =  123
 4     def __init__(self):
 5         #字段(普通字段)属于对象
 6         self.name = 'la'
 7 
 8     def show(self):
 9         print(self.name)
10 
11 obj =  foo()


字段位置及访问规则范例:

 1 class province:
 2     country = "中国"  #静态字段存在类里,在代码加载时已创建
 3     def __init__(self,name):
 4         self.name = name   #普通字段存在于对象里,对象创建后调用时存在于内存中
 5                            #如果country字段存在对象中被反复调用会占用大量的内存资源
 6 #省的属性是中国
 7 hn = province('河南')
 8 hb = province('河北')
 9 sd = province('山东')
10 print(hn.name) #打印河南的名字
11 
12 
13 # 一般情况:自己访问自己的字段,即类访问静态字段、对象访问普通字段
14 #字段访问规则:
15 #   普通字段只能用对象访问
16 #   静态字段用类访问(万不得已才可使用对象访问)
17 print(hn.name)
18 print(province.country)
19 #print(hn.country)


第22,23个:类成员之普通方法和静态方法以及类
    
方法:
    所有的方法属于类
    1、普通方法:至少一个self,对象执行
    2、静态方法:任意参数, 类执行(万不得已才可使用对象访问)
    3、类方法:至少有一个cls,类执行(万不得已才可使用对象访问)
    

 1 class province:
 2     country = "中国"
 3     def __init__(self,name):
 4         self.name = name
 5 
 6     #普通方法,由对象去调用执行(方法属于类)
 7     def show(self):
 8         print(self.name)
 9         return True
10 
11     #静态方法属于类,由类来调用执行。可以没有参数也可以有多个参数
12     @staticmethod
13     def f1(arg1,arg2):
14         print(arg1,arg2)
15 
16     #类方法由类来调用,是静态方法的一种特殊形式,至少有一个参数cls
17     @classmethod
18     def f2(cls):
19         print(cls)
20 
21 hn = province('河南') #创建对象
22 print(hn.show())   #对象调用普通方法
23 
24 province.f1(12,34) #类调用静态方法
25 province.f2() #类调用类方法,打印出类的名称


第24,25,26个:类成员之属性

属性:
  具有方法的写作形式,具有字段的访问形式
  具体方法里做什么都是自己定义的

 1 '''
 2 class pager:
 3   def __init__(self,all_count):
 4     self.all_count = all_count
 5 
 6   def all_pager(self):
 7     a1,a2=divmod(self.all_count,10)
 8     if a2 == 0 :
 9       return a1
10     else:
11       return a1 + 1
12 p = pager(101)
13 print(p.all_pager())
14 '''
15 class pager:
16   def __init__(self,all_count):
17     self.all_count = all_count
18 
19   @property #表示添加了属性,调用的时候就不用加括号
20   def all_pager(self):
21     a1,a2=divmod(self.all_count,10)
22     if a2 == 0 :
23       return a1
24     else:
25       return a1 + 1
26 p = pager(101)
27 print(p.all_pager) #调用all_pager方法取值的时候不用加括号    

-----------------------------------------
属性即可以 获取 又可以 设置 还可以 删除
第一种形式(比较少见):

 1 class pager:
 2   def __init__(self,all_count):
 3     self.all_count = all_count
 4   
 5   @property #表示添加了属性,调用的时候就不用加括号
 6   def all_pager(self):
 7     a1,a2=divmod(self.all_count,10)
 8     if a2 == 0 :
 9       return a1
10     else:
11       return a1 + 1
12 
13   @all_pager.setter
14   def all_pager(self,value):
15     print(value)
16 
17   @all_pager.deleter
18   def all_pager(self):
19     print(123)
20 
21 p = pager(100) #100是和__init__方法里的all_count一致的
22 print(p.all_pager) #获取
23 p.all_pager=111 #设置
24 del p.all_pager #删除

第二种形式(比较常见):

 1 class pager:
 2   def __init__(self,all_count):
 3     self.all_count = all_count
 4 
 5   def f1(self):
 6     return 123
 7   def f2(self,value):
 8     print(value)
 9   def f3(self):
10     print(1234)
11 
12 foo = property(fget=f1,fset=f2,fdel=f3)
13 
14 p = pager(100)
15 print(p.foo) #对应着fget=f1
16 p.foo = "han" #对应着fget=f2
17 del p.foo #对应着fget=f3

第27个:类成员之成员修饰符

成员:字段、方法、属性
两种修饰符:一种私有(只能在类内部访问)
      一种公有

公有修饰符:

 1 class foo:
 2   def __init__(self,name):
 3     self.name = name
 4 
 5   def f1(self):
 6     print(self.name)
 7 
 8 p = foo("hk")
 9 #共有的两种形式:
10 #print(p.name) #1在类的外部直接调用
11 print(p.f1()) #2在类的外部通过方法间接的调用

私有修饰符:

1 class foo:
2   def __init__(self,name):
3     self.__name = name #创建一个私有的字段
4 
5   def f1(self):
6     print(self.__name) #私有的字段
7 
8 p = foo("hk")
9 print(p.f1()) #私有的字段只能在类的内部调用,通过f1方法调用
# print(p._foo__name) #私有的成员修饰符也可以访问,不到万不得已不用!!!

存在类的继承,父类的私有子类是无法获取的。

 1 class foo:
 2     def __init__(self,name):
 3         self.__name = name   #创建一个私有的字段
 4 
 5     def f1(self):
 6         print(self.__name)  #私有的字段
 7 
 8 class bar(foo):
 9     def f2(self):
10         print(self.__name)
11 
12 obj = bar("han")
13 obj.f2() #存在类的继承,私有的成员修饰符也是不可以的。
 1 class foo:
 2     __cc = 123 #静态字段
 3     def __init__(self,name):
 4         self.name = name
 5     def f1(self):
 6         print(self.name)
 7         return 135
 8     def f2(self):
 9         print(foo.__cc) #类名 . 静态字段
10 '''
11     @staticmethod  #静态方法
12     def f3():
13         print(foo.__cc)
14 foo.f3()
15 '''
16 obj = foo('han')
17 print(obj.f2())   #对象执行f2的方法,私有的成员修饰符在类中可以找到并执行
原文地址:https://www.cnblogs.com/hanxiaobei/p/5751414.html