python定义类对象的问题
classMessage:def_init_(self,aString):self.text=aStringdefprintIt(self):printself.text...
class Message:
def _init_ (self,aString):
self.text = aString
def printIt(self):
print self.text
m1 = Message("Hello world")
m2=Message("So long,it was short but sweet")
note=[m1,m2]#把变量放到链表里
for msg in note:
msg.printIt()#依次打印每条信息
提示 m1 = Message("Hello world")
有错 怎么回事 求指教 展开
def _init_ (self,aString):
self.text = aString
def printIt(self):
print self.text
m1 = Message("Hello world")
m2=Message("So long,it was short but sweet")
note=[m1,m2]#把变量放到链表里
for msg in note:
msg.printIt()#依次打印每条信息
提示 m1 = Message("Hello world")
有错 怎么回事 求指教 展开
1个回答
展开全部
你的代码似乎是"_init_",其实应该是"__init__" -- 前后双下划线
#!/usr/bin/python
class Message:
def __init__ (self,aString):
self.text = aString
def printIt(self):
print(self.text)
m1 = Message("Hello world")
m2 = Message("So long,it was short but sweet")
note = [m1,m2]
for msg in note:
msg.printIt()
>python -u "test.py"
Hello world
So long,it was short but sweet
>Exit code: 0 Time: 0.494
#!/usr/bin/python
class Message:
def __init__ (self,aString):
self.text = aString
def printIt(self):
print(self.text)
m1 = Message("Hello world")
m2 = Message("So long,it was short but sweet")
note = [m1,m2]
for msg in note:
msg.printIt()
>python -u "test.py"
Hello world
So long,it was short but sweet
>Exit code: 0 Time: 0.494
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询