python-3.x - AttributeError:“str”对象没有属性“describe”
问题描述
class Computer:
def _inti_(self, storage, color , system):
no_of_Computer = 0
self.storage = storage
self.color = color
self.system = system
Computer.no_of_Computer +=1
def describe (self):
print(f'my storage is {self.storage} and my color is{self.color} and my system is {self.system}')
Computer_1 = ("1TB , silver , windows ")
Computer_2 = (" 4TB , black , linux")
Computer_3 = (" 9TB , white ,mac ")
Computer_1.describe()
解决方案
Computer_1
,Computer_2
并且Computer_3
不是Computer
实例,它们只是字符串(用括号括起来)。您需要调用Compueter
的构造函数来创建它的新实例。另外,请注意每个参数应该是它自己的字符串,而不是一个带有逗号的字符串。此外,请注意构造函数是由方法定义的__init__
(注意双取消划线),而不是_inti_
:
class Computer:
def __init__(self, storage, color , system):
no_of_Computer = 0
self.storage = storage
self.color = color
self.system = system
Computer.no_of_Computer +=1
def describe (self):
print(f'my storage is {self.storage} and my color is{self.color} and my system is {self.system}')
Computer_1 = Computer("1TB", "silver", "windows")
Computer_2 = Computer("4TB", "black", "linux")
Computer_3 = Computer("9TB", "white", "mac")
Computer_1.describe()
推荐阅读
- java - No such file or directory - Exception in thread "main" java.io.FileNotFoundException
- postgresql - Select an object with different assignation in another column
- ios - Swift - 有没有办法触发程序断点在停止后继续
- arrays - What's the best way to count occurences of positive, negative, and 0 values in an unsorted array?
- ms-word - 如何为microsoft office 开发插件
- c# - 如何绑定到 WPF MVVM 中的上下文 MenuItem 标头?
- selenium - Jenkins - Timed out connecting to Chrome
- python - 为达到最大连接限制而引发的建议异常类型
- kubernetes - Reset / Rollback Kubernetes to just create state?
- c# - App Center push 在 Postman 中有效,但在我的 C# 应用程序中无效