创建一个日期类,定义几个变量:年、月、日、小时、分钟、秒;构造函数的参数为System.DateTime类型,然后将值分别赋给定义的变量;然后构造两个重载方法SetTime,分别使用按值传递参数和按引用传递参数方式来对定义的变量进行计算。然后定义一个方法DisplayTime将这些变量的值输出。最后使用这个类,查看变量在调用方法SetTime(按引用传递参数方式)前后值的变化情况,比较按值传递和按引用传递这两种方式的区别。 问题:“然后构造两个重载方法SetTime,分别使用按值传递参数和按引用传递参数方式来对定义的变量进行计算。”,何解? 以下是我的代码: 
using System; 
using System.Collections.Generic; 
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; 
        } 
        public void SetTime(int yr, int mh, int dy, int hr, int mt, int sd) 
        {         } 
        public void SetTime(ref int yr, ref int mh, ref int dy, ref int hr, ref int mt, ref int 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); 
        } 
    } 
    class Program 
    { 
        static void Main(string[] args) 
        {         } 
    } 
} 编到这里就编不下去了,思路模糊了; 
还有,最后一个要求是“最后使用这个类,查看变量在调用方法SetTime(按引用传递参数方式)前后值的变化情况,比较按值传递和按引用传递这两种方式的区别。 
”?又是什么意思呢? 
给这条题目搞晕了,请大家指点一下,谢谢

解决方案 »

  1.   


    using System; 
    using System.Collections.Generic; 
    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; 
            } 
            public void SetTime(int yr, int mh, int dy, int hr, int mt, int sd) 
            { 
                yr--;
                mh--;
                …………//随便改一下数值;
            } 
            public void SetTime(ref int yr, ref int mh, ref int dy, ref int hr, ref int mt, ref int sd) 
            { 
                yr--;
                mh--;
                …………//随便改一下数值;        } 
            public void DisplayTime() 
            { 
                Console.WriteLine("year-month-day h:m:s is{0}-{1}-{2} {3}:{4}:{5}", year, month, day, hour, minute, second); 
            } 
        } 
        class Program 
        { 
            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();
            } 
        } 
      

  2.   

    ref  按引用传递是传递你的变量地址,方法里面修改的话会把原有的变量值给修改掉。
    按值传递不说了。