首先建立控制台应用程序
C#的控制台应用程序
然后编写实例代码
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DeclaringConstructor
{
public class Time
{
int Year;
int Month;
int Date;
int Hour;
int Minute;
int Second;
public void DisplayCurrentTime()
{
System.Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}\n", Month, Date, Year, Hour, Minute, Second);
}
public Time(System.DateTime dt)
{
Year = dt.Year;
Month = dt.Month;
Date = dt.Day;
Hour = dt.Hour;
Minute = dt.Minute;
Second = dt.Second;
}
static void Main(string[] args)
{
System.DateTime currentTime = System.DateTime.Now;
Time t = new Time(currentTime);
t.DisplayCurrentTime();
Console.ReadLine();
}
}
}
结果如下所示: