Object-oriented members of the class of 1--
Object-oriented three characteristics: 1. Package 2. 3. polymorphism inheritance
opp can do is break down the code, the code redundancy and minimize the existing code and then write a custom program, rather than modifying the code field, or start from scratch
First, the members of the class:
1. Field: ordinary fields, static fields
2. Methods: General methods, class methods, static methods
3. Properties
class Person:
country = 'CN' #静态字段
__money = 99999 #私有静态字段
def __init__(self,name):
self.name = name #普通字段
self.__age = 30 #私有普通字段
def show(self): #普通方法
self.__think()
return self.__age
@classmethod #类方法
def cls_func(cls):
return cls.__money
@staticmethod
def static_func():
return 123
@property
def nature(self): #属性
return '鸽子'
def __think(self): #私有方法
return '画个圈圈'
Static fields belong to the class, leaving only one in memory
Common fields belong to the object, each object stored in each
Common methods belong to the object, comprising at least one parameter of self, invoked by the object
Class by the class method calls, comprising at least one parameter CLS,
Static class methods are called, no parameters self
Properties There are two forms defined above using a decorator, the following embodiment is a static field
def nature(self):
return '鸽子'
na = property(nature)
Public and private
Private static field, __money = 99999, 错误:Person.__money
can be accessed by indirect methods,
Private field,self.__age = 30,错误:obj.__age
Private method, error: obj .__ think ()
* Ps: have to access private property, you can through the object ._ class __ attribute name * obj._Person__money
self
self usually the name of the first parameter to the method of the class, Python will automatically fill in the instance of an object (that is, implicitly main method call), does not have to be called self, location is the key.
Second, special member (part)
__init__ __str__ __call__ __add__ __dict__ __doc__ __name__
__getitem__ __setitem__ __delitem__
__getattr__ __setattr__
class A:
'''nothing....'''
def __init__(self,data):
self.data = data
def __str__(self,data):
return self.data
def __call__(self):
print('Hellow')
def __add__(self,other):
return self.data + other
__init__
When # constructor, each instance is created, Python will automatically call it, in addition to any parameters passed explicitly name the class, but also invisible incoming new instance.
__str__
When you want to print an object, running__str__
__add__
Objects appear in the "+" expression, will run__add__
__call__
When objects are called, will run__call__
__dict__
All members return a dictionary of all the classes or objects
__doc__
Returning to the description information of the class
__name__
Returns the name of the class
a = A(1)
print(a) # A
a() # Hellow
print(a + 2) # 3
print(a.__dict__) # {'data': 1}
print(a.__doc__) #nothing...
print(a.__class__.__name__) # A
Not defined __dict__
and __doc__
, why not being given? , But also because all the classes inherit the default object;: class A (object) ; redefining overwrites the original function, does not make sense.
class B:
def __init__(self,number):
self.number = number
self.dic = {'k1':1,'k2':2,'k3':3,}
def __getitem__(self, key):
return self.dic[key]
def __setitem__(self, key, value):
self.dic[key] = value
def __delitem__(self, key):
del self.dic[key]
b =B(88)
n = b['k1'] #触发 __getitem__
print(n) # 1
b['k4'] = 4 #触发 __setitem__
print(b.dic) #{'k1': 1, 'k2': 2, 'k3': 3, 'k4': 4}
del b['k2'] #触发__delitem__
print(b.dic) #{'k1': 1, 'k3': 3, 'k4': 4}
class C:
pass
c = C()
c.name = 'Sroxi'
print(c.name) # Sroxi
Class C, without any defined, c.name = 'Sroxi' is how to achieve?
- triggered the object's __setattr__
methods, subclass does not make sense to rewrite the same
class Foo:
def __init__(self):
object.__setattr__(self, 'info', {}) #在对象中设置值的本质,注意:这里info带引号
def __setattr__(self, key, value):
#c.name = 'Sroxi',触发该方法,在object中有字典这样的容器进行接收
self.info[key]=value
def __getattr__(self, item):
#c.name触发该方法,将字典中的对应的value进行返回
return self.info[item]