1. 定义1-2个你认为最能体现重载特性的类,并对其方法或运算符进行重载来验证及了解重载的基本特性
2. 定长参数列表的方法重载
3. 变长参数列表的方法重载
4. 为自定义的类的运算符(+,—,*,/及++,――)实现重载
a) 注意体现对于引用类型的++及--运算符的前后置情况的区别
b) 注意值类型及引用类型的区别
5. 重载自定义类型的true/false运算符,并应用到实例中
6. 重载算定义类型的显式和隐式运算符。
{
class MessageHandler
{
private static MethodInfo[] mi = // Storage Methods' infomation
typeof(MessageHandler).GetMethods();
private ComplexNumber c1 = new ComplexNumber();
private ComplexNumber c2 = new ComplexNumber();
public enum CplNum {real, image}; // Index Real or Image
public struct Digit
{
byte value;
public Digit(byte value)
{
if (value < 0 || value > 9)
throw new ArgumentException();
this.value = value;
}
public static implicit operator byte(Digit d)
{
return d.value;
}
public static explicit operator Digit(byte b)
{
return new Digit(b);
}
}
class ComplexNumber
{
private double real;
private double image;
// Constructors
public ComplexNumber() { }
public ComplexNumber(double real, double image)
{
this.real = real;
this.image = image;
}
// Addition overloading
public static ComplexNumber operator +(ComplexNumber op1,
ComplexNumber op2)
{
return new ComplexNumber(op1[CplNum.real] + op2[CplNum.real],
op1[CplNum.image]
+ op2[CplNum.image]);}
// Increment overloading
public static ComplexNumber operator ++(ComplexNumber op)
{
double real = op[CplNum.real] + 1.0;
double image = op[CplNum.image] + 1.0;
return new ComplexNumber(real, image);
}
// true/false overloading
// If current complex number is a purec image number,
// return true, otherwise return false
public static bool operator true(ComplexNumber cn)
{
return 0 != cn.image && 0 == cn.real;
}
public static bool operator false(ComplexNumber cn)
{
return 0 != cn.real;
}
// Indexer
public double this[CplNum index]
{
get
{
// do some bounds checking
if ((index < CplNum.real) || (index > CplNum.image))
{
throw new IndexOutOfRangeException(
"Cannot get element " + index);
}
if (CplNum.real == index)
{
return real;
}
else
{
return image;
}
}
set
{
if ((index < CplNum.real) || (index > CplNum.image))
{
throw new IndexOutOfRangeException(
"Cannot set element " + index);
}
if (CplNum.real == index)
{
real
= value;}
else
{
image
= value;}
}
}
}
public void Message()
{
Console.Write(
"In Message(): ");Console.WriteLine(
"Hello World!");}
public void Message(char ch)
{
Console.Write(
"In Message(Char): ");Console.WriteLine(ch);
}
public void Message(string msg)
{
Console.Write(
"In Message(System.String): ");Console.WriteLine(msg);
}
public void Message(string msg, int val)
{
Console.Write(
"In Message(System.String, Int32): ");Console.WriteLine(
"val = {0}", val);}
public void Message(string msg, params int[] args)
{
Console.Write(
"In Message(System.String, Int32[]): ");Console.Write(msg);
for (int ix = 0; ix < args.Length; ix++)
{
Console.Write(
"{0} ", args[ix].ToString());}
Console.WriteLine();
}
public void Message(string msg, params double[] args)
{
Console.Write(
"In Message(System.String, Double[]): ");Console.Write(msg);
for (int ix = 0; ix < args.Length; ix++)
{
Console.Write(
"{0:F1} ", args[ix]);}
Console.WriteLine();
}
public void Message(string msg, params object[] args)
{
Console.Write(
"In Message(System.String, System.Object[]): ");Console.Write(msg);
if (args.Length != 0)
{
foreach (object o in args)
{
Console.Write(
"{0} ", o.ToString());}
}
Console.WriteLine();
}
public static void Main(string[] args)
{
Console.Title
= "Lab #5-Polymorphism by 88250";Console.ForegroundColor
= ConsoleColor.DarkRed;// Method overloading demostrate
Console.WriteLine("Enumerate all Methods named "Message": ");
foreach (MethodInfo m in mi)
{
if ("Message" == m.Name)
{
Console.WriteLine(
" Mehtod: {0}", m.GetBaseDefinition());}
}
MessageHandler mh
= new MessageHandler();mh.Message();
mh.Message(
'V');mh.Message(
"I'm Daniel");double fVal1 = 10.0, fVal2 = 0.0;
// all match: Message(string, params double[])
mh.Message("Lly ", 10.0, fVal1, fVal2, 1024.0);
mh.Message(
"Double fib: ", 1.0, 1.0, 2.0, 3.0, 5.0, 8.0, 13.0, 21.0, 34.0);int iVal1= 10, iVal2 = 0;
mh.Message(
"mumble", iVal1);// all match: Message(string, params int[])
mh.Message("88250 ", 10, iVal1, iVal2, 1024);
mh.Message(
"Fib: ", 1, 1, 2, 3, 5, 8, 13, 21, 34, 55);// match: Message(string, params Object[])
mh.Message("DLly ", 88250, "LLY", 84588990, "DL");
Console.ForegroundColor
= ConsoleColor.Blue;// Operator and true/false overloading demonstrate
mh.c1[CplNum.real] = 1.0;
mh.c1[CplNum.image]
= 2.0;mh.c2[CplNum.real]
= -1.0;mh.c2[CplNum.image]
= -3.0;
Console.WriteLine(
"C1 = {0} + {1}i", mh.c1[CplNum.real], mh.c1[CplNum.image]);Console.WriteLine(
"C2 = {0} + {1}i", mh.c2[CplNum.real], mh.c2[CplNum.image]);
ComplexNumber result
= new ComplexNumber((mh.c1 + mh.c2)[CplNum.real],(mh.c1
+ mh.c2)[CplNum.image]);if (result)
{
Console.WriteLine(
"C1 + C2 = {0}i", result[CplNum.image]);Console.WriteLine(
"The result number is a pure image number!");}
else
{
Console.WriteLine(
"C1 + C2 = {0} + {1}i", result[CplNum.real], result[CplNum.image]);}
ComplexNumber cn1
= result++;Console.WriteLine(
"(C1 + C2)++ = {0} + {1}i", cn1[CplNum.real], cn1[CplNum.image]);ComplexNumber cn2
= ++result;Console.WriteLine(
"++(C1 + C2) = {0} + {1}i", cn2[CplNum.real], cn2[CplNum.image]);}
}
}
做重载操作符的时候的返回值时,往往需要“new”一个新的变量--除了true和false操作符。这在重载 “++”和“--” 操作符时尤其显得重要。也就是说我们做在a++时,我们将丢弃原来的a值,而取代的是新的new出来的值给a! 值得注意的是e=a++或f=++a中e的值或f的值根本与我们重载的操作符返回值没有一点联系!它们的值仅仅是在前置和后置的情况下获得a的旧值或新值 而已!所以,在C#中是可是实现前置和后置的行为的!
另外,在源码清单中的代码例子提供了Digit类型和byte类型之间的隐式转换和显式转换。从Digit到byte的转换为隐式转换,转换过程不会因为丢失任何信息而抛出异常。从byte到Digit的转换为显式转换,转换过程有可能因丢失信息而抛出异常。