创建一个日期类,定义几个变量:年、月、日、小时、分钟、秒;构造函数的参数为System.DateTime类型,然后将值分别赋给定义的变量;然后构造两个重载方法SetTime,分别使用按值传递参数和按引用传递参数方式来对定义的变量进行计算。然后定义一个方法DisplayTime将这些变量的值输出。试比较按值传递和按引用传递这两种方式的区别。
提示:方法采用两种方式来传递参数,默认按照值传递的方式,另一种为按引用传递。方法只能有一个返回值,当然这个返回值可能是程序员定义的多个值集合的一个结构。当程序需要多于一个返回值时,如果只允许按值传递,会带来不便。而按引用传递参数,使得可以按需要同时返回的几个值都作为参数传递给方法,被调用的方法在其程序的内部对这些值做了修改后,调用者可以访问这些按引用传递的参数,来获取被调用方法计算的结果。
//本程序由王赟 计算机092编写 2011,11,6 编写,仅供交流
//做完C#作业,便把东西传了上来。
///<summary>
///wangyun 2011,11,06 9:36 at The Second ClassRoom Building
///</summary>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace S3_2
{
class Time
{
private int year;
private int month;
private int day;
private int hour;
private int minute;
private int second;
/// <summary>
/// 不带参数的构造函数
/// </summary>
public Time()
{ }
/// <summary>
/// 带参数的构造函数
/// </summary>
/// <param name="dt"></param>
public Time(System.DateTime dt)
{
this.year = dt.Year;
this.month = dt.Month;
this.day = dt.Day;
this.hour = dt.Hour;
this.minute = dt.Minute;
this.second = dt.Second;
}
/// <summary>
/// 设置时间,值传递
/// </summary>
public void SetTime(int year,int month,int day,int hour,int minute,int second)
{
this.year = year;
this.month = month;
this.day = day;
this.hour = hour;
this.minute = minute;
this.second = second;
if (this.second >= 60)
{
this.second = this.second - 60;
this.minute++;
}
if (this.minute >=60)
{
this.minute = this.minute - 60;
this.hour++;
}
if (this.hour >= 24)
{
this.hour = this.hour - 24;
this.day++;
}
second = this.second;
minute = this.minute;
hour = this.hour;
day = this.day;
}
/// <summary>
/// 设置时间,引用传递
/// </summary>
public void SetTime(ref int year,ref int month, ref int day,ref int hour,ref int minute, ref int second)
{
this.year = year;
this.month = month;
this.day = day;
this.hour = hour;
this.minute = minute;
this.second = second;
if (this.second >= 60)
{
this.second = this.second - 60;
this.minute++;
}
if (this.minute >= 60)
{
this.minute = this.minute - 60;
this.hour++;
}
if (this.hour >= 24)
{
this.hour = this.hour - 24;
this.day++;
}
second = this.second;
minute = this.minute;
hour = this.hour;
day = this.day;
}
/// <summary>
/// 显示时间
/// </summary>
public void DisplayTime()
{
Console.WriteLine("时间为:{0}年{1}月{2}日 {3}时{4}分{5}秒", year, month, day, hour, minute, second);
}
static void Main(string[] args)
{
int second = 61;
int minute = 59;
int hour = 23;
int day = 5;
int month = 11;
int year = 2011;
System.DateTime currentTime = System.DateTime.Now;
Time t = new Time(currentTime);
Console.Write("对象初始化..\n");
t.DisplayTime();
Console.Write("调用时间设置函数\n");
Console.WriteLine("初始值year={0},month={1},day={2},hour={3},minute={4},second={5}", year, month, day, hour, minute, second);
t.SetTime(year, month, day, hour, minute, second);
t.DisplayTime();
Console.WriteLine("值传递year={0},month={1},day={2},hour={3},minute={4},second={5}", year, month, day, hour, minute, second);
t.SetTime(ref year, ref month,ref day,ref hour,ref minute,ref second);
t.DisplayTime();
Console.WriteLine("引用传递year={0},month={1},day={2},hour={3},minute={4},second={5}", year, month, day, hour, minute, second);
}
}
}