Informatiker Board (http://www.informatikerboard.de/board/index.php)
- Themengebiete (http://www.informatikerboard.de/board/board.php?boardid=1)
-- Praktische Informatik (http://www.informatikerboard.de/board/board.php?boardid=6)
--- Typensicherheit (http://www.informatikerboard.de/board/thread.php?threadid=2210)
Geschrieben von Batista am 04.04.2015 um 21:01:
Typensicherheit
.directupload.net/file/d/3947/p53z3ued_jpg.htm
| code: |
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
|
public class Flasche <G extends Getränk, V extends Verschluss> {
private G geschmack;
private V verschluss;
public Flasche ( G geschmack, V verschluss) {
this.geschmack=geschmack;
this.verschluss=verschluss;
}
public Verschluss oeffen() throws Nullpointerexception{
if(this.verschluss==null) {
throw new Nullpointerexception ("Flasche schon geöffnet");
}
else {
Verschluss<V>x=this.verschluss;
this.verschluss=null;
}
return x;
}
public Getraenk leeren() throws Nullpointerexception{
if (this.verschluss==null && this.getraenk!=null){
Getraenk x= this.getraenk;
this.getraenk=null;
return x;
}else {
throw new Nullpointerexception ("Flasche noch zu oder bereits geöffnet");
}
}
|
|
Geschrieben von Batista am 06.04.2015 um 10:29:
RE: Typensicherheit
Da habe ich glatt die Frage vergessen
public Getraenk leeren() throws Nullpointerexception{
if (this.verschluss==null && this.getraenk!=null){
Getraenk x= this.getraenk; a
Getraenk<G> x= this.getraenk;b
Welche der Varianten a oder b ist richtig?
Geschrieben von eulerscheZahl am 06.04.2015 um 12:05:
| code: |
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
|
public class Flasche<G extends Getränk, V extends Verschluss> {
private G getränk;
private V verschluss;
public Flasche(G geschmack, V verschluss) {
this.getränk = geschmack;
this.verschluss = verschluss;
}
public V oeffen() throws NullPointerException {
if (this.verschluss == null) {
throw new NullPointerException("Flasche schon geöffnet");
} else {
V x = this.verschluss;
this.verschluss = null;
return x;
}
}
public G leeren() throws NullPointerException {
if (this.verschluss == null && this.getränk != null) {
G x = this.getränk;
this.getränk = null;
return x;
} else {
throw new NullPointerException(
"Flasche noch zu oder bereits geöffnet");
}
}
} |
|
Du solltest dich entscheiden, ob du den Inhalt Getränk, Getraenk oder Geschmack nennen willst.
Getraenk<G> x ist falsch, entweder Getraenk oder G. Zwecks Typsicherheit solltest du aber ein G zurückgeben, wodurch x auch vom Typ G sein muss.
return x muss innerhalb des if stehen, da x außerhalb nicht definiert ist
und NullPointerException ist CamelCase geschrieben, hat also mehr als einen Großbuchstaben.
Geschrieben von Karlito am 06.04.2015 um 12:13:
Hallo Batista,
weiterhin ist mir aufgefallen, dass die Typparameter und die Parameter im Konstruktor die falsche Reihenfolge haben und ich hätte noch den Hinweis, dass man Sonderzeichen in Variablennamen vermeiden sollte.
Gruß,
Karlito
Geschrieben von Batista am 07.04.2015 um 12:08:
Gut, damit sollte ich bei der Nachholklausur keine Probleme mehr .
Forensoftware: Burning Board, entwickelt von WoltLab GmbH