Binding method:
Method Object Binding Binding Method / Class
Binding method: special, who is who is going to bind to the tune, and will pass over itself
Binding methods of the class: binding to class, call the class, the class itself will pass over
Binding class methods used in any place?
- without going through the object, only need to be able to get something through the class when the class of binding method
- binding method class may be adjusted by the object
'''
类中使用@classmethod修饰的方法就是绑定到类的方法,这类方法专门为类定制,通过类名调用绑定到类的方法时,会将类本身当做参数传给类方法的第一个参数
'''
class Operate_database():
host='192.168.0.5'
port='3306'
user='lzs'
password='1234'
@classmethod
def connect(cls):##约定俗成第一个参数名为cls,也可以定义为其他参数名
print(cls)
print(cls.host+":"+cls.port+' '+cls.user+'/'+cls.password)
Operate_database.connect()
##<class '__main__.Operate_database'>
##192.168.0.5:3306 abc/123456
Unbound methods:
@Staticmethod modified using internal methods in the class is the non-binding method, and ordinary methods such defined functions without distinction, not tied to an object or class, anyone can call, automatically and without the effect of traditional values
import hashlib
class Operate_database():
def __init__(self,host,port,user,password):
self.host=host
self.port=port
self.user=user
self.password=password
@staticmethod
def get_password(salt,password):
m=hashlib.md5(salt.encode('utf=8')) ##加盐处理
m.update(password.encode('utf-8'))
return m.hexdigest()
hash_password=Operate_database.get_password('lala','123456') ##通过类来调用
print(hash_password)
####f7a1cc409ed6f51058c2b4a94a7e1956
p=Operate_database('192.168.0.5','3306','abc','123456')
hash_password=p.get_password(p.user,p.password) ##也可以通过对象调用
print(hash_password)
Derived class:
Inherited his father's class at the same time they have init, then the parent class init also need
Class combinations:
You can refer to the class object / passing as a parameter / value as return / as a container element, similar to the function object
Diamond inheritance problem:
The new class: inherited object classes, python3 in all new class
Classic: not inherit object's class, there are only python2
In the diamond inheritance when the breadth of the new class of priority (the apex of the tree finally found); Classic depth-first (in the end find a way, find in the end, find next)
Polymorphism and polymorphism:
A thing in a variety of forms, animal --- "human / pig / dog
##多态
import abc
class Animal(metaclass=abc.ABCmeta):
@abc.abstractmethod
def eat():
print('eat')
class People(Animal):
def eat():
pass
class Pig(Animal):
def eat():
pass
def run():###报错,因为规定了要有eat()属性
def run():
pass
###多态性
peo=People()
peo.eat()
peo1=People()
peo1.eat()
pig=Pig()
pig.eat()
def func(obj):
obj.eat()
class Cat(Animal):
def eat()
pass
cat=Cat()
func(cat)
Ducks type:
Just looks like a duck, called like a duck, swim like a duck, is a duck
Class Package:
Hidden property, only inside the class can be accessed outside the class can not access
class Foo():
__count=0
def get_count(self):
return self._count
f=Foo()
f.__count ##报错
f._Foo__count ##不能这样做
property attribute class
The method becomes property reference
class People():
def __init__(self,height,weight):
self.height=height
self.weight=weight
@property
def bmi(self):
return weight/(height**2)
@bmi.setter
def bmi(self,value)
print('setter')
@bmi.deleter
def bmi(self):
print('delter')
peo=People
peo.bmi
Binding method classes and objects and non-binding method:
There is no way decorator decorative object is bound methods, classes can call, but must pass a reference to self
Garnished with @classmethod decorator class method is a method of binding parameters write cls, cls is the class itself, an object can be called, or parameters cls class itself
Garnished with @staticmethod decorator method is non-binding approaches, is an ordinary