Skip to content
Snippets Groups Projects
Commit 0cebf2be authored by Uwe Wienkop's avatar Uwe Wienkop
Browse files

2020-04-22

parent 05030622
No related branches found
No related tags found
No related merge requests found
......@@ -18,6 +18,7 @@ namespace _01OperatorOverloading
z = Convert.ToInt32(d[0]);
n = Convert.ToInt32(d[1]);
}
public static Bruch Mult1(Bruch b1, Bruch b2)
{
int zaehler = b1.z * b2.z;
......@@ -36,7 +37,13 @@ namespace _01OperatorOverloading
=> new Bruch(b1.z * b2.z, b1.n * b2.n);
public static Bruch operator *(Bruch b1, Bruch b2)
=> new Bruch(b1.z * b2.z, b1.n * b2.n);
public static bool operator ==(Bruch b1, Bruch b2)
=> b1.z == b2.z && b1.n == b2.n;
// nur wenn b1.z == b2.z ist, wird b1.n == b2.n ausgeführt
public static bool operator !=(Bruch b1, Bruch b2)
=> !(b1 == b2);
// Konvertierungsoperatoren:
public static explicit operator int(Bruch b)
=> b.z / b.n;
public static implicit operator double(Bruch b)
......@@ -56,21 +63,39 @@ namespace _01OperatorOverloading
Bruch b2 = new Bruch(2);
Bruch b3 = new Bruch("3/4");
// Bruch e1 = 3.Mult(b1);
// Bruch e1 = b1.Mult(b2); // wäre möglich
// Bruch e1 = b1.Mult(2); // ist möglich
// Bruch e1 = 3.Mult(b1); // ist nicht möglich, da 3 vom Typ int und nicht Bruch ist
Bruch e2 = Bruch.Mult(b1, b2);
//Bruch e2 = Bruch.Mult(2, b2); // nicht nötig, über Konv.Op. erledigt
//Bruch e2 = Bruch.Mult(b1, 2); // nicht nötig, über Konv.Op. erledigt
Bruch e3 = b1 * b2;
Bruch e4 = b1 * 2;
Bruch e5 = 2 * b1;
// 2 --> impl.Konv. in einen Bruch
// Wir haben Bruch.operator*(Bruch, Bruch)
// int.op*, double.op*, Bruch.op*
// Bruch.op*(Bruch, Bruch)
// int->Bruch, Bruch ???
// 2 --> impl.Konv. --> Bruch
// op* (Bruch, Bruch)
// double.op*(int, double) --> double.op*(int-->double, double)
int k2 = 3 * (int)4.5;
int k = (int)e3; // explizite Konvertierung
double d = e3; // implizite Konvertierung
Console.WriteLine(k);
Console.WriteLine(d);
double d2 = 34;
int k2 = (int)3.5;
double d2 = (int)34;
int k3 = (int)3.5;
Bruch b4 = new Bruch(2, 5); // b4: neuer Speicher #100000
Bruch b5 = new Bruch(2, 5); // b5: neuer Speicher #200000
if (b4 == b5)
Console.WriteLine("Sind gleich");
else
Console.WriteLine("Sind unterschiedlich");
}
}
}
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -37,6 +37,8 @@ namespace _01OperatorTrueFalse
public static bool operator !=(LaunchStatus x, LaunchStatus y) => !(x == y);
public override bool Equals(object obj) => obj is LaunchStatus other && this == other;
// Equals testet, ob ein hier: Launchstatus-Obj. gleich einem anderen OBJEKT ist
// Dies setzt gleiche Typen und gleiche Inhalte voraus!
public override int GetHashCode() => status;
}
......@@ -49,11 +51,16 @@ namespace _01OperatorTrueFalse
// Überladung von true und false. Daher wird GetNavigationLaunchStatus nur aufgerufen,
// wenn GetFuelLauchStatus gleich true ist!
LaunchStatus okToLaunch1 = GetFuelLaunchStatus() && GetNavigationLaunchStatus();
// GetFuelLauchStatus() --> LaunchStatus ---(op true)--> bool
// Wenn true --> GetNavigationLaunchStatus() --> LaunschStatus ---(op true)--> bool
// GetFuelLaunchStatus() & GetNavigationLaunchStatus(); --- Einfaches &
Console.WriteLine("----------------");
LaunchStatus okToLaunch2 = GetFuelLaunchStatus() & GetNavigationLaunchStatus();
// Nochmals: Durch das überladene true ist der nachfolgende Ausdruck zulässig!
Console.WriteLine("----------------");
Console.WriteLine(okToLaunch1 ? "Ready to go!" : "Wait!");
// okToLaunch1 ---(op true)--> bool
//if (okToLaunch1.Equals(okToLaunch2))
}
static LaunchStatus GetFuelLaunchStatus()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment