Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132939 views
License: OTHER
1
public class Student {
2
// die Attribute sind nun nach außen nicht mehr sichtbar
3
private String name;
4
private int semester;
5
private int matriculationNumber;
6
7
public Student(String name, int semester, int matriculationNumber) {
8
// hier wird wie gewohnt alles initialisiert
9
}
10
}
11
12
public class Main {
13
public static void main(String[] args) {
14
Student maxMustermann = new Student("Max Mustermann", 3, 1234567);
15
// hier bekommt man nun einen Compilerfehler
16
maxMustermann.matriculationNumber = 3141592;
17
// ...
18
}
19
}
20
21
public class Student {
22
// ... Attribute, Konstruktor usw. ...
23
24
25
// die getter-Methode für das Attribute 'name'
26
public String getName() {
27
return this.name;
28
}
29
// ... weitere getter-Methoden usw. ...
30
}
31
32
public class Main {
33
public static void main(String[] args) {
34
Student maxMustermann = new Student("Max Mustermann", 3, 1234567);
35
// liest den Namen und gibt ihn aus
36
System.out.println(maxMustermann.getName());
37
// ...
38
}
39
}
40
41