如何打印e1和e2值。它不会返回任何值。
class Employee:
def __init__(self,name,age):
self.name=name
self.age=age
e1=Employee("xyz",'25')
e2=Employee("abc",'23')
print("Employee Details...")
print("Name:",e1.name,"age:",e1.age)
print("Name:",e2.name,"age:",e2.age)
return
t=Employee()
print t
答案 0 :(得分:1)
您应该重新定义__init__
方法。那是因为在对象初始化时调用了__init__
。
class Employee:
def __init__(self, name,age):
self.name = name
self.age=age
e1=Employee("xyz",'25')
e2=Employee("abc",'23')
print("Employee Details...")
print("Name:",e1.name,"age:",e1.age)
print("Name:",e2.name,"age:",e2.age)
编辑:
您可以使用此代码通过一些更新来实现所需的输出。
您可以在,
之后添加逗号print
,以允许下一个print
语句从同一行开始,更改代码:
class Employee:
def __init__(self, name,age):
self.name = name
self.age=age
e1=Employee("xyz",'25')
e2=Employee("abc",'23')
print("Employee Details:"),
print("Name:",e1.name,"age:",e1.age),
print("Name:",e2.name,"age:",e2.age)
输出:
Employee Details: (' Name:', 'xyz', 'age:'25') (' Name:', 'abc', 'age:'23')
答案 1 :(得分:1)
您要做的事情似乎是重新发明__str__
方法。以下是您如何做到这一点的建议:
class Employee:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return "Name: {0}, age: {1}".format(self.name, self.age)
t = Employee("uday", 25)
print t
我在这里做了一些假设,并试图猜测你的实际目标。 我希望它仍有帮助。