Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ';超过最大递归深度';每当我做OOP_Python_Oop_Python 2.7_Recursion - Fatal编程技术网

Python ';超过最大递归深度';每当我做OOP

Python ';超过最大递归深度';每当我做OOP,python,oop,python-2.7,recursion,Python,Oop,Python 2.7,Recursion,我正在尝试我的第一个OOP问题。我教科书中的问题是: 编写一个程序,通过将电视创建为对象来模拟电视。允许用户增加/减少音量,并设置频道号。确保频道和音量值保持在有效范围内 我的问题是: 我的结构大体正确吗?(因为我选择了inc/dec音量等的方法) 为什么会出现递归错误?我猜这意味着我处于某种无限循环中,但我绝对看不到它 我未完成的“解决方案”: File "C:\tv.py", line 20, in set_channel self.channel = new_channel R

我正在尝试我的第一个OOP问题。我教科书中的问题是:

编写一个程序,通过将电视创建为对象来模拟电视。允许用户增加/减少音量,并设置频道号。确保频道和音量值保持在有效范围内

我的问题是:

  • 我的结构大体正确吗?(因为我选择了inc/dec音量等的方法)

  • 为什么会出现递归错误?我猜这意味着我处于某种无限循环中,但我绝对看不到它

  • 我未完成的“解决方案”:

      File "C:\tv.py", line 20, in set_channel
        self.channel = new_channel
    RuntimeError: maximum recursion depth exceeded
    
    #带课堂的电视
    #用户可以输入频道号
    #和增加/减少音量
    类别电视(对象):
    “电视机。”
    def _;初始化(自,通道=1,卷=20):
    self.channel=channel
    self.volume=体积
    def get_通道(自):
    “”“返回当前通道。”“”
    返回自我通道
    def设置_通道(自身、新_通道):
    “”“设置频道。”“”
    如果1<新频道>5:
    打印“无效通道。必须介于1-5之间。”
    其他:
    self.channel=新频道
    通道=属性(获取通道、设置通道)
    def显示_音量(自身):
    “”“返回当前卷。”“”
    返回自卷
    def inc_卷(自身):
    “”“将音量增加1个单位。”“”
    如果self.volume>=20:
    打印“最大卷数:”,获取卷数“\n”
    其他:
    自卷+=1
    def dec_体积(自身):
    “”“将音量减少1个单位。”“”
    
    如果self.volume您的属性与您试图使用该属性访问的值具有相同的名称。因此,当您访问它时,它会递归地尝试获取它自己的值

    重命名属性应该可以解决此问题

    将代码更改为如下所示:

    # Television with Classes
    # User able to enter a channel number
    # and increase/decrease the volume
    
    class Television(object):
        """A TV set."""
        def __init__(self, channel = 1, volume = 20):
            self.channel = channel
            self.volume = volume
    
        def get_channel(self):
            """Return current channel."""
            return self.channel
    
        def set_channel(self, new_channel):
            """Set the channel."""
            if 1 < new_channel > 5:
                print "Invalid Channel. Must be between 1-5."
            else:
                self.channel = new_channel
    
        channel = property(get_channel, set_channel)
    
        def show_volume(self):
            """Return current volume."""
            return self.volume
    
        def inc_volume(self):
            """Increase the volume by 1 unit."""
            if self.volume >= 20:
                print "Volume is at maximum: ", get_volume, "\n"
            else:
                self.volume += 1
    
        def dec_volume(self):
            """Decrease the volume by 1 unit."""
            if self.volume <= 0:
                print "Volume is at minimum: ", get_volume, "\n"
            else:
                self.volume -= 1
    
    
    sony = Television()
    print "Current channel: ", sony.channel
    print "Changing channel to 3..."
    sony.channel(3)
    print "Current channel: ", sony.channel
    print "Current volume: ", self.volume
    print "Increasing volume..."
    sony.inc_volume()
    print "Current volume: ", self.volume
    
    raw_input("\n\nPress enter to exit.")
    
    将阻止递归发生。您将使用
    Channel
    作为属性(因此调用此属性),它使用get/set\u Channel方法访问
    Channel
    字段

    对该属性的分配也不适用于
    sony.channel(3)
    。您必须像其他任何值一样实际指定属性:

    channel = 0
    Channel = property(get_channel, set_channel)
    
    或者(如果您已经更改了属性问题)

    不过,这并不能解决您稍后遇到的卷问题
    self.volume
    未声明为字段(或属性),inc/dec\u volume函数中的
    get\u volume
    也是如此。

    问题在于:

    sony.Channel = 3
    
    def get_通道(自):
    “”“返回当前通道。”“”
    返回自我通道
    def设置_通道(自身、新_通道):
    “”“设置频道。”“”
    如果1<新频道>5:
    打印“无效通道。必须介于1-5之间。”
    其他:
    self.channel=新频道
    通道=属性(获取通道、设置通道)
    
    self.channel的每个请求都运行get_channel(),但是get_channel()请求self.channel,依此类推

    为了避免这种情况,您需要创建helper变量,该变量将存储self.channel的实际值

    def get_channel(self):
        """Return current channel."""
        return self.channel
    
    def set_channel(self, new_channel):
        """Set the channel."""
        if 1 < new_channel > 5:
            print "Invalid Channel. Must be between 1-5."
        else:
            self.channel = new_channel
    
    channel = property(get_channel, set_channel)
    
    self.\uuu channel=None#self.channel真正存储的字段
    def get_通道(自):
    “”“返回当前通道。”“”
    返回自我通道
    def设置_通道(自身、新_通道):
    “”“设置频道。”“”
    如果1<新频道>5:
    打印“无效通道。必须介于1-5之间。”
    其他:
    self.\uuuu通道=新的\u通道
    通道=属性(获取通道、设置通道)#公共接口
    
    你的“获取音量”方法在哪里?@SlavenTomac我还没有做过。我认为不需要它。你应该在哪一行添加一些代码,把它改成什么。得到我的⁺无论如何:)@Alfe你是怎么写上标的
    +1
    ?:)@第四种方法允许基本的HTML。有一个“sup”标记来生成上标文本。@thefourtheye我正在使用键盘上的compose键来完成这个任务。[撰写][^][+]和[撰写][^][1]。看起来是这样的:⁺1对+1(这并不像你看到的那样有效)。
    def get_channel(self):
        """Return current channel."""
        return self.channel
    
    def set_channel(self, new_channel):
        """Set the channel."""
        if 1 < new_channel > 5:
            print "Invalid Channel. Must be between 1-5."
        else:
            self.channel = new_channel
    
    channel = property(get_channel, set_channel)
    
    self.__channel = None # Field where self.channel will be really stored
    
    def get_channel(self):
        """Return current channel."""
        return self.__channel
    
    def set_channel(self, new_channel):
        """Set the channel."""
        if 1 < new_channel > 5:
            print "Invalid Channel. Must be between 1-5."
        else:
            self.__channel = new_channel
    
    channel = property(get_channel, set_channel) # Public interface