C # in a wonderful operator overload
Careful friends may find that although C # can overload operators, but compared with C , it is very different. When the definition is defined, the operator method must be static, and at least one parameter (one and two, one and two), C # and C , the most important feature is: <,>; ==,! =; True, False must be paired, that is, "<" must be overloaded ">", heavy is overloaded "==", and "TRUE" must be heavy. "False"; in addition, the two virtual methods of the base class Object: gethashcode () and Equals (Object Obj).
The following is the program code, some places are not perfect, can only say only explain the problem, please advise:
Using system;
USING SYSTEM.XML;
Namespace ConsoleApplication8
{
///
/// Name class
/// summary>
Class name
{
Private string dimstname;
PRIVATE STRING LASTNAME;
Public namer ()
{
This.init (NULL, NULL);
}
Public Name (String Fn, String LN)
{
THIS.INIT (FN, LN);
}
Private void init (String Fn, String LN)
{
THIS.FIRSTNAME = Fn;
THIS.LASTNAME = ln;
}
///
/// Overload operator TRUE
/// summary>
/// param>
///
Public Static Bool Operator True (Namer N)
{
IF (n.firstname == null || n.lastname == null)
Return False;
Else
Return True;
}
///
/// Overload operator FALSE
/// summary>
/// param>
///
Public Static Bool Operator False (NAMER N)
{
IF (n.firstname == null || n.lastname == null)
Return False;
Else
Return True;
}
///
/// Overload operator ==
/// summary>
/// param>
/// param>
///
Public Static Bool Operator == (Namer N1, NAMER N2) {
IF (n1.firstname == n2.firstname && n1.lastname == n2.lastname)
Return True;
Else
Return False;
}
///
/// Overload operator! =
/// summary>
/// param>
/// param>
///
Public Static Bool Operator! = (Namer N1, NAMER N2)
{
IF (n1.firstname! = n2.firstname || n1.lastname! = n2.lastname)
Return True;
Else
Return False;
}
///
/// Overload operator>
/// summary>
/// param>
/// param>
///
Public Static Bool Operator> (Namer N1, NAMER N2)
{
Return (n1.firstname.compareto (n2.firstname)> 0 && n1.lastname.comPareto (n2.lastname)> 0);
}
///
/// Overload operator <
/// summary>
/// param>
/// param>
///
Public Static Bool Operator <(Namer N1, NAMER N2)
{
Return (n1.firstname.compareto (n2.firstname) <0 && n1.lastname.compareto (n2.lastname) <0);
}
///
/// Rewriting method, must have, behind 111 is messy, you can also write other
/// summary>
///
Public Override Int getHashcode ()
{
Return Base.gethashcode () * 111;
}
///
/// Rewriting method, there must be
/// summary>
/// param>
///
Public Override Bool Equals (Object Obj)
{
Return Base.equals (OBJ);
}
///
/// Rewriting method, there must be
/// summary>
///
Public override string toString ()
{
Return "surname:" this.firstname "name:" this.lastname
}
Public static void main ()
{
Namer n1 = new name ("li", "zanhong");
Name N2 = New Name ("Ahang", "Aan");
// Namer n2 = new name ("li", "zanhong");
IF (n1 == n2)
Console.writeline ("The same name");
Else
Console.WriteLine ("Different Name");
/
IF (n1! = n2)
Console.WriteLine ("Different Names is established");
Else
Console.WriteLine ("The same name is established");
/
IF (n1> n2)
Console.writeline (n1.toString () ">" n2.tostring ());
ELSE IF (N1 Console.writeline (n1.toString () "<" n2.toString ()); Else Console.writeLine ("Not Corrected Compare"); Console.readline (); } } }