Class property, classmethod, ataticmethod methods and polymorphism

1. After property 

defines a function of a class as a feature, when the object uses obj.name again, it is impossible to notice that its name is
calculated by executing a function. The use of this feature follows the unified access method. The principle

behind egon.bmi is a function, so it cannot be assigned
class People:
def __init__(self,name):
self.__name=name


@property
def name(self): #obj.name
print('What you are accessing now is Username...')
return self.__name

@name.setter #obj.name='EGON'
def name(self,x):
# print('================ ==',x)
if type(x) is not str:
raise TypeError('name must be str type, silly')
self.__name=x

@name.deleter
def name(self):
# print('I won't let you delete')
del self.__name

obj=People('egon')

print(obj.name)
print(obj.name( ))

print(obj.name)

obj.name='EGON'

print(obj.name)

obj.name=123

del obj.name
obj.name


2. Polymorphism
Polymorphism refers to multiple forms of the same thing
2. Why use polymorphism
? Create a set of unified rules with base classes and force subclasses to follow (implemented with abstract classes), so that you can
directly use the methods under the object without considering the specific type of the object.


3. How Using polymorphism
Polymorphism: you can directly use the methods under the object without considering the specific type of the object
class Cat(Animal):
def eat(self):
print('cat eat')

def drink(self):
print('cat drink')

def run(self):
print('cat run')

def bark(self):
print('喵喵喵')

class Dog(Animal):
def eat(self):
print('dog eat')

def drink(self):
print('dog drink')

def run(self):
print('dog run')

def bark(self):
print('汪汪汪')

class Pig(Animal):
def eat(self):
print('pig eat')

def drink(self):
print('pig drink')

def run(self):
print('pig run')

def bark(self):
print('Hum hum')

c=Cat ()
d=Dog()
p=Pig()

c.bark()
d.bark()
p.bark()

Note:
The abstract base class itself cannot be instantiated

3. Binding method and non-binding method

1 Method:
The function defined inside the class is used by the object by default, and it is bound to the object, which is called the binding method of the
object. The method of binding the object is special:
it should be called by the object, and the object is called , the object will be automatically passed in as the first parameter

. The method bound to the class is special:
it should be called by the class, and when the class is called, the class will be automatically passed in as the first parameter

import settings

class People:
def __init__(self,name,age):
self.name=name
self.age=age

def tell(self):
print('%s:%s' %(self.name,self.age))

@classmethod
def from_conf(cls):
return cls(settings.NAME,settings.AGE)


p=People('egon',19)

print(People.from_conf)
p4=People.from_conf(People)
print(People.from_conf)
p4=People.from_conf()
p4.tell()


2、staticmethod:非绑定方法,就是一个普通函数
#Feature: It is neither bound to a class nor an object, which means that anyone can use it
# Anyone who uses it is an ordinary function, which means that there is no automatic value transfer feature
import settings
import hashlib
import time

class People:
def __init__(self,name,age):
self.uid=self.create_id() self.name
=name
self.age=age

def tell(self):
print('%s: %s: %s' %(self.uid,self.name,self.age))

@classmethod
def from_conf(cls):
return cls(settings.NAME,settings.AGE)

@staticmethod
def create_id():
m=hashlib.md5()
m.update(str(time.clock()).encode('utf-8'))
return m.hexdigest()

obj=People('egon',18)
print(obj.uid,obj.name,obj.age)
obj.tell()

print(obj.create_id)
print(People.create_id)

print(obj.create_id())
print(People.create_id())

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324459512&siteId=291194637