警告:我一直在学习Python 10分钟,所以对任何愚蠢的问题道歉!
我编写了以下代码,但是我得到以下异常:
消息文件 名称行位置回溯节点 31 exceptions.TypeError:此构造函数不带参数
class Computer:
name = "Computer1"
ip = "0.0.0.0"
screenSize = 17
def Computer(compName, compIp, compScreenSize):
name = compName
ip = compIp
screenSize = compScreenSize
printStats()
return
def Computer():
printStats()
return
def printStats():
print "Computer Statistics: --------------------------------"
print "Name:" + name
print "IP:" + ip
print "ScreenSize:" , screenSize // cannot concatenate 'str' and 'tuple' objects
print "-----------------------------------------------------"
return
comp1 = Computer()
comp2 = Computer("The best computer in the world", "27.1.0.128",22)
有什么想法吗?
答案 0 :(得分:36)
我将假设您来自Java-ish背景,因此需要指出几个关键点。
class Computer(object):
"""Docstrings are used kind of like Javadoc to document classes and
members. They are the first thing inside a class or method.
You probably want to extend object, to make it a "new-style" class.
There are reasons for this that are a bit complex to explain."""
# everything down here is a static variable, unlike in Java or C# where
# declarations here are for what members a class has. All instance
# variables in Python are dynamic, unless you specifically tell Python
# otherwise.
defaultName = "belinda"
defaultRes = (1024, 768)
defaultIP = "192.168.5.307"
def __init__(self, name=defaultName, resolution=defaultRes, ip=defaultIP):
"""Constructors in Python are called __init__. Methods with names
like __something__ often have special significance to the Python
interpreter.
The first argument to any class method is a reference to the current
object, called "self" by convention.
You can use default function arguments instead of function
overloading."""
self.name = name
self.resolution = resolution
self.ip = ip
# and so on
def printStats(self):
"""You could instead use a __str__(self, ...) function to return this
string. Then you could simply do "print(str(computer))" if you wanted
to."""
print "Computer Statistics: --------------------------------"
print "Name:" + self.name
print "IP:" + self.ip
print "ScreenSize:" , self.resolution //cannot concatenate 'str' and 'tuple' objects
print "-----------------------------------------------------"
答案 1 :(得分:5)
Python中的构造函数称为__init__
。您还必须使用“self”作为类中所有方法的第一个参数,并使用它来设置类中的实例变量。
class Computer:
def __init__(self, compName = "Computer1", compIp = "0.0.0.0", compScreenSize = 22):
self.name = compName
self.ip = compIp
self.screenSize = compScreenSize
self.printStats()
def printStats(self):
print "Computer Statistics: --------------------------------"
print "Name:", self.name
print "IP:", self.ip
print "ScreenSize:", self.screenSize
print "-----------------------------------------------------"
comp1 = Computer()
comp2 = Computer("The best computer in the world", "27.1.0.128",22)
答案 2 :(得分:4)
答案 3 :(得分:2)
首先,请查看here。
答案 4 :(得分:2)
有许多事情需要指出:
__init__
。<强> C ++:强>
class comp {
std::string m_name;
foo(std::string name);
};
foo::foo(std::string name) : m_name(name) {}
<强>的Python:强>
class comp:
def __init__(self, name=None):
if name: self.name = name
else: self.name = 'defaultName'
答案 5 :(得分:1)
这不是有效的python。
Python类的构造函数是def __init__(self, ...):
,你不能重载它。
您可以做的是使用参数的默认值,例如
class Computer:
def __init__(self, compName="Computer1", compIp="0.0.0.0", compScreenSize=17):
self.name = compName
self.ip = compIp
self.screenSize = compScreenSize
self.printStats()
return
def printStats(self):
print "Computer Statistics: --------------------------------"
print "Name : %s" % self.name
print "IP : %s" % self.ip
print "ScreenSize: %s" % self.screenSize
print "-----------------------------------------------------"
return
comp1 = Computer()
comp2 = Computer("The best computer in the world", "27.1.0.128",22)
答案 6 :(得分:1)
啊,这些是新python开发人员的常见问题。
首先,应该调用构造函数:
__init__()
您的第二个问题是忘记将self参数包含在您的类方法中。
此外,当您定义第二个构造函数时,您将替换Computer()方法的定义。 Python非常动态,可以让你快乐地重新定义类方法。
如果你不想让它们成为必需的话,pythonic方法可能会使用默认值。
答案 7 :(得分:1)
Python不支持函数重载。