当前位置:  开发笔记 > 编程语言 > 正文

Python:重载构造函数的问题

如何解决《Python:重载构造函数的问题》经验,为你挑选了2个好方法。

警告:我一直在学习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)

有什么想法吗?



1> Paul Fisher..:

我将假设您来自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 "-----------------------------------------------------"


当天的答案.清晰,高效,现实.

2> truppo..:

调用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)

推荐阅读
女女的家_747
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有