hour.cs

来自「微软(Microsoft)出版社C井练习文件及解答」· CS 代码 · 共 63 行

CS
63
字号

namespace Operators
{
	struct Hour
	{
		public Hour(int initialValue)
		{
			this.value = System.Math.Abs(initialValue) % 24;
		}

		public int Value
		{
			get { return this.value; }
		}

		public static bool operator==(Hour lhs, int rhs)
		{
			return lhs.value == rhs;
		}

		public static bool operator!=(Hour lhs, int rhs)
		{
			return lhs.value != rhs;
		}

		public static bool operator==(int lhs, Hour rhs)
		{
			return lhs == rhs.value;
		}

		public static bool operator!=(int lhs, Hour rhs)
		{
			return lhs != rhs.value;
		}

		public override bool Equals(object other)
		{
			return (other is Hour) && Equals((Hour)other);
		}

		public bool Equals(Hour other)
		{
			return this.value == other.value;
		}

		public override int GetHashCode()
		{
			return this.value;
		}

		public static Hour operator++(Hour arg)
		{
			arg.value++;
			if (arg.value == 24)
			{
				arg.value = 0;
			}
			return arg;
		}

		private int value;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?