using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharpMethod
{
class Program
{
static void Main(string[] args)
{
Time t1 = new Time();
t1.TimeShow();
Time t2 = new Time(20,28,40);
t2.TimeShow();
}
}
public class Time
{
private int hour;
private int minute;
private int second;
public int Hour
{
get { return hour; }
set { hour = (value >= 0 && value <= 23 ? value : 0); }
}
public int Minute
{
get { return minute; }
set { minute = value >= 0 && value <= 59 ? value : 0; }
}
public int Second
{
get { return second; }
set { second = value >= 0 && value <= 59 ? value : 0; }
}
public void TimeShow()
{
Console.WriteLine("当前时间为:{0}时{1}分{2}秒", hour, minute, second);
}
public Time()
{
hour = minute = second = 0;
Console.WriteLine("初始化构造函数完成!");
}
public Time(int h, int m, int s)
{
hour = (h >= 0 && h <= 23 ? h : 0);
minute = (m >= 0 && m <= 59 ? m : 0);
second = (s >= 0 && s <= 259 ? s : 0);
Console.WriteLine("赋初值构造函数完成!");
}
~Time()
{
Console.WriteLine("析构函数执行,对象销毁完成!");
}
}
}
网友评论