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

Weiter Op-Überladungen

parent d1757915
No related branches found
No related tags found
No related merge requests found
No preview for this file type
No preview for this file type
...@@ -99,6 +99,7 @@ namespace _01Handy ...@@ -99,6 +99,7 @@ namespace _01Handy
class Program class Program
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
Console.WriteLine($"Anzahl Handies: {Handy.HandyCount}"); Console.WriteLine($"Anzahl Handies: {Handy.HandyCount}");
......
...@@ -7,7 +7,8 @@ namespace _02_OpUeberladenBrueche ...@@ -7,7 +7,8 @@ namespace _02_OpUeberladenBrueche
int z, n; int z, n;
public Bruch(int Z, int N=1) { z = Z;n = N; } public Bruch(int Z, int N=1) { z = Z;n = N; }
public override string ToString() => $"{z}/{n}"; public override string ToString() => $"{z}/{n}";
public void Mult1(Bruch br) #region Verschiedene Mult-Varianten
public void Mult1_NichtStatic(Bruch br)
{ {
this.z *= br.z; this.z *= br.z;
this.n *= br.n; this.n *= br.n;
...@@ -24,24 +25,68 @@ namespace _02_OpUeberladenBrueche ...@@ -24,24 +25,68 @@ namespace _02_OpUeberladenBrueche
{ {
return new Bruch(b1.z * b2.z, b1.n * b2.n); return new Bruch(b1.z * b2.z, b1.n * b2.n);
} }
#endregion
public static Bruch operator* (Bruch b1, Bruch b2) public static Bruch operator* (Bruch b1, Bruch b2)
=> new Bruch(b1.z * b2.z, b1.n * b2.n); => new Bruch(b1.z * b2.z, b1.n * b2.n);
public static Bruch operator *(int k, Bruch b2)
=> new Bruch(k * b2.z, b2.n);
public static bool operator ==(Bruch b1, Bruch b2)
=> b1.z == b2.z && b1.n == b2.n;
public static bool operator !=(Bruch b1, Bruch b2)
=> !(b1 == b2);
public static implicit operator Bruch(int k) => new Bruch(k); public static implicit operator Bruch(int k) => new Bruch(k);
// Konvertierung int --> Bruch
public static explicit operator int(Bruch b) => b.z/b.n;
public static implicit operator Bruch(string s)
{
string[] d = s.Split('/');
int z = Convert.ToInt32(d[0]);
int n = Convert.ToInt32(d[1]);
return new Bruch(z, n);
}
} }
enum CarBrand { Leutekutsche, OnlyOffRoad, FaehrtDoch, Zweisitzer }
class Program class Program
{ {
static void g(int p1, double p2)
{
Console.WriteLine(p1);
Console.WriteLine(p2);
}
static void Main(string[] args) static void Main(string[] args)
{ {
Bruch b1 = new Bruch(1, 2); Bruch b1 = new Bruch(1, 2);
Bruch b2 = new Bruch(2, 3); Bruch b2 = new Bruch(2, 3);
Console.WriteLine(b1); Console.WriteLine(b1);
b1.Mult1(b2); string s = b1.ToString();
Bruch b3 = b1.Mult2(b2);
Bruch b4 = b1.Mult2(2); b1.Mult1_NichtStatic(b2); // Erg. wird in b1 gespeichert; Nicht empfehlenswert
Bruch b3 = b1.Mult2(b2); // Erg. wird als NEUES Bruch-Obj. zurückgegeben
Bruch b4 = b1.Mult2(2); // Mult mit int-Konst.; funkt. nur bei b1*2 nicht 2*b1
Bruch b5 = Bruch.Mult3(b1, b2); // Static Impl. von Mult; Bruch*Bruch
Bruch b6 = 2*b2; // Konvertierung von int-2 in Bruch(2,1)
Bruch b62 = "1/3" * (Bruch)"2/6"; // Überladung des *-Operators für die Klasse Bruch
Console.WriteLine(Bruch.Mult3(b1,b2));
// b1*b2 --> Bruch.ToString() --> Ausgabe
int d = (int) (3.14 + 3);
g(2, 3.14);
g(2, 3); // Implizite Konvertierung 3 --> 3.0
g((int) 3.14, 2); // 3.14 -- Explizite Konvertierung zu 3
Bruch b7 = new Bruch (2,3); // Ann. Speicheradr = 100.000
Bruch b8 = new Bruch(2, 3); // Ann. Speicheradr = 200.000
if (b7 == b8)
Console.WriteLine("Die Brüche sind gleich");
else
Console.WriteLine("Die Brüche sind NICHT GLEICH");
Bruch b5 = Bruch.Mult3(b1, b2); //Bruch b9 = new Bruch("4/5");
Bruch b6 = 1 * b2; int d = (1+2)+3;
} }
} }
} }
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment