using System;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            Myclass myclass = new Myclass();
            myclass.Myint = 1;
            Console.WriteLine(myclass.Myint);
            myclass.Myint = -1;
            Console.WriteLine(myclass.Myint);
            Console.ReadLine();
        }
    }
    class Myclass
    {
        int myint;
        public int Myint
        {
            get 
            {
                return myint;
            }
            set 
            {
                if (value > 0)
                {
                    myint = value;
                }
                else
                {
                    Console.WriteLine("myint字段只接受大于0的值!");
                }
            }
        }
    }
}-------------------------------------------------------------------------------------
输出是:
1
myint字段只接受大于0的值!
1
------------------------------------------------------------------------------------1
myint字段只接受大于0的值!
 
这两个输出好理解 最后一个输出1   怎么解释?从哪里来的?

解决方案 »

  1.   

    myclass.Myint = -1;并没有改变myclass.Myint 的值,所以还是1,你又输出了一次
      

  2.   

    class Myclass
        {
            int myint;
            public int Myint
            {
                get
                {
                    return myint;
                }
                set
                {
                    if (value > 0)
                    {
                        myint = value;
                    }
                    else
                    {
                        myint = 999999999;
                        Console.WriteLine("myint字段只接受大于0的值!");
                    }
                }
            }
        }
      

  3.   

    Console.WriteLine(myclass.Myint); 这就是输出第三行的1呀
      

  4.   

    类是按地址传递的,在给属性赋值的时候如果小于零myclass.Myint的值并没有变化,你又输出了一次
      

  5.   

    Console.WriteLine(myclass.Myint); 
      

  6.   

       class   Program 
            { 
                    static   void   Main(string[]   args) 
                    { 
                            Myclass   myclass   =   new   Myclass(); 
                            myclass.Myint   =   1; 
                            Console.WriteLine(myclass.Myint); 1  
                            myclass.Myint   =   -1; 
                            Console.WriteLine(myclass.Myint); 3
                           Console.ReadLine(); 
                    } 
            } 
            class   Myclass 
            { 
                    int   myint; 
                    public   int   Myint 
                    { 
                            get   
                            { 
                                    return   myint; 
                            } 
                            set   
                            { 
                                    if   (value   >   0) 
                                    { 
                                            myint   =   value; 
                                    } 
                                    else 
                                    { 
                                            Console.WriteLine("myint字段只接受大于0的值!"); 2  
                                    } 
      

  7.   

    第3个输出你的myint没变化所以还是1
      

  8.   

    myclass.Myint   =   1; 
    并没有改变。。你重新输出了一次
      

  9.   

    你在作myclass.Myint   =   -1; 时由于你的set里的if没有对value < 0进行判断,所以在Console.WriteLine(myclass.Myint); 时又把原来的myclass.Myint   =   1中得到的值返回,于是多了一个好象不知道哪里来得"1"