python_day024 additions and deletions of class attributes

1.类属性的增删改查:

class Chinese: #以下均是属性
    country = "China"
    def __init__(self,name):
        self.name = name

    def play_bsktball(self,bsktball):
        print("%s 正在打 %s"%(self.name,bsktball))
#类查看
print(Chinese.country)

#类中修改数据属性
#类修改
Chinese.country = "ChinaChina"
p1 = Chinese("ALEX") #声明类实例
print(p1.__dict__)
print(p1.country)

#类增加
Chinese.dang = "dang"
print(Chinese.dang)#调用dang的三种方式
print(p1.dang)
print(p1.__dict__) #类中只含有一个属性"name":"ALEX" 实例基于引用,先从自身字典找,其次从类字典中找
print(Chinese.__dict__["dang"])

#类删除
del Chinese.dang
del Chinese.country
print(Chinese.__dict__)

#类中修改函数属性
#增加函数属性
def eat_food(self,food):
    print("%s 正在吃 %s"%(self.name,food))
Chinese.eat = eat_food
print(Chinese.__dict__)
p1.eat("noodle")

#修改函数属性
def test(self,play_bsktball):
    print("修改函数%s中内容" %play_bsktball)

Chinese.play_bsktball = test
p1.play_bsktball("paly_bsktball")

 2.实例属性的增删改查

猜你喜欢

转载自www.cnblogs.com/yuyukun/p/10582583.html