编写控制台程序,定义一个类,在勒种定义private字段age以及对应的属性Age,在属性的set程序块中编写代码,实现对属性的复制进行检查。在类中定义一个方法,在main()方法中实现其调用

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

namespace AttributeTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Human.Age = 30;//通过属性给字段赋值
            Console.WriteLine(Human.printInfo().ToString());
            Console.ReadKey();

        }
        class Human
        {
            private static int age;
            public static int Age //类的属性
            {
                get { return age; }
                set
                {
                    if(value>0)
                    {
                        age = value;
                    }
                    else
                    {
                        age = 0;
                    }
                }
            }
            public static int printInfo()
            {
                return age;
            }

        }
    }
}

猜你喜欢

转载自blog.csdn.net/laizhuocheng/article/details/89403298