second.cs

来自「微软系列丛书<<C#2005从入门到精通>>」· CS 代码 · 共 78 行

CS
78
字号

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

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

		// add static implicit operator Second (int arg) here

		// don't delete the following operator
		public static bool operator==(Second lhs, Second rhs)
		{
			return lhs.value == rhs.value;
		}

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

		public static bool operator==(int lhs, Second rhs)
		{
			return lhs == rhs.value;
		}
		
		// don't delete the following operator
		public static bool operator!=(Second lhs, Second rhs)
		{
			return lhs.value != rhs.value;
		}

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

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

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

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

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

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

		private int value;
	}
}

⌨️ 快捷键说明

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