码迷,mamicode.com
首页 > 其他好文 > 详细

属性方法

时间:2018-03-01 00:33:11      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:method   dict   ssm   running   body   attr   war   elf   sel   

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("AAA")
print(d)
# print(d.__dict__) #打印所有实例属性,不包括类属性
# d(1,2,3,name=333)

属性方法

标签:method   dict   ssm   running   body   attr   war   elf   sel   

原文地址:https://www.cnblogs.com/smlie/p/8486259.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!