{
return opl.x * opl. * opl.z;
}
, .
// , using System;
// , class ThreeD {
int , , z; //
public ThreeD() { = = z = 0; }
public ThreeD(int i, int j, int k) { x = i; = j; z = k; }
// +.
public static ThreeD operator +(ThreeD opl, ThreeD op2)
{
ThreeD result = new ThreeD();
result.x = opl.x + op2.x;
result. = opl.y + op2.y;
result.z = opl.z + op2.z;
Return result;
}
// ThreeD int. public static implicit operator int(ThreeD opl)
{
return opl.x * opl.y * opl.z;
}
// X, Y, Z. public void Show()
{
Console.WriteLine(x + ", " + + ", " + z);
}
}
class ThreeDDemo { static void Main() {
ThreeD a = new ThreeD(1, 2, 3);
ThreeD b = new ThreeD(10, 10, 10);
ThreeD = new ThreeD(); int i;
Console.Write(" a: ");
A.Show();
Console.WriteLine();
Console.Write(" b: ");
B.Show();
Console.WriteLine();
= a + b; // b Console.Write(" a + b: ");
C.Show ();
Console.WriteLine(); i = a; // int
Console.WriteLine(" i = a: " + i); Console.WriteLine();
i=a*2-b; // int
Console.WriteLine(" a * 2 -
}
}
.
: 1, 2, 3
: 10, 10, 10
+: 11, 12, 13 i = : 6
* 2 - : -988
, ThreeD , i = , . 6, , . int , . operator int () = + .
. , ThreeD double . .
: ; ; . , , , . . , int.
|
|
// , using System;
// , class ThreeD {
int , , z; // public ThreeD() { = = z = 0; }
public ThreeD(int i, int j, int k) { x = i; = j; z = k; }
// +.
public static ThreeD operator +(ThreeD opl, ThreeD op2)
{
ThreeD result = new ThreeD();
result.x = opl.x + op2.x; result. = opl. + op2.y; result.z = opl.z + op2.z;
Return result;
}
// , public static explicit operator int(ThreeD opl)
{
return opl.x * opl. * opl.z;
}
// X, Y, Z. public void Show ()
{
Console.WriteLine (x + ", " + + ", " + z);
}
}
class ThreeDDemo { static void Main() {
ThreeD a = new ThreeD(1, 2, 3);
ThreeD b = new ThreeD(10, 10, 10);
ThreeD = new ThreeD(); int i;
Console.Write(" a: ");
A.Show();
Console.WriteLine();
Console.Write(" b: ");
B.Show();
Console.WriteLine();
= a + b; // b Console.Write(" a + b: ");
C.Show();
Console.WriteLine();
i = (int) a; // int ,
// Console.WriteLine(" i = : " + i);
Console.WriteLine();
i = (int)a * 2 - (int)b; // Console.WriteLine(" * 2 - b: " + i);
}
}
, int. , , .
i = (int) ; // int ,
//