From 7f5fedfe166547f76d7dd8300dc73310ac021fd8 Mon Sep 17 00:00:00 2001 From: sebi Date: Mon, 22 Jul 2024 11:20:25 +0200 Subject: [PATCH] Inteface --- Exercises/E42_Interfaces/Exercise_1.cs | 45 ++++++++++++++++++++++++++ Exercises/E42_Interfaces/Exercise_2.cs | 40 +++++++++++++++++++++++ Exercises/E42_Interfaces/Exercise_3.cs | 10 ++++++ Program.cs | 29 +++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 Exercises/E42_Interfaces/Exercise_1.cs create mode 100644 Exercises/E42_Interfaces/Exercise_2.cs create mode 100644 Exercises/E42_Interfaces/Exercise_3.cs diff --git a/Exercises/E42_Interfaces/Exercise_1.cs b/Exercises/E42_Interfaces/Exercise_1.cs new file mode 100644 index 0000000..97a4088 --- /dev/null +++ b/Exercises/E42_Interfaces/Exercise_1.cs @@ -0,0 +1,45 @@ +namespace Exercises_C_Sharp.E42_Interfaces +{ + //Sie sehen unten drei verschiedene Klassen aus drei Subsystemen Ihrer Firma. Diese verlangt jetzt von Ihnen, dass alle Namen auf der Konsole ausgegeben werden sollen. Schreiben Sie dafür ein Interface und implementieren Sie dieses bei den Klassen. + //Vergessen Sie nicht, den Typen dynamic auf den Namen Ihres Interfaces zu ändern. + class Exercise_1 + { + public static void Start() + { + List NamesList = new(){ new Person("Matteo"), new Customer("Emilia"), new Human("Noah"), new Person("Emma"), new Customer("Finn"), new Human("Hannah"), new Person("Luca"), new Customer("Sophia"), new Human("Leon"), new Person("Mia"), new Customer("Henry"), new Human("Lina"), new Person("Elias"), new Customer("Ella"), new Human("Paul") }; + + foreach(var element in NamesList) + element.PrintName(); + + } + } + + //Code START + + //Code ENDE + + class Person{ + string _personName; + public Person(string name) + { + _personName = name; + } + + } + class Customer{ + string _customerName; + public Customer(string name) + { + _customerName = name; + } + + } + class Human{ + string _humanName; + public Human(string name) + { + _humanName = name; + } + + } +} \ No newline at end of file diff --git a/Exercises/E42_Interfaces/Exercise_2.cs b/Exercises/E42_Interfaces/Exercise_2.cs new file mode 100644 index 0000000..6c114b0 --- /dev/null +++ b/Exercises/E42_Interfaces/Exercise_2.cs @@ -0,0 +1,40 @@ +namespace Exercises_C_Sharp.E42_Interfaces +{ + //Sie sollen für einen Sondmixer die Klassen Drums, Guitar und Keyboard erstellen. Alle drei sollen von ISound ableiten. Um die Applikation zu testen, geben Sie bei der implementierten Methoden ein zufälliges Byte-Array zurück. Erstellen Sie dann die drei Objekte der jeweiligen Klassen und übergeben diese der Methode MergeSound(). + + class Exercise_2 + { + public static void Start() + { + //Code START + + //Code ENDE + } + + public static void MergeSound(List sounds) + { + List byteList = new(); + foreach(var element in sounds) + { + byte[] bytes = element.MakeSound(); + for(int i = 0; i < bytes.Length; i++) + { + if(byteList.Count <= i) + byteList.Add(bytes[i]); + else + byteList[i] = (byte)(byteList[i] | bytes[i]); + } + } + + //ToDo!!!! + } + } + + interface ISound{ + byte[] MakeSound(); + } + + //Code START + + //Code ENDE +} \ No newline at end of file diff --git a/Exercises/E42_Interfaces/Exercise_3.cs b/Exercises/E42_Interfaces/Exercise_3.cs new file mode 100644 index 0000000..cd76402 --- /dev/null +++ b/Exercises/E42_Interfaces/Exercise_3.cs @@ -0,0 +1,10 @@ +namespace Exercises_C_Sharp.E42_Interfaces +{ + class Exercise_3 + { + public static void Start() + { + //ToDo!!!! + } + } +} \ No newline at end of file diff --git a/Program.cs b/Program.cs index 9fb4ec2..6695c76 100644 --- a/Program.cs +++ b/Program.cs @@ -598,6 +598,33 @@ namespace Exercises_C_Sharp } }; //************************ + //******* Vererbung ****** + //************************ + ExerciseGroup vererbungElements = new() + { + Name = "40. Vererbung", + ElementList = new List() + { + new(){Name = "Aufgabe 1", Method = Exercises_C_Sharp.E40_Vererbung.Exercise_1.Start}, + new(){Name = "Aufgabe 2", Method = Exercises_C_Sharp.E40_Vererbung.Exercise_2.Start}, + new(){Name = "Aufgabe 3", Method = Exercises_C_Sharp.E40_Vererbung.Exercise_3.Start}, + new(){Name = "Aufgabe 3", Method = Exercises_C_Sharp.E40_Vererbung.Exercise_4.Start} + } + }; + //************************ + //******* Interfaces ***** + //************************ + ExerciseGroup interfaceElements = new() + { + Name = "42. Interfaces", + ElementList = new List() + { + new(){Name = "Aufgabe 1", Method = Exercises_C_Sharp.E42_Interfaces.Exercise_1.Start}, + new(){Name = "Aufgabe 2", Method = Exercises_C_Sharp.E42_Interfaces.Exercise_2.Start}, + new(){Name = "Aufgabe 3", Method = Exercises_C_Sharp.E42_Interfaces.Exercise_3.Start} + } + }; + //************************ //******* Kapselung ****** //************************ ExerciseGroup kapselungElements = new() @@ -734,6 +761,8 @@ namespace Exercises_C_Sharp fileformatsElements, serializeElements, mysqlElements, + vererbungElements, + interfaceElements, kapselungElements, patternElements,