(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Overload true and false for TwoDimension.
using System;
class TwoDimension {
int x, y;
public TwoDimension() {
x = y = 0;
}
public TwoDimension(int i, int j) {
x = i;
y = j;
}
// Overload true.
public static bool operator true(TwoDimension op) {
if((op.x != 0) || (op.y != 0))
return true; // at least one coordinate is non-zero
else
return false;
}
// Overload false.
public static bool operator false(TwoDimension op) {
if((op.x == 0) && (op.y == 0))
return true; // all coordinates are zero
else
return false;
}
// Overload unary --.
public static TwoDimension operator --(TwoDimension op)
{
// for ++, modify argument
op.x--;
op.y--;
return op;
}
// Show X, Y, Z coordinates.
public void show()
{
Console.WriteLine(x + ", " + y);
}
}
class MainClass {
public static void Main() {
TwoDimension a = new TwoDimension(5, 6);
TwoDimension b = new TwoDimension(10, 10);
TwoDimension c = new TwoDimension(0, 0);
Console.Write("Here is a: ");
a.show();
Console.Write("Here is b: ");
b.show();
Console.Write("Here is c: ");
c.show();
Console.WriteLine();
if(a)
Console.WriteLine("a is true.");
else
Console.WriteLine("a is false.");
if(b)
Console.WriteLine("b is true.");
else
Console.WriteLine("b is false.");
if(c)
Console.WriteLine("c is true.");
else
Console.WriteLine("c is false.");
Console.WriteLine();
Console.WriteLine("Control a loop using a TwoDimension object.");
do {
b.show();
b--;
} while(b);
}
}
Here is a: 5, 6
Here is b: 10, 10
Here is c: 0, 0
a is true.
b is true.
c is false.
Control a loop using a TwoDimension object.
10, 10
9, 9
8, 8
7, 7
6, 6
5, 5
4, 4
3, 3
2, 2
1, 1
true and false operator
using System;
public class MyType
{
public static bool operator true ( MyType e )
{
return ( e == null ) ? false : e.b;
}
public static bool operator false ( MyType e )
{
return ( e == null ) ? true : !e.b;
}
public bool b;
public MyType( bool b )
{
this.b = b;
}
public static void Main( string[] args )
{
MyType myTrue = new MyType( true );
MyType myFalse = new MyType( false );
MyType myNull = null;
if ( myTrue )
{
System.Console.WriteLine( "true" );
}
else
{
System.Console.WriteLine( "false" );
}
if ( myFalse )
{
System.Console.WriteLine( "true" );
}
else
{
System.Console.WriteLine( "false" );
}
if ( myNull )
{
System.Console.WriteLine( "true" );
}
else
{
System.Console.WriteLine( "false" );
}
}
}
true
false
false
true/false operator for Complex
using System;
public struct Complex
{
public Complex( double real, double imaginary ) {
this.real = real;
this.imaginary = imaginary;
}
public override string ToString() {
return String.Format( "({0}, {1})", real, imaginary );
}
public double Magnitude {
get {
return Math.Sqrt( Math.Pow(this.real, 2) + Math.Pow(this.imaginary, 2) );
}
}
public static bool operator true( Complex c ) {
return (c.real != 0) || (c.imaginary != 0);
}
public static bool operator false( Complex c ) {
return (c.real == 0) && (c.imaginary == 0);
}
private double real;
private double imaginary;
}
public class MainClass
{
static void Main() {
Complex cpx1 = new Complex( 1.0, 3.0 );
if( cpx1 ) {
Console.WriteLine( "cpx1 is true" );
} else {
Console.WriteLine( "cpx1 is false" );
}
Complex cpx2 = new Complex( 0, 0 );
Console.WriteLine( "cpx2 is {0}", cpx2 ? "true" : "false" );
}
}
cpx1 is true
cpx2 is false