Konstruktoren

Video

Mit dem Laden des Videos akzeptierst du die Datenschutzerklärung von YouTube. Wenn du die Menge an Daten reduzieren möchtest, die YouTube von dir sammelt, solltest du dich vorher aus deinem YouTube-Account ausloggen, das Speichern von Cookies für das Google-Ads-Programm deaktivieren und/oder Cookies im Browser blockieren.

YouTube immer automatisch laden
Inhaltsverzeichnis
Als Text lesen

Wie dir vielleicht schon aufgefallen ist, werden beim Erstellen von Objekten sehr häufig direkt im Anschluss Attribute dieses Objekts gesetzt. Um diesen Teil etwas zu verschönern gibt es einen besonderen Code-Block: Den Konstruktor

Ein Konstruktor nimmt Werte (Parameter / Argumente) entgegen, die direkt für die Instanziierung des Objekts verwendet werden können. In unserem Rechteck-Beispiel könnte das so aussehen:

public class Rectangle {
double posX;
double posY;
double width;
double height;
// Konstruktor
Rectangle(double initialPosX, double initialPosY, /* Übergebene Parameter */
double initialWidth, double initialHeight) {
// Den Objektvariablen die Werte der lokalen Variablen zuweisen
posX = initialPosX;
posY = initialPosY;
width = initialWidth;
height = initialHeight;
}
}
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

In diesem Beispiel werden dem Konstruktor also alle vier Attribute als lokale Variablen übergeben und anschließend im Objekt selbst gespeichert.

Wie du siehst, müssen hierbei die Benennungen der Parameter angepasst werden, weil es sonst Namenskonflikte gibt. Es gibt aber eine Möglichkeit, trotzdem identische Namen zu verwenden: Das this-Keyword:

class Rectangle {
double posX;
double posY;
double width;
double height;
// Konstruktor
Rectangle(double posX, double posY, double width, double height) {
this.posX = posX;
this.posY = posY;
this.width = width;
this.height = height;
/* ^ ^
* | Bezieht sich auf die übergebene lokale Variable height
* |
* Bezieht sich auf das Objektattribut height
*/
}
}
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Das this-Keyword ermöglicht es uns zu betonen, dass wir etwas aus dem eigenen Objekt meinen.

Der Compiler weiß also durch Verwendung von this, dass in Zeile 9 die in Zeile 2 deklarierte Objektvariable posX gemeint ist, und nicht der gleichnamige Parameter aus Zeile 8. Gleichzeitig ist auch klar, dass ohne Verwendung von this eben doch die übergebene lokale Variable gemeint ist, und nicht die Objektvariable.

Wenn wir nun ein neues Rectangle Objekt instanziieren, können wir direkt die Eigenschaften übergeben:

// Erstelle ein Rechteck an Position (1|2) mit Breite 16 und Höhe 9
Rectangle rect = new Rectangle(1.0, 2.0, 16.0, 9.0);
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Erstelle einen Konstruktor für die in der letzten Aufgabe beschriebenen Klasse User. Dieser soll wie im obigen Beispiel lediglich alle Attribute der Klasse als Parameter entgegennehmen und dann die Objektattribute entsprechend setzen.

Erstelle anschließend in der main-Methode die folgenden User-Objekte:

  • Ted Shmosby, befreundet mit Barnacle, Stuart und Ranjit
  • Barnacle Stinson, befreundet mit Ted und Ranjit
  • Stuart Bowers, befreundet mit Ted
  • Ranjit Singh, befreundet mit Ted und Barnacle

Die ids kannst du frei wählen.

Online Editor

Mit dem Laden des Online Editors akzeptierst du die Datenschutzerklärung von JDoodle, einem Produkt der Nutpan pty Ltd (Australien). JDoodle verwendet die Tools reCAPTCHA und Google Analytics, wodurch Daten an Google übertragen werden können. Ich habe leider keinen Einfluss auf diesen Prozess. Du kannst Google Analytics über ein Browserplugin deaktivieren.

Online Editor immer automatisch laden

Speichere anschließend die vier User in einem Array

User[] users = {tedShmosby, barnacleStinson, stuartBowers, ranjitSingh};
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

und gib aus diesem Array die Namen aller User aus, die mehr als einen Freund haben.

Häufige Fehlerquellen
  • this falsch verwendet
  • Array-Größen falsch
  • error: cannot find symbol: Du hast eine Variable verwendet, bevor sie deklariert wurde. Erstelle zuerst die leeren Arrays, und befülle sie nachdem das User-Objekt instanziiert wurde.
Lösungsvorschlag Teilaufgabe a)
public class User {
int id;
String firstName;
String lastName;
User[] friends;
User(int id, String firstName, String lastName, User[] friends) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.friends = friends;
}
public static void main(String[] args) {
User tedShmosby = new User(0, "Ted", "Shmosby", new User[3]);
User barnacleStinson = new User(1, "Barnacle", "Stinson", new User[2]);
User stuartBowers = new User(2, "Stuart", "Bowers", new User[1]);
User ranjitSingh = new User(3, "Ranjit", "Singh", new User[2]);
tedShmosby.friends[0] = barnacleStinson;
tedShmosby.friends[1] = stuartBowers;
tedShmosby.friends[2] = ranjitSingh;
barnacleStinson.friends[0] = tedShmosby;
barnacleStinson.friends[1] = ranjitSingh;
stuartBowers.friends[0] = tedShmosby;
ranjitSingh.friends[0] = tedShmosby;
ranjitSingh.friends[1] = barnacleStinson;
}
}
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Lösungsvorschlag Teilaufgabe b)
public class User {
int id;
String firstName;
String lastName;
User[] friends;
User(int id, String firstName, String lastName, User[] friends) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.friends = friends;
}
public static void main(String[] args) {
User tedShmosby = new User(0, "Ted", "Shmosby", new User[3]);
User barnacleStinson = new User(1, "Barnacle", "Stinson", new User[2]);
User stuartBowers = new User(2, "Stuart", "Bowers", new User[1]);
User ranjitSingh = new User(3, "Ranjit", "Singh", new User[2]);
tedShmosby.friends[0] = barnacleStinson;
tedShmosby.friends[1] = stuartBowers;
tedShmosby.friends[2] = ranjitSingh;
barnacleStinson.friends[0] = tedShmosby;
barnacleStinson.friends[1] = ranjitSingh;
stuartBowers.friends[0] = tedShmosby;
ranjitSingh.friends[0] = tedShmosby;
ranjitSingh.friends[1] = barnacleStinson;
User[] users = {tedShmosby, barnacleStinson, stuartBowers, ranjitSingh};
for (int i = 0; i < users.length; i++) {
User user = users[i];
if (user.friends.length > 1) {
System.out.println(user.firstName + " " + user.lastName
+ " hat mehr als einen Freund.");
}
}
}
}
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Dir gefällt meine Arbeit?

Unterstütze Mich
AllgemeinSocialRechtliches
HomeInstagramDatenschutz
ÜberYouTubeImpressum
TikTok
Ko-fi

Made with love by a former noob.

© 2022-2024 Timo Friedl