Experimental requirements
1. Define 1-2 classes that you think are most capable of representing overloaded features and overload their methods or operators to verify and understand the basic characteristics of overloading
2. Method overload for fixed-length parameter list
3. Variable-length parameter list method overload
4. Implement overloads for the operators of custom classes (+,—, *,/and ++,――)
A) Note the difference between the precedence of the + + and-operators for reference types
b Note the difference between value types and reference types
5. Overload the True/false operator of the custom type and apply it to the instance
6. An overloaded calculation defines the explicit and implicit operators of a type.
Using System;
Using System.Reflection;
Namespace SEI. DL88250. Sourcecodes.csharp
{
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 to New ComplexNumber (real, image);
}
True/false overloading
If complex number is a PUREC image number,
return true and 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))
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.