创建一个日期类,定义几个变量:年、月、日、小时、分钟、秒;构造函数的参数为System.DateTime类型,然后将值分别赋给定义的变量;然后构造两个重载方法SetTime,分别使用按值传递参数和按引用传递参数方式来对定义的变量进行计算。然后定义一个方法DisplayTime将这些变量的值输出。最后使用这个类,查看变量在调用方法SetTime(按引用传递参数方式)前后值的变化情况,比较按值传递和按引用传递这两种方式的区别。 问题:“然后构造两个重载方法SetTime,分别使用按值传递参数和按引用传递参数方式来对定义的变量进行计算。”,何解? 这是我的程序,运行有错怎么改
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Date
{
    class DateTime
    {
        public int year;
        public int month;
        public int day;
        public int hour;
        public int minute;
        public int second;
        //构造函数
        public DateTime(System.DateTime date)
        {
            year = date.Year;
            month = date.Month;
            day = date.Day;
            hour = date.Hour;
            minute = date.Minute;
            second = date.Second;
           // Console.WriteLine("year-month-day h:m:s is{0}-{1}-{2} {3}:{4}:{5}", year, month, day, hour, minute, second);
        }
        public void SetTime()
        {
            int yr, mh, dy, hr, mt, sd;
            DateTime ds = new DateTime(System.DateTime.Now);
            yr = ds.year;
            mh = ds.month;
            dy = ds.day;
            hr = ds.hour;
            mt = ds.minute;
            sd = ds.second;
          //  Console.WriteLine("year-month-day h:m:s is{0}-{1}-{2} {3}:{4}:{5}", yr, mh, dy, hr, mt, sd);
        }
        public static void SetTime(ref int yr, ref int mh, ref int dy, ref int hr, ref int mt, ref int sd)
        {            DateTime ds = new DateTime(System.DateTime.Now);
            yr = ds.year;
            mh = ds.month;
            dy = ds.day;
            hr = ds.hour;
            mt = ds.minute;
            sd = ds.second;
          //  Console.WriteLine("year-month-day h:m:s is{0}-{1}-{2} {3}:{4}:{5}", yr, mh, dy, hr, mt, sd);
        }
        public void DisplayTime()
        {
            Console.WriteLine("year-month-day h:m:s is{0}-{1}-{2} {3}:{4}:{5}", year, month, day, hour, minute, second);
        }         static void Main(string[] args)
        {
           System.DateTime dt =  System.DateTime.Now;
           this.DateTime(dt);
           SetTime(year,month,day,hour,minute,second);
           DisplayTime();
           SetTime(ref year, ref month, ref day, ref hour, ref minute, ref second);
           DisplayTime();        }
    }}

解决方案 »

  1.   

    没太看明白楼主的意思,不过看代码,这样调用肯定会出错,改成下面的代码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();        }
      

  2.   

    http://hi.baidu.com/%C1%F5%D0%A1%C0%F61984519/blog/item/efaa4434f2173282a71e12e9.html这是我的程序,运行有错怎么改
    --------------------
    你看看具体在哪里报错.报什么错先吧..然后构造两个重载方法SetTime,分别使用按值传递参数和按引用传递参数方式来对定义的变量进行计算
    就是让你写个重载方法里面做不同的操作
    分别是引用传递的方式
    和值传递的方式.
    参考:值传递和引用传递
      

  3.   

    lz,你用ref,首先要现对变量赋予初值至少那里都是出错的你按照1楼的方式来试试看还有没有错
      

  4.   

    值传递是传递标识符所代表的变量的副本,而引用传递是传递的本身
    System.DateTime dt =  System.DateTime.Now; 
    year = dt.Year; 
    month = dt.Month; 
    day = dt.Day; 
    hour = dt.Hour; 
    minute = dt.Minute; 
    second = dt.Second
    SetTime(year,month,day,hour,minute,second); 
    DisplayTime(); 
    SetTime(ref year, ref month, ref day, ref hour, ref minute, ref second); 
    DisplayTime(); 
      

  5.   

    Programming C#中关于in,out,ref的示例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);
            }
        }
    }