dynamischer / statischer typ

J

Javaneuling223

Gast
Hallo liebe Forenmitglieder. ICh bräuchte mal ein bisschen Hilfe bitte. Und Zwar habe ich folgendes:

1 c l a s s Human{
2 publ i c s t a t i c i n t typeCode = 0 ;
3 publ i c i n t getTypeCode ( ) { r e turn typeCode ; }
4 publ i c S t r i n g c a t e g o r y ( ) { r e turn "Normalo " ; }
5 publ i c s t a t i c S t r i n g s p e c i a l P owe r ( ) { r e turn " Keine " ; }
6 }
7 c l a s s S u p e rVi l l a i n ext ends Human{
8 publ i c s t a t i c i n t typeCode = 2 ;
9 publ i c S t r i n g c a t e g o r y ( ) { r e turn " Boe s ewi cht " ; }
10 publ i c s t a t i c S t r i n g s p e c i a l P owe r ( ) { r e turn " Fr e e z eRay " ; }
11 }
12 c l a s s SuperHero ext ends Human{
13 publ i c s t a t i c i n t typeCode = 1 ;
14 publ i c i n t getTypeCode ( ) { r e turn typeCode ; }
15 publ i c S t r i n g c a t e g o r y ( ) { r e turn " Held " ; }
16 publ i c s t a t i c S t r i n g s p e c i a l P owe r ( ) { r e turn "Hammer " ; }
17 }
18
19 publ i c c l a s s Typed {
20 publ i c s t a t i c void main ( S t r i n g [ ] a r g s ) {
21 Human p = new SuperHero ( ) ;
22 p = new Human ( ) ;
23 p = new S u p e rVi l l a i n ( ) ;
24
25 SuperHero z = new SuperHero ( ) ;
26 }
27 }

So, nun zu meiner Frage. Ich verstehe das nämlich nicht ganz. Ich muss aus Zeile 21 22 und 23 herauslesen was der statische und dynamische Typ der Variable p ist. könntet ihr mir bisschen erklären wie ich das erkenne? bei z nach zeile 25 ist es das gleiche.

die letze frage dazu ist wie folgt:

Geben Sie jeweils an, ob der dynamische oder der statische Typ der Referenzvariablen bei
folgenden Aufrufen bzw. Feldzugriffen entscheidet, welche Version der betroffenen polymorphen
Methode oder Variable verwendet wird:
(a) Aufruf einer statischen Methode
(b) Lesen einer statischen Variablen
(c) Aufruf einer Instanz-Methode
(d) Schreiben einer Instanz-Variablen

bitte natülich nicht um lösungen, sondern um hilfe wie ich diese aufgabe beweltigen kann.
 

XHelp

Top Contributor
Poste mal den Code mit sinnvoller Formatierung und mit Verwendung von JAVA-Tags (wie es übrigens ROT über dem Textfeld steht)
 
J

Javaneuling223

Gast
Java:
1 c l a s s Human{
2 publ i c s t a t i c i n t typeCode = 0 ;
3 publ i c i n t getTypeCode ( ) { r e turn typeCode ; }
4 publ i c S t r i n g c a t e g o r y ( ) { r e turn "Normalo " ; }
5 publ i c s t a t i c S t r i n g s p e c i a l P owe r ( ) { r e turn " Keine " ; }
6 }
7 c l a s s S u p e rVi l l a i n ext ends Human{
8 publ i c s t a t i c i n t typeCode = 2 ;
9 publ i c S t r i n g c a t e g o r y ( ) { r e turn " Boe s ewi cht " ; }
10 publ i c s t a t i c S t r i n g s p e c i a l P owe r ( ) { r e turn " Fr e e z eRay " ; }
11 }
12 c l a s s SuperHero ext ends Human{
13 publ i c s t a t i c i n t typeCode = 1 ;
14 publ i c i n t getTypeCode ( ) { r e turn typeCode ; }
15 publ i c S t r i n g c a t e g o r y ( ) { r e turn " Held " ; }
16 publ i c s t a t i c S t r i n g s p e c i a l P owe r ( ) { r e turn "Hammer " ; }
17 }
18
19 publ i c c l a s s Typed {
20 publ i c s t a t i c void main ( S t r i n g [ ] a r g s ) {
21 Human p = new SuperHero ( ) ;
22 p = new Human ( ) ;
23 p = new S u p e rVi l l a i n ( ) ;
24
25 SuperHero z = new SuperHero ( ) ;
26 }
27 }
 
J

Javaneuling223

Gast
Java:
class Human{
public static int typeCode = 0 ;
public int getTypeCode( ) {return typeCode; }
public String category( ) {return "Normalo"; }
public static String specialPower( ) {return "Keine"; }
}
class SuperVillain extends Human{
public static int typeCode = 2 ;
public String category( ) {return "Boesewicht"; }
public static String specialPower( ) {return "FreezeRay"; }
}
class SuperHero extends Human{
public static int typeCode = 1;
public int getTypeCode( ) {return typeCode; }
public String category( ) {return "Held"; }
public static String specialPower( ) {return "Hammer" ; }
}

public class Typed {
public static void main (String[ ] args) {
Human p = new SuperHero ( ) ;
p = new Human ( ) ;
p = new SuperVillain( ) ;
SuperHero z = new SuperHero ( ) ;
}
}


sry nochmal, hoffe jetzt is richtig
 
J

Javaneuling223

Gast
1. also in zeile 21 nehm ich an das p statisch und Human dynamisch ist, da human das objekt ist welches in p reinspeichert. aber mir ist nicht klar wieso sich das dann ändern sollte. p bleibt für mch statisch und nur SuperVillain ist meiner ansicht nach nicht von beiden. hoffe ich nehme das alles nicht total falsch an :(
2. z = statisch, Superhero = dynamisch
3. dort hab ich gar kein plan wie ich das machen soll
 

skuzzle

Mitglied
1. also in zeile 21 nehm ich an das p statisch und Human dynamisch ist, da human das objekt ist welches in p reinspeichert. aber mir ist nicht klar wieso sich das dann ändern sollte. p bleibt für mch statisch und nur SuperVillain ist meiner ansicht nach nicht von beiden. hoffe ich nehme das alles nicht total falsch an :(
2. z = statisch, Superhero = dynamisch
3. dort hab ich gar kein plan wie ich das machen soll

2. Superhero statisch und SuperHero dynamisch soweit ich weiss

mir wurde das so erklärt das alles links vom gleichheitszeichen statisch und alles rechts davon dynamisch is
 

Neue Themen


Oben