Csharp/CSharp Tutorial/Operator Overload/int plus object — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 15:18, 26 мая 2010
Overload the int + object
<source lang="csharp">using System;
class TwoDimension {
int x, y; public TwoDimension() { x = y = 0; } public TwoDimension(int i, int j) { x = i; y = j; } // Overload binary + for int + object. public static TwoDimension operator +(int op1, TwoDimension op2) { TwoDimension result = new TwoDimension(); result.x = op2.x + op1; result.y = op2.y + op1; return result; } // Show X, Y public void show() { Console.WriteLine(x + ", " + y); }
}
class TwoDimensionDemo {
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 = 15 + b; // int + object Console.Write("Result of 15 + b: "); c.show(); }
}</source>
Here is a: 1, 2 Here is b: 10, 10 Result of 15 + b: 25, 25