Python:构造函数重载的问题 [英] Python: Problem with overloaded constructors

查看:411
本文介绍了Python:构造函数重载的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

警告:我已经学习了整整10分钟的Python,因此对任何愚蠢的问题都深表歉意!

WARNING: I have been learning Python for all of 10 minutes so apologies for any stupid questions!

我已经编写了以下代码,但是出现以下异常:

I have written the following code, however I get the following exception:


消息文件
名称行位置回溯节点
31
exception.TypeError:此构造方法需要没有参数

Message File Name Line Position Traceback Node 31 exceptions.TypeError: this constructor takes no arguments



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)

有什么想法吗?

推荐答案

我将假设您要来

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 "-----------------------------------------------------"

这篇关于Python:构造函数重载的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆