6

@staticmethod

classDog(object):

def__init__(self, name):

self.name = name

@staticmethod

defeat(self):

print('eatttt,,,,,%s'%self.name)

d = Dog('f')

# d.eat

Dog.eat(d)

静态方法,只是名义上归类管理,实际上在静态方法里访问不了类或者实例的任何属性。

@classmethod

classDog(object):

name ='vv'

def__init__(self, name):

self.name = name

# @staticmethod

@classmethod

defeat(self):

print('eatttt,,,,,%s'%self.name)

d = Dog('f')

d.eat()

类方法只能访问类变量,不能访问实例变量

@property

classDog(object):

def__init__(self, name):

self.name = name

# @staticmethod

# @classmethod

@property

defeat(self):

print('eatttt,,,,,%s'%self.name)

d = Dog('f')

d.eat

属性方法,把一个方法变成属性,不能直接删除该属性(如del d.eat,不可以),可以定义一个@eat.deleter,在这个方法里删除一些这个属性用到的一些私有属性

classDog(object):

def__init__(self, name):

self.name = name

self.__food ='defaultfood'

# @staticmethod

# @classmethod

@property

defeat(self):

print('eatttt,,,,,%s,,,%s'%(self.name,self.__food))

@eat.setter

defeat(self,food):

# print('set to food:', food)

self.__food ='baozi'

@eat.deleter

defeat(self):

delself.__food

print('删除')

d = Dog('f')

d.eat

d.eat ='包子'

d.eat

del d.eat

你可能感兴趣的:(6)