111,120
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Text;
namespace InOutRef
{
public class Time
{
//似有成员变量
private int Year;
private int Month;
private int Date;
private int Hour;
private int Minute;
private int Second;
//公共访问方法
public void DisplayCurrentTime()
{
System.Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}", Month, Date, Year, Hour, Minute, Second);
}
public int GetHour()
{
return Hour;
}
public void SetTime(int hr, out int min, ref int sec)
{
//如果传入的时间≥30
//递增分并将秒设为 0
//否则不动
if (sec >= 30)
{
Minute++;
Second = 0;
}
//设为传入的值
Hour = hr;
//将分秒传出
min = Minute;
sec = Second;
}
//构造方法
public Time(System.DateTime dt)
{
Year = dt.Year;
Month = dt.Month;
Date = dt.Day;
Hour = dt.Hour;
Minute = dt.Minute;
Second = dt.Second;
}
}
class Tester
{
static void Main()
{
System.DateTime currentTime = System.DateTime.Now;
Time t = new Time(currentTime);
t.DisplayCurrentTime();
int theHour = 3;
int theMinute;//out可以避免引用参数初始化的要求
int theSecond = 20;
t.SetTime(theHour, out theMinute, ref theSecond);
System.Console.WriteLine("{0}:{1}:{2}", theHour, theMinute, theSecond);
theSecond = 40;
t.SetTime(theHour, out theMinute, ref theSecond);
System.Console.WriteLine("{0}:{1}:{2}", theHour, theMinute, theSecond);
}
}
}
static void Main(string[] args)
{
System.DateTime dt = System.DateTime.Now;
DateTime dtMy = new DateTime(dt);
int year = 0;
int month = 0;
int day = 0;
int hour = 0;
int minute = 0;
int second = 0;
SetTime(ref year, ref month, ref day, ref hour, ref minute, ref second);
dtMy.DisplayTime();
SetTime(ref year, ref month, ref day, ref hour, ref minute, ref second);
dtMy.DisplayTime();
}