四十一、python面向对象二


A、成员:
1.字段:静态字段(每个对象都有同一字段),普通字典(每个对象都有不同的数据)
2.方法:静态方法(无需使用对象的内容),类方法,普通方法(使用对象中的数据)
3.特性:普通特性(将方法伪造成字段)
   通过类去访问的有:静态字段,静态方法,类方法
   通过对象去访问的有:普通字段,类的方法
   自己的成员自己去访问
   静态方法:没有self,前添加@staticmethod,即为静态方法(通过类去访问)
   类方法:比静态方法多一个参数,该参数是为了显示哪个类,前添加@classmethod
   特性:将方法未造成字段来执行,前添加@property

快速判断,类执行,对象执行
self 对象调用
无self 类调用
test01
class Provice:

#静态字段,类中
country="China"

def __init__(self,name):

#普通字段,对象中
self.name=name


#普通方法,类中
def show(self):
print("show")

#静态方法
@staticmethod
def add():
print("加法")

#类方法,多一个参数
@classmethod
def dev(cls):
print("减法",cls)

#特性,将方法未造成字段来执行
@property
def csv(self):
print("csv")

#设置csv中的值,3个csv处名字必须相同
@csv.setter
def csv(self,value):
print(value)
pro=Provice("山东")
print(Provice.country)
print(pro.name)
Provice.add()
print(Provice.dev())
print(pro.csv)
pro.csv="123456"
-------------------------------------------------

  China
  山东
  加法
  减法 <class '__main__.Provice'>
  csv
  123456


B、利用反射导入模块、查找类、创建对象、查找对象中的字

imp=__import__("test01",fromlist=True)
print(imp)

class_name=getattr(imp,"Foo")
print(class_name)

r=class_name('zhangyu')
print(r)
print(getattr(r,'name'))
----------------------------------------

  <module 'test01' from 'F:\myworkspacedirectory\Function\member\test01.py'>
  <class 'test01.Provice'>
  <test01.Provice object at 0x00000247FEFC79E8>
  zhangyu


C、成员修饰符
成员:
字段:静态字段(每个对象都有同一字段),普通字典(每个对象都有不同的数据)
方法:静态方法(无需使用对象的内容),类方法,普通方法(使用对象中的数据)
特性:普通特性(将方法伪造成字段)
两种:共有的,私有的(两个下划线开头,只有自己能使用,派生类也不能访问)

class Provice:

#静态字段,类中
country="China"

#私有字段,供内部使用
__country = "Eglish"

def __init__(self,name,name1):

#普通字段,对象中
self.name=name
#私有的有普通字段
self.__name1=name1

def __add(self):
print("__add")

@staticmethod
def __dev():
print("__dev")

# 普通方法,类中
def show(self):
print("show")
print(Provice.__country)
self.__name1="666"
self.__add()
self.__dev()

@staticmethod
def mm():
Provice.__dev
Provice.__name1 = "6661"

obj=Provice("zhangyu","nnn")
print(Provice.country) #错误情况
#print(Provice.__country)
obj.show()
Provice.mm()
---------------------------------------------

  China
  show
  Eglish
  __add
  __dev


D、类的特殊成员1
__init__
__del__
__call__===>Django
xxx.__dict__:可查看类中的成员

class Foo:
'''
我是类的注释
'''
country="zhongguo"

def __init__(self):
self.name="666"

def __call__(self, *args, **kwargs):
print("call")
return 1

def __getitem__(self, item):
print(item,type(item),"__getitem__")

def __setitem__(self, key, value):
print(key,value,"__setitem__")

def __delitem__(self, key):
print(key,"__delitem__")

def add(self):
print("1")

# r=Foo()()
# print(r)

r=Foo() #------------->执行init
r() #------------------>执行__call__
r['k1'] #------------------>执行__getitem__
r['k2']='666' #------------------>执行__setitem__
del r['xxx'] #------------------>执行__执行__delitem__
r[1:5:2] #2.7执行getslice 3.0执行getitem
r[1:3:3]=[11,22,33,44,55,66] #2.7执行setslice 3.0执行setitem
del r[1:3:3] #2.7执行detslice 3.0执行detitem


print(r.__dict__)
print(Foo.__dict__)
----------------------------------------------

  call
  k1 <class 'str'> __getitem__
  k2 666 __setitem__
  xxx __delitem__
  slice(1, 5, 2) <class 'slice'> __getitem__
  slice(1, 3, 3) [11, 22, 33, 44, 55, 66] __setitem__
  slice(1, 3, 3) __delitem__
  {'name': '666'}

{'__module__': '__main__', '__doc__': ' 我是类的注释 ', 'country': 'zhongguo', '__init__': <function Foo.__init__ at 0x000001C307CB8950>, '__call__': <function Foo.__call__ at 0x000001C307CB89D8>, '__getitem__': <function Foo.__getitem__ at 0x000001C307CB8A60>, '__setitem__': <function Foo.__setitem__ at 0x000001C307CB8AE8>, '__delitem__': <function Foo.__delitem__ at 0x000001C307CB8B70>, 'add': <function Foo.add at 0x000001C307CB8BF8>, '__dict__': <attribute '__dict__' of 'Foo' objects>, '__weakref__': <attribute '__weakref__' of 'Foo' objects>}


E、iter:类的特殊成员2

class Foo:

def __iter__(self):
yield 1
yield 2
yield 3
yield 4


obj=Foo()
#如果执行for对象时,自动会执行对象的iter方法,生成器
for i in obj:
print(i)
----------------------------------------------------
1
2
3
4