为什么堆栈溢出流除了这个代码? [英] why stack over flow excepton is thrown in this code?

查看:86
本文介绍了为什么堆栈溢出流除了这个代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public int MovieID

        {

           设为

            {

                value = r1.Next(1,10);

                MovieID =值;

            }¥b $ b           得到
            {

               返回MovieID;

            }¥b $ b        }

public int MovieID
        {
            set
            {
                value = r1.Next(1, 10);
                MovieID = value;
            }
            get
            {
                return MovieID;
            }
        }

推荐答案

getter确实"返回MovieID"。这反过来调用MovieID的getter,它调用MovieID的getter ....并且无限期地直到Stack溢出。

The getter does "return MovieID". This in turn calls the getter of MovieID, which calls the getter of MovieID.... and so indefinitely until the Stack overflows.

你可能想写"return movieID"。 (使用小写的m),其中movieID将是一个私有变量,用作属性的后备存储(使用大写字母M)。

You probably meant to write "return movieID" (with a lowercase m), where movieID would be a private variable used as the backing store for the property (with a capital M).

private int movieID;
public int MovieID
         {
             set
             {
                 value = r1.Next(1, 10);
                 movieID = value;
             }
             get
             {
                 return movieID;
             }
         }




编辑:请注意,将值分配给 "" value"在二传手中不是一个好主意。该属性可能无法按预期工作。

Note that assigning a value to "value" in the setter is not a good idea. The property will probably not work as intended.


这篇关于为什么堆栈溢出流除了这个代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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