09003 字段,属性,方法,源代码 – 恵玩科技

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace day9
{
    class Student
     
    {
        public Student()
        {
            Console.WriteLine("构造方法.");

        }
        public string name;
        public string gender;

        //用属性来保护字段
        private int age;
        public int Age
        {
            get { return age; }
            set { age=value;
                if (value == 33)
                {
                    age = 44;
                    for (int i = 1; i < 10; i++)
                    {
                        Console.WriteLine( i+"\n在set方法里面是可以添加判断和循环的");
                    }
                }
            }
        }


        public void Say()
        {
            Console.WriteLine("HelloWord!");
        }
        public void SayAge()
        {
            Console.WriteLine("我的年龄是:{0}",Age);
        }
    }

    
}

 

打赏作者