class Chinese: def __init__(self, name): self.name = name def play_ball(self): print('%s 正在打篮球' % self.name) p1 = Chinese('小白') p1.age = 19 # 实例只有数据属性,没有函数属性 print(p1.__dict__) del p1.age print(p1.__dict__) 输出: {'name': '小白', 'age': 19} {'name': '小白'}
1 class MyData: 2 pass 3 x = 1 4 y = 2 5 MyData.x = 1 6 MyData.y = 2 7 p1 = MyData() 8 print(p1.x) 9 输出: 10 1
1 class Chinese: 2 country = 'china' 3 def __init__(self, name): 4 self.name = name 5 6 def play_ball(self): 7 print('%s 正在打篮球' % self.name) 8 9 10 p1 = Chinese('alex') 11 p1.country = 'japan' 12 print(p1.country) 13 print(Chinese.country) 14 输出: 15 japan 16 china