8.7.6 Operators
来源:互联网 发布:马拉多纳 知乎 编辑:程序博客网 时间:2024/05/19 12:25
An operator is a member that defines the meaning of an expression operator
that can be applied to instances
of the class. There are three kinds of operators that can be defined: unary
operators, binary operators, and
conversion operators.
The following example defines a Digit type that represents decimal
digits.integral values between 0
and 9.
using System;
public struct Digit
{
byte value;
public Digit(byte value) {
if (value < 0 || value > 9) throw new ArgumentException();
this.value = value;
}
public Digit(int value): this((byte) value) {}
public static implicit operator byte(Digit d) {
return d.value;
}
public static explicit operator Digit(byte b) {
return new Digit(b);
}
public static Digit operator+(Digit a, Digit b) {
return new Digit(a.value + b.value);
}
public static Digit operator-(Digit a, Digit b) {
return new Digit(a.value - b.value);
}
public static bool operator==(Digit a, Digit b) {
return a.value == b.value;
}
public static bool operator!=(Digit a, Digit b) {
return a.value != b.value;
}
public override bool Equals(object value) {
if (value == null) return false;
if (GetType() == value.GetType()) return this == (Digit)value;
return false; }
public override int GetHashCode() {
return value.GetHashCode();
}
public override string ToString() {
return value.ToString();
}
}
C# LANGUAGE SPECIFICATION
38
class Test
{
static void Main() {
Digit a = (Digit) 5;
Digit b = (Digit) 3;
Digit plus = a + b;
Digit minus = a - b;
bool equals = (a == b);
Console.WriteLine("{0} + {1} = {2}", a, b, plus);
Console.WriteLine("{0} - {1} = {2}", a, b, minus);
Console.WriteLine("{0} == {1} = {2}", a, b, equals);
}
}
The Digit type defines the following operators:
. An implicit conversion operator from Digit to byte.
. An explicit conversion operator from byte to Digit.
. An addition operator that adds two Digit values and returns a Digit value.
. A subtraction operator that subtracts one Digit value from another, and
returns a Digit value.
. The equality (==) and inequality (!=) operators, which compare two Digit
values.
that can be applied to instances
of the class. There are three kinds of operators that can be defined: unary
operators, binary operators, and
conversion operators.
The following example defines a Digit type that represents decimal
digits.integral values between 0
and 9.
using System;
public struct Digit
{
byte value;
public Digit(byte value) {
if (value < 0 || value > 9) throw new ArgumentException();
this.value = value;
}
public Digit(int value): this((byte) value) {}
public static implicit operator byte(Digit d) {
return d.value;
}
public static explicit operator Digit(byte b) {
return new Digit(b);
}
public static Digit operator+(Digit a, Digit b) {
return new Digit(a.value + b.value);
}
public static Digit operator-(Digit a, Digit b) {
return new Digit(a.value - b.value);
}
public static bool operator==(Digit a, Digit b) {
return a.value == b.value;
}
public static bool operator!=(Digit a, Digit b) {
return a.value != b.value;
}
public override bool Equals(object value) {
if (value == null) return false;
if (GetType() == value.GetType()) return this == (Digit)value;
return false; }
public override int GetHashCode() {
return value.GetHashCode();
}
public override string ToString() {
return value.ToString();
}
}
C# LANGUAGE SPECIFICATION
38
class Test
{
static void Main() {
Digit a = (Digit) 5;
Digit b = (Digit) 3;
Digit plus = a + b;
Digit minus = a - b;
bool equals = (a == b);
Console.WriteLine("{0} + {1} = {2}", a, b, plus);
Console.WriteLine("{0} - {1} = {2}", a, b, minus);
Console.WriteLine("{0} == {1} = {2}", a, b, equals);
}
}
The Digit type defines the following operators:
. An implicit conversion operator from Digit to byte.
. An explicit conversion operator from byte to Digit.
. An addition operator that adds two Digit values and returns a Digit value.
. A subtraction operator that subtracts one Digit value from another, and
returns a Digit value.
. The equality (==) and inequality (!=) operators, which compare two Digit
values.
- 8.7.6 Operators
- Operators
- Operators
- Operators
- Operators
- 14.9.6 Reference type equality operators
- XPath Operators
- 14.2 Operators
- 17.9 Operators
- Oracle Operators
- Conversion Operators
- C++ Operators
- Conversion Operators
- boost operators
- Javascript:Operators
- Arithmetic Operators
- Set Operators
- boost operators
- 8.7 Classes
- 8.7.1 Constants & 8.7.2 Fields
- 8.7.3 Methods
- 8.7.4 Properties
- 8.7.5 Events
- 8.7.6 Operators
- 8.7.7 Indexers
- 8.7.8 Instance constructors
- 8.7.9 Destructors
- 8.7.10 Static constructors
- 8.7.11 Inheritance
- 8.8 Structs
- 8.9 Interfaces
- 8.10 Delegates