C # experiments [4]

Source: Internet
Author: User
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))

{

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

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.