C# 属性2

来源:互联网 发布:淘宝号怎么注销手机号 编辑:程序博客网 时间:2024/05/21 05:56
using System;using System.Collections.Generic;using System.Linq;using System.Text;//通过属性,可以使得类的变量在类的外部被使用namespace ConsoleApplication10{    class Time    {        //私有变量成员        private int year;        private int month;        private int data;        private int hour;        private int minute;        private int sencond;        //属性声明        public int Hour        {            get { return hour; }            set { hour = value; }        }        //公共方法        public void DisplayCurrentTime()        {            Console.WriteLine("Time:{0}/{1}/{2} {3}:{4}:{5}", month, data, year, hour, minute, sencond);        }        //构造函数        public Time(DateTime dt)        {            year = dt.Year;            month = dt.Month;            data = dt.Day;            hour = dt.Hour;            minute = dt.Minute;            sencond = dt.Second;        }    }    public class Tester    {        public void Run()        {            DateTime currentTime = DateTime.Now;            Time t = new Time(currentTime);            t.DisplayCurrentTime();            int theHour = t.Hour;            //显示            Console.WriteLine("Retrieved the hour:{0}", theHour);            //增量            theHour ++;            //通过属性重新赋值            t.Hour=theHour;            //显示属性            Console.WriteLine("Updated the hour:{0}",t.Hour);        }    }    class Program    {        static void Main(string[] args)        {            Tester test = new Tester();            test.Run();            Console.ReadLine();        }    }}
0 0
原创粉丝点击