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();
}
}
Here is a: 1, 2
Here is b: 10, 10
Result of 15 + b: 25, 25
8.7.int plus object |
| 8.7.1. | Overload the int + object |