|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- class Fruit:
- price=0 #类属性
- def __init__(self):
- self.color='red' #实例属性
- country="China" #局部变量
- if __name__=="__main__":
- print (Fruit.price)
- apple=Fruit()
- print(apple.color)
- Fruit.price=Fruit.price+15
- print ("apple's price:"+str(apple.price))
- banana=Fruit()
- print ("banana's price:"+str(banana.price))
复制代码
这段能正常运行
- 0
- red
- apple's price:15
- banana's price:15
复制代码
- class Fruit:
- price = 0
- def __int__ (self):
- self.color = 'red'
- country = 'China'
- if __name__ == '__main__':
- print(Fruit.price)
- apple = Fruit()
- print(apple.color)
- Fruit.price = Fruit.price + 15
- print('apple\'s price:' + str(apple.price))
- banana = Fruit()
- print('banana\'s price:' + str(banana.price))
复制代码
这段提示出问题
- 0
- Traceback (most recent call last):
- File "class__attribute.py", line 13, in <module>
- print(apple.color)
- AttributeError: 'Fruit' object has no attribute 'color'
复制代码
|
|