类与对象练习题(2018/07/03)

.类的成员变量与方法、构造方法 在程序中经常要对时间进行操作但是并没有时间类型的数据。
* 那么我们可以自己实现一个时间类来满足程序中的需要。
* 定义名为MyTime的类其中应有三个整型成员时hour分minute秒second为了保证数据的安全性这三个成员变量应声明为私有。
* 为MyTime类定义构造方法以方便创建对象时初始化成员变量。
* 再定义diaplay方法用于将时间信息打印出来。
* 为MyTime类添加以下方法
* addSecond(int sec)
* addMinute(int min)
* addHour(int hou)
* subSecond(int sec)
* subMinute(int min)
* subHour(int hou)
* 分别对时、分、秒进行加减运算。

using System;

namespace cchoop
{
    class MyTime
    {
        private int hour;
        private int minute;
        private int second;

        public MyTime(int hour, int minute, int second)
        {
            this.hour = hour;
            this.minute = minute;
            this.second = second;
            CheckTime();
        }

        public void Diaplay()
        {
            Console.WriteLine("时间为:{0}:{1}:{2}",
                hour >= 10 ? hour.ToString() : "0" + hour,
                minute >= 10 ? minute.ToString() : "0" + minute,
                second >= 10 ? second.ToString() : "0" + second);
            Console.WriteLine("=================================");
        }

        public void AddSecond(int sec)
        {
            Console.WriteLine("增加了{0}秒",sec);
            this.second += sec;
            CheckTime();
        }
        public void AddMinute(int min)
        {
            Console.WriteLine("增加了{0}分", min);
            this.minute += min;
            CheckTime();
        }
        public void AddHour(int hou)
        {
            Console.WriteLine("增加了{0}时", hou);
            this.hour += hou;
            CheckTime();
        }
        public void SubSecond(int sec)
        {
            Console.WriteLine("减少了{0}秒", sec);
            this.second -= sec;
            CheckTime();
        }
        public void SubMinute(int min)
        {
            Console.WriteLine("减少了{0}分", min);
            this.minute -= min;
            CheckTime();
        }
        public void SubHour(int hou)
        {
            Console.WriteLine("减少了{0}时", hou);
            this.hour -= hou;
            CheckTime();
        }

        private void CheckTime()
        {
            //格式化秒(second)
            if (this.second >= 60)
            {
                this.minute += this.second / 60;
                this.second = this.second % 60;
            }
            else if (this.second < 0)
            {
                this.minute += (this.second / 60 - 1);
                this.second = this.second % 60 + 60;
            }

            //格式化分(minute)
            if (this.minute >= 60)
            {
                this.hour += this.minute / 60;
                this.minute = this.minute % 60;
            }
            else if (this.minute < 0)
            {
                this.hour += (this.minute / 60 - 1);
                this.minute = this.minute % 60 + 60;
            }

            //格式化时(hour)
            if (this.hour >= 24)
            {
                this.hour = this.hour % 24;
            }
            else if (this.hour < 0)
            {
                this.hour = this.hour % 24 + 24;
            }
        }
    }
}

测试:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace cchoop
{
    class Program
    {
        static void Main(string[] args)
        {
            MyTime myTime = new MyTime(16, 50, 52);
            myTime.Diaplay();
            myTime.AddHour(56);
            myTime.AddMinute(20);
            myTime.Diaplay();
            myTime.SubSecond(80);
            myTime.Diaplay();
            myTime.AddSecond(80);
            myTime.Diaplay();
        }
    }
}

这里写图片描述


猜你喜欢

转载自blog.csdn.net/qq_34937637/article/details/80903387