From 2d14c38c6987b2943232303c39f849ceffa4ba6d Mon Sep 17 00:00:00 2001 From: Uwe Wienkop <uwe.wienkop@th-nuernberg.de> Date: Tue, 22 Jun 2021 11:19:23 +0200 Subject: [PATCH] 2021-06-22 Generics --- 07-1 Intro Interfaces/Classes.cs | 27 +++++++- 07-1 Intro Interfaces/Program.cs | 4 +- 08-2 Generics Intro/Program.cs | 18 +++-- 09-1 KeyValueList/KeyValueList.cs | 8 ++- 09-1 KeyValueList/Program.cs | 2 + .../13-2 VererbungWdhlg.csproj | 9 +++ 13-2 VererbungWdhlg/Program.cs | 68 +++++++++++++++++++ 13-Ubg ApplyAll/Program.cs | 20 ++++-- Prog2WienkopSS2021.sln | 8 ++- 9 files changed, 147 insertions(+), 17 deletions(-) create mode 100644 13-2 VererbungWdhlg/13-2 VererbungWdhlg.csproj create mode 100644 13-2 VererbungWdhlg/Program.cs diff --git a/07-1 Intro Interfaces/Classes.cs b/07-1 Intro Interfaces/Classes.cs index a089c53..b3a1984 100644 --- a/07-1 Intro Interfaces/Classes.cs +++ b/07-1 Intro Interfaces/Classes.cs @@ -19,6 +19,9 @@ namespace _07_1_Intro_Interfaces } + + + abstract class Kfz : IBesteuerbar, IVerkaufbar { // Je nach Anwendung: Property entweder in Kfz realisieren ... @@ -29,7 +32,17 @@ namespace _07_1_Intro_Interfaces //public abstract double Wert { get; set; } public abstract double Steuern(); - public abstract void Verkaufen(); + + // public void Verkaufen() {} + // -- Erfüllung des Interfaces + // public virtual void Verkaufen() {} + // -- Erfüllung des Interfaces PLUS kann in abgeleiteten Klassen spezialisiert werden + // public abstract void Verkaufen(); + // -- Erkenne die Verpflichtung des Interfaces an Erfüllung in den abgeleiteten Klassen + public virtual void Verkaufen() + { + Console.WriteLine("Verkauft"); + } public abstract void Fahren(); } @@ -47,11 +60,21 @@ namespace _07_1_Intro_Interfaces return 100; } + //public override void Verkaufen() + //{ + // Console.WriteLine("Pkw wird verkauft"); + //} + } + + class Sportwagen : Pkw + { public override void Verkaufen() { - Console.WriteLine("Pkw wird verkauft"); + Console.WriteLine("Sportwagen wird verkauft"); } } + + //class Motorrad : Kfz //{ diff --git a/07-1 Intro Interfaces/Program.cs b/07-1 Intro Interfaces/Program.cs index 73d2444..2c63964 100644 --- a/07-1 Intro Interfaces/Program.cs +++ b/07-1 Intro Interfaces/Program.cs @@ -17,8 +17,8 @@ namespace _07_1_Intro_Interfaces kfz.Fahren(); IVerkaufbar[] verkObj = new IVerkaufbar[3]; - verkObj[0] = new Pkw(); - verkObj[1] = new Pflanzen(); + verkObj[0] = new Sportwagen(); + verkObj[1] = new Pkw(); verkObj[2] = new Immobilien(); foreach (var item in verkObj) diff --git a/08-2 Generics Intro/Program.cs b/08-2 Generics Intro/Program.cs index 34a720c..5c15309 100644 --- a/08-2 Generics Intro/Program.cs +++ b/08-2 Generics Intro/Program.cs @@ -3,7 +3,7 @@ using System.Diagnostics.CodeAnalysis; namespace _08_2_Generics_Intro { - class Person : IComparable<Person> + class Person : IComparable<Person> { string name, vorname; public Person(string Name, string Vorname) { name = Name;vorname = Vorname; } @@ -28,14 +28,22 @@ namespace _08_2_Generics_Intro //{ // return (a < b) ? a : b; //} + //static int Min(int a, int b) + //{ + // return (a < b) ? a : b; + //} //static string Min(string a, string b) //{ // return (a.CompareTo(b) < 0) ? a : b; //} - static TYP Min<TYP>(TYP a, TYP b) where TYP:IComparable<TYP> - { - return (a.CompareTo(b)<0) ? a : b; + static T Min<T>(T a, T b) where T:IComparable<T> + { // IComparable<TYP>: int CompareTo(T other) | "Nur" IComparable: int CompareTo(object other) + return (a.CompareTo(b)<0) ? a : b; // a.CompareTo(b) :: a-b } + + + + //static object Min(object a, object b) //{ // return (a.CompareTo(b) < 0) ? a : b; @@ -63,7 +71,7 @@ namespace _08_2_Generics_Intro static void Main(string[] args) { Console.WriteLine($"Minimum von 4 und 7: {Min(4, 7)}"); - Console.WriteLine($"Minimum von 7.5 und 7.3: {Min(7.5, 7.3)}"); + Console.WriteLine($"Minimum von 7.5 und 7.3: {Min(7.5, 7)}"); Console.WriteLine($"Minimum von Berta und Anton: {Min("Berta", "Anton")}"); Console.WriteLine($"Minimum von zwei Person-Objekten: {Min(new Person("Meier","Anton"), new Person ("Meier","Berta"))}"); } diff --git a/09-1 KeyValueList/KeyValueList.cs b/09-1 KeyValueList/KeyValueList.cs index 0ff2f7a..88f24a9 100644 --- a/09-1 KeyValueList/KeyValueList.cs +++ b/09-1 KeyValueList/KeyValueList.cs @@ -1,10 +1,11 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Text; namespace _09_1_KeyValueList { - class KeyValueList<K,V> where K:IEquatable<K> + class KeyValueList<K,V>:IComparable<KeyValueList<K,V>> where K:IComparable<K> { private class KVItem { @@ -17,6 +18,7 @@ namespace _09_1_KeyValueList KVItem first = null, last = null; int count = 0; + public int CompareTo(KeyValueList<K, V> other) => 0; // Noch Dummy-Implementierung private void AddEnd(K key, V val) { KVItem newItem = new KVItem(key, val); // 1. Neues Element anlegen @@ -37,7 +39,7 @@ namespace _09_1_KeyValueList private KVItem ItemSearch(K searchKey) { for (KVItem item = first; item != null; item = item.next) - if (item.key.Equals(searchKey)) + if (item.key.CompareTo(searchKey)==0) return item; return null; } @@ -65,5 +67,7 @@ namespace _09_1_KeyValueList // ... return res; } + + } } diff --git a/09-1 KeyValueList/Program.cs b/09-1 KeyValueList/Program.cs index 63124c8..54500aa 100644 --- a/09-1 KeyValueList/Program.cs +++ b/09-1 KeyValueList/Program.cs @@ -99,6 +99,8 @@ namespace _09_1_KeyValueList Console.WriteLine(item); } #endregion + + KeyValueList<KeyValueList<string, string>, string> terminkalender3; } } } diff --git a/13-2 VererbungWdhlg/13-2 VererbungWdhlg.csproj b/13-2 VererbungWdhlg/13-2 VererbungWdhlg.csproj new file mode 100644 index 0000000..f65387b --- /dev/null +++ b/13-2 VererbungWdhlg/13-2 VererbungWdhlg.csproj @@ -0,0 +1,9 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>netcoreapp3.1</TargetFramework> + <RootNamespace>_13_2_VererbungWdhlg</RootNamespace> + </PropertyGroup> + +</Project> diff --git a/13-2 VererbungWdhlg/Program.cs b/13-2 VererbungWdhlg/Program.cs new file mode 100644 index 0000000..173ab78 --- /dev/null +++ b/13-2 VererbungWdhlg/Program.cs @@ -0,0 +1,68 @@ +using System; + +namespace _13_2_VererbungWdhlg +{ + interface IBetankbar { + void tanke(); + } + interface IBefuellbar : IBetankbar + { + double Fuellstand { get; set; } + void befuelle(); + } + interface ILadbar : IBetankbar + { + double Ladestand { get; set; } + void lade(); + } + + + +abstract class FossilerAntrieb : IBefuellbar +{ + [8] double fuellstand; + public double[9] { + get { return fuellstand; } +set +{ if (value >= 0.0 && value <= 1.0) fuellstand = value; } + } + [10] void befuelle(); +[11] () { + if (fuellstand < 0.5) befuelle(); + else Console.WriteLine("Noch fahrbereit"); +} +} + +abstract class ElektrischerAntrieb : ILadbar +{ + [14] double ladestand; + public double[15] { + get { return ladestand; } +set +{ + if (value >= 0.0 && value <= 1.0) ladestand = value; +} +[16] void lade(); +[17] () { + if (ladestand < 0.5) lade(); + else Console.WriteLine("Noch fahrbereit"); +} +} +class ElektrischesFZ : ElektrischerAntrieb +{ + [18] + void lade() + { + Console.WriteLine("Ladestecker entnehmen, warten, Ladestecker in Halterung."); + ladestand = 1.0; + } +} + +class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} diff --git a/13-Ubg ApplyAll/Program.cs b/13-Ubg ApplyAll/Program.cs index f5fe9ab..7620148 100644 --- a/13-Ubg ApplyAll/Program.cs +++ b/13-Ubg ApplyAll/Program.cs @@ -18,7 +18,7 @@ namespace _13_Ubg_ApplyAll delegate T MeineFkt<T>(T x); //delegate int MeineFkt<int>(int x) delegate bool MeinFilter<T>(T x); - + //delegate bool Predicate<T>(T x); // static int[] ApplyAll<int>(MeineFkt<int> fkt, params int[] werte) // static string[] ApplyAll<string>(MeineFkt<string> fkt, params string[] werte) @@ -34,7 +34,7 @@ namespace _13_Ubg_ApplyAll return res; } - static T[] Select<T>(MeinFilter<T> filter, params T[] werte) + static T[] Select<T>(Predicate<T> filter, params T[] werte) { List<T> res = new List<T>(); foreach (var item in werte) @@ -48,14 +48,24 @@ namespace _13_Ubg_ApplyAll Console.WriteLine(); return res.ToArray(); } + + static void Main(string[] args) { ApplyAll(x => x + 1, 10, 30, 40, 50, 60, 70, 80, 100); - ApplyAll(x => (int) (x * 0.5), 10, 30, 40, 50, 60, 70, 80, 100); + + ApplyAll(x => (int)(x * 0.5), 10, 30, 40, 50, 60, 70, 80, 100); + ApplyAll(delegate (int x) { return (int)(x * 0.5); }, 10, 30, 40, 50, 60, 70, 80, 100); + ApplyAll(delegate (int x) { return x + 1; }, ApplyAll(x => 2 * x, 10, 20, 30)); - ApplyAll(x => x + 1, "Anton","Berta","Claudia"); + ApplyAll(x => x + 1, "Anton", "Berta", "Claudia"); + ApplyAll(x => x.ToUpper(), "Anton", "Berta", "Claudia"); + + Select(x => x > 50, 10, 30, 40, 50, 60, 70, 80, 100); + Select(x => x.Contains("er"), "Anton", "Berta", "Claudia", "Dieter"); + Select(x => true, "Anton", "Berta", "Claudia", "Dieter"); - Select(x => x>50, 10, 30, 40, 50, 60, 70, 80, 100); + ApplyAll(x => x.ToUpper(), Select(x => x.Contains("er"), "Anton", "Berta", "Claudia", "Dieter")); } } } diff --git a/Prog2WienkopSS2021.sln b/Prog2WienkopSS2021.sln index c72ef9e..06994d5 100644 --- a/Prog2WienkopSS2021.sln +++ b/Prog2WienkopSS2021.sln @@ -111,7 +111,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "12-3 VererbungRedo", "12-3 EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "13-1 ApplyAll", "13-Ubg ApplyAll\13-1 ApplyAll.csproj", "{1335DAC1-39A7-4CB1-9893-F4017A57E2BA}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "13-UbgCycleList-Mo", "13-UbgCycleList-Mo\13-UbgCycleList-Mo.csproj", "{C1594C99-2A3F-4948-AC3E-ED7D16379C48}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "13-UbgCycleList-Mo", "13-UbgCycleList-Mo\13-UbgCycleList-Mo.csproj", "{C1594C99-2A3F-4948-AC3E-ED7D16379C48}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "13-2 VererbungWdhlg", "13-2 VererbungWdhlg\13-2 VererbungWdhlg.csproj", "{D0B0CCA1-1E7B-4C35-BAD2-AABD1993E9A8}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -339,6 +341,10 @@ Global {C1594C99-2A3F-4948-AC3E-ED7D16379C48}.Debug|Any CPU.Build.0 = Debug|Any CPU {C1594C99-2A3F-4948-AC3E-ED7D16379C48}.Release|Any CPU.ActiveCfg = Release|Any CPU {C1594C99-2A3F-4948-AC3E-ED7D16379C48}.Release|Any CPU.Build.0 = Release|Any CPU + {D0B0CCA1-1E7B-4C35-BAD2-AABD1993E9A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D0B0CCA1-1E7B-4C35-BAD2-AABD1993E9A8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D0B0CCA1-1E7B-4C35-BAD2-AABD1993E9A8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D0B0CCA1-1E7B-4C35-BAD2-AABD1993E9A8}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE -- GitLab