Csharp/CSharp Tutorial/Operator Overload/operator overload Binary Plus Subtract

Материал из .Net Framework эксперт
Версия от 12:18, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Add + operator for Complex type

using System;
public struct Complex
{
    public Complex( double real, double imaginary ) {
        this.real = real;
        this.imaginary = imaginary;
    }
    static public Complex Add( Complex lhs, Complex rhs ) {
        return new Complex( lhs.real + rhs.real, lhs.imaginary  + rhs.imaginary );
    }
    static public Complex Add( Complex lhs, double rhs ) {
        return new Complex( rhs + lhs.real, lhs.imaginary );
    }
    public override string ToString() {
        return String.Format( "({0}, {1})", real, imaginary );
    }
    static public Complex operator+( Complex lhs, Complex rhs ) {
        return Add( lhs, rhs );
    }
    static public Complex operator+( double lhs, Complex rhs ) {
        return Add( rhs, lhs );
    }
    static public Complex operator+( Complex lhs, double rhs ) {
        return Add( lhs, rhs );
    }
    private double real;
    private double imaginary;
}
public class MainClass
{
    static void Main() {
        Complex cpx1 = new Complex( 1.0, 3.0 );
        Complex cpx2 = new Complex( 1.0, 2.0 );
        Complex cpx3 = cpx1 + cpx2;
        Complex cpx4 = 20.0 + cpx1;
        Complex cpx5 = cpx1 + 25.0;
        Console.WriteLine( "cpx1 == {0}", cpx1 );
        Console.WriteLine( "cpx2 == {0}", cpx2 );
        Console.WriteLine( "cpx3 == {0}", cpx3 );
        Console.WriteLine( "cpx4 == {0}", cpx4 );
        Console.WriteLine( "cpx5 == {0}", cpx5 );
    }
}
cpx1 == (1, 3)
cpx2 == (1, 2)
cpx3 == (2, 5)
cpx4 == (21, 3)
cpx5 == (26, 3)

An operator overloading: binary + and -

using System; 
 
class TwoDimension { 
  int x, y;   
 
  public TwoDimension() { 
     x = y = 0; 
  } 
  public TwoDimension(int i, int j) { 
     x = i; 
     y = j;  
  } 
 
  // Overload binary +. 
  public static TwoDimension operator +(TwoDimension op1, TwoDimension op2) 
  { 
    TwoDimension result = new TwoDimension(); 
 
    result.x = op1.x + op2.x; 
    result.y = op1.y + op2.y; 
 
    return result; 
  } 
 
  // Overload binary -. 
  public static TwoDimension operator -(TwoDimension op1, TwoDimension op2) 
  { 
    TwoDimension result = new TwoDimension(); 
 
    /* Notice the order of the operands. op1 is the left 
       operand and op2 is the right. */ 
    result.x = op1.x - op2.x;
    result.y = op1.y - op2.y;  
 
    return result; 
  } 
   
  public void show() 
  { 
    Console.WriteLine(x + ", " + y); 
  } 
} 
 
class MainClass { 
  public static void Main() { 
    TwoDimension a = new TwoDimension(1, 2); 
    TwoDimension b = new TwoDimension(10, 10); 
    TwoDimension c = new TwoDimension(); 
 
    Console.Write("Here is a: "); 
    a.show(); 
    Console.WriteLine(); 
    Console.Write("Here is b: "); 
    b.show(); 
    Console.WriteLine(); 
 
    c = a + b; // add a and b together 
    Console.Write("Result of a + b: "); 
    c.show(); 
    Console.WriteLine(); 
 
    c = a + b + c; // add a, b and c together 
    Console.Write("Result of a + b + c: "); 
    c.show(); 
    Console.WriteLine(); 
 
    c = c - a; // subtract a 
    Console.Write("Result of c - a: "); 
    c.show(); 
    Console.WriteLine(); 
 
    c = c - b; // subtract b 
    Console.Write("Result of c - b: "); 
    c.show(); 
    Console.WriteLine(); 
  } 
}
Here is a: 1, 2
Here is b: 10, 10
Result of a + b: 11, 12
Result of a + b + c: 22, 24
Result of c - a: 21, 22
Result of c - b: 11, 12

Operator overloading: +, -

using System;
public class Point
{
    public Point( int x, int y )
    {
        this.X = x;
        this.Y = y;
    }
    public int X;
    public int Y;
    public static Point operator + ( Point a, Point b )
    {
        return new Point( a.X + b.X, a.Y + b.Y );
    }
    public static Point operator - ( Point a )
    {
        return new Point( - a.X , - a.Y );
    }
    static void Main(string[] args)
    {
        Point p = new Point( 3, 4 );
        Point q = new Point( 36, -5 );
        Point r = p + ( - q );
        System.Console.WriteLine( "Result: x = {0}, y = {1}", r.X, r.Y );
    }
}
Result: x = -33, y = 9