klassen2:constructors:start
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
— | klassen2:constructors:start [2024/08/31 10:03] (aktuell) – angelegt - Externe Bearbeitung 127.0.0.1 | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
+ | ====== Konstruktoren ====== | ||
+ | {{youtube> | ||
+ | |||
+ | Wir haben inzwischen schon viele Objekte instanziert, | ||
+ | < | ||
+ | |||
+ | <div class=" | ||
+ | |||
+ | <script type=" | ||
+ | new Rectangle(50, | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | |||
+ | </ | ||
+ | <WRAP center round info 60%> | ||
+ | Beim Instanzieren eines neuen Objekts mithilfe des Schlüsselwortes '' | ||
+ | </ | ||
+ | |||
+ | Wir lernen in diesem Kapitel, wie wir die Konstruktoren unserer Klassen selbst schreiben können. | ||
+ | |||
+ | ===== Beispiel 1: Würfel ===== | ||
+ | Programmiere eine Klasse '' | ||
+ | <code MyJava> | ||
+ | Würfel w6 = new Würfel(6); | ||
+ | Würfel w20 = new Würfel(20); | ||
+ | println(w6.würfle()); | ||
+ | println(w20.würfle()); | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | |||
+ | <div class=" | ||
+ | |||
+ | <script type=" | ||
+ | Würfel w6 = new Würfel(6); | ||
+ | Würfel w20 = new Würfel(20); | ||
+ | println(w6.würfle()); | ||
+ | println(w20.würfle()); | ||
+ | |||
+ | class Würfel{ | ||
+ | int seitenzahl; | ||
+ | |||
+ | // Das ist die Konstruktor-Methode ("der Konstruktor" | ||
+ | // dass sie den Bezeichner der Klasse (" | ||
+ | | ||
+ | seitenzahl = s; | ||
+ | } | ||
+ | |||
+ | int würfle(){ | ||
+ | return Math.floor(Math.random()*seitenzahl) + 1; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ===== Beispiel 2: Buntstift ===== | ||
+ | Schau' Dir das folgende Beispielprogramm an und überlege Dir, an welchen Stellen des Programms der Konstruktor '' | ||
+ | |||
+ | < | ||
+ | |||
+ | <div class=" | ||
+ | |||
+ | <script type=" | ||
+ | Buntstift rotstift = new Buntstift(Color.red); | ||
+ | rotstift.schreibe(" | ||
+ | |||
+ | Buntstift grünstift = new Buntstift(Color.lime); | ||
+ | grünstift.schreibe(" | ||
+ | rotstift.schreibe(" | ||
+ | |||
+ | class Buntstift { | ||
+ | |||
+ | Color farbe; | ||
+ | |||
+ | // Das ist die Konstruktor-Methode ("der Konstruktor" | ||
+ | // dass sie den Bezeichner der Klasse (" | ||
+ | | ||
+ | farbe = farbe1; | ||
+ | } | ||
+ | |||
+ | void schreibe(String text) { | ||
+ | println(text, | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | ===== Aufgabe ===== | ||
+ | Führe das Programm Schrittweise mit "step into ({{: | ||
+ | |||
+ | Die Definition des Konstruktors der Klasse '' | ||
+ | |||
+ | <code learnj [enable_line_numbers=" | ||
+ | | ||
+ | farbe = farbe1; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <WRAP center round info 60%> | ||
+ | Eine **Konstruktordefinition** sieht aus wie die Definition einer " | ||
+ | * Sie besitzt keinen Rückgabetyp. | ||
+ | * Ihr Bezeichner stimmt exakt mit dem Bezeichner der Klasse überein. | ||
+ | Findet der Compiler eine Methodendefinition mit diesen Eigenschaften, | ||
+ | </ | ||
+ | Im Falle der Klasse '' | ||
+ | <code learnj [enable_line_numbers=" | ||
+ | | ||
+ | farbe = farbe1; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Das Schlüsselwort " | ||
+ | Hat es Dich gewundert, warum wir den Parameter im Konstruktor oben '' | ||
+ | < | ||
+ | <div class=" | ||
+ | <script type=" | ||
+ | Buntstift rotstift = new Buntstift(Color.red); | ||
+ | rotstift.schreibe(" | ||
+ | |||
+ | class Buntstift { | ||
+ | |||
+ | Color farbe; | ||
+ | |||
+ | | ||
+ | farbe = farbe; | ||
+ | } | ||
+ | |||
+ | | ||
+ | println(text, | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | Führe das Programm wieder Schrittweise mit "step into ({{: | ||
+ | |||
+ | <WRAP center round tip 60%> | ||
+ | **Tipp:** Klicke mit der Maus auf den Bezeichner '' | ||
+ | </ | ||
+ | |||
+ | Das Problem besteht darin, dass der Parameter '' | ||
+ | <code learnj> | ||
+ | farbe = farbe; | ||
+ | </ | ||
+ | in Zeile 9, dass der Wert des Parameters '' | ||
+ | Wir müssen dem Compiler irgendwie mitteilen, dass der Bezeichner '' | ||
+ | <code learnj> | ||
+ | this.farbe = farbe; | ||
+ | </ | ||
+ | Klicke im unteren Beispiel auf die verschiedenen Vorkommen des Bezeichners '' | ||
+ | < | ||
+ | <div class=" | ||
+ | <script type=" | ||
+ | Buntstift rotstift = new Buntstift(Color.red); | ||
+ | rotstift.schreibe(" | ||
+ | |||
+ | class Buntstift { | ||
+ | |||
+ | Color farbe; | ||
+ | |||
+ | | ||
+ | this.farbe = farbe; | ||
+ | } | ||
+ | |||
+ | | ||
+ | println(text, | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ===== Beispiel 3: Polynomrechner ===== | ||
+ | Die Klasse '' | ||
+ | |||
+ | < | ||
+ | |||
+ | <div class=" | ||
+ | |||
+ | <script type=" | ||
+ | PolynomZweitenGrades p = new PolynomZweitenGrades(1, | ||
+ | println(" | ||
+ | println(" | ||
+ | |||
+ | class PolynomZweitenGrades { | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | this.a = a; | ||
+ | this.b = b; | ||
+ | this.c = c; | ||
+ | } | ||
+ | |||
+ | | ||
+ | return a * x * x + b * x + c; | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Aufgabe 1: Die Klasse Bruch ===== | ||
+ | Im vorhergehenden Kapitel haben wir eine [[[klassen2: | ||
+ | <code myJava> | ||
+ | Bruch b1 = new Bruch(); | ||
+ | b1.zähler = 3; | ||
+ | b1.nenner = 4; | ||
+ | </ | ||
+ | einfach schreiben kann | ||
+ | <code myJava> | ||
+ | Bruch b1 = new Bruch(3,4); | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | |||
+ | <div class=" | ||
+ | |||
+ | <script type=" | ||
+ | |||
+ | Bruch b1 = new Bruch(4, 3); | ||
+ | Bruch b2 = new Bruch(1, 2); | ||
+ | b1.ausgabeUnecht(); | ||
+ | b1.ausgabeEcht(); | ||
+ | b1.mal(b2); | ||
+ | b1.ausgabeUnecht(); | ||
+ | |||
+ | class Bruch { | ||
+ | |||
+ | int z; | ||
+ | int n; | ||
+ | |||
+ | // Hier fehlt der Konstruktor! | ||
+ | |||
+ | | ||
+ | println(z + "/" | ||
+ | } | ||
+ | |||
+ | | ||
+ | |||
+ | int a1 = z; | ||
+ | |||
+ | if(Math.abs(z) >= n) { | ||
+ | int ganzerAnteil = z / n; | ||
+ | a1 = z - ganzerAnteil * n; | ||
+ | if(a1 < 0) { | ||
+ | a1 = -a1; | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | | ||
+ | println(a1 + "/" | ||
+ | | ||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | z = z * b2.z; | ||
+ | n = n * b2.n; | ||
+ | |||
+ | kürze(); | ||
+ | |||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | z = z * b2.n; | ||
+ | n = n * b2.z; | ||
+ | |||
+ | kürze(); | ||
+ | |||
+ | } | ||
+ | |||
+ | | ||
+ | |||
+ | // a/b + c/d = (a*d)/(b*d) + (c*b)/(d*b) = (a*d + c*b)/(b*d) | ||
+ | |||
+ | z =(z * b2.n + b2.z * n); | ||
+ | n = n * b2.n; | ||
+ | |||
+ | kürze(); | ||
+ | |||
+ | } | ||
+ | |||
+ | | ||
+ | |||
+ | // a/b - c/d = (a*d)/(b*d) - (c*b)/(d*b) = (a*d - c*b)/(b*d) | ||
+ | |||
+ | z =(z * b2.n - b2.z * n); | ||
+ | n = n * b2.n; | ||
+ | |||
+ | kürze(); | ||
+ | |||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | for(int i = 2; i <= Math.sqrt(Math.abs(z)); | ||
+ | |||
+ | if(z % i == 0 && n % i == 0) { | ||
+ | z = z / i; | ||
+ | n = n / i; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | </ | ||
+ | </ | ||
+ | [[.loesung: | ||
+ | |||
+ | ===== Aufgabe 2: Die Klasse Tier ===== | ||
+ | Füge der [[klassen2: | ||
+ | <code myJava> | ||
+ | Tier p = new Tier(" | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | |||
+ | <div class=" | ||
+ | |||
+ | <script type=" | ||
+ | Tier p = new Tier(" | ||
+ | p.vorstellen(); | ||
+ | |||
+ | class Tier { | ||
+ | | ||
+ | | ||
+ | int beinzahl; | ||
+ | |||
+ | // Hier fehlt der Konstruktor! | ||
+ | |||
+ | void vorstellen(){ | ||
+ | println(" | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | </ | ||
+ | [[.loesung: |