标签:obj tar eth 同名 @class delete ssm com 并且
多态
接口重用, 一种接口,多种实现
静态方法@staticmethod
只是名义上归类管理, 实际上在静态方法里访问不了类或实例中的任何属性
类方法@classmethod
只能访问类变量,不能访问实例变量
属性方法@property
把一个方法变成一个静态属性
给参数赋值要重新定义一个同名函数,并且前面加@属性名.setter
import os
# os.system()
# os.mkdir()
class Dog(object):
def __init__(self,name):
self.name = name
@staticmethod #实际上跟类没什么关系了
def eat(self):
print("%s is eating %s" %(self.name,‘dd‘))
def talk(self):
print("%s is talking"% self.name)
d = Dog("ChenRonghua")
d.eat(d)
d.talk()
ChenRonghua is eating dd
ChenRonghua is talking
import os
# os.system()
# os.mkdir()
class Dog(object):
‘‘‘这个类是描述狗这个对象的‘‘‘
def __init__(self,name):
self.name = name
self.__food = None
#@staticmethod #实际上跟类没什么关系了
#@classmethod
@property #attribute
def eat(self):
print("%s is eating %s" %(self.name,self.__food))
@eat.setter
def eat(self,food):
print("set to food:",food)
self.__food = food
@eat.deleter
def eat(self):
del self.__food
print("删完了")
def talk(self):
print("%s is talking"% self.name)
def __call__(self, *args, **kwargs):
print("running call",args,kwargs)
def __str__(self):
return "<obj:%s>" %self.name
#print(Dog.__dict__) #打印类里的所有属性,不包括实例属性
d = Dog("ChenRonghua")
print(d)
# print(d.__dict__) #打印所有实例属性,不包括类属性
# d(1,2,3,name=333)
#Dog("ChenRonghua")()
http://www.cnblogs.com/alex3714/articles/5213184.html
静态方法@staticmethod 属性方法@property
标签:obj tar eth 同名 @class delete ssm com 并且
原文地址:https://www.cnblogs.com/rongye/p/9965717.html