Announcement

Collapse
No announcement yet.

Triviale Anfängerfrage

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Triviale Anfängerfrage

    Hi<br>
    ich will folgendes Problem lösen:<br>
    Ein Klasse die eine Methode hat, welche 3 mal überschrieben wird, dann eine subKlasse, welche diese Methode nocheinmal überschreibt und dann beweist, dass alle 4 Versionen der Methode verfügbar sind.<br>
    hier mein Code<br>
    class Flower{
    Flower(int i){<br>
    System.out.println("Flower constr");<br>

    }<br>
    Flower(String s){<br>
    System.out.println("String args");<br>
    }<br>
    Flower(String s, int i){<br>
    this(99);<br>
    this.s = s;<br>
    System.out.println("Sring and int args");<br>
    }<br>
    }<br>
    <br>
    public class Blume extends Flower{<br>
    Blume(int i){<br>
    Flower f = new FLower("Hi");<br>
    Flower f1 = new Flower(i + 9);<br>
    Flower f3 = new Flower("Hello", 47);<br>
    <br>
    }<br>
    public static void main(String[] args){<br>
    Blume b = new Blume(7);<br>
    }<br>
    <br>
    }<br>
    Danke für eure Hilfe<br>
    Othmar

  • #2
    <P><FONT SIZE=1 STYLE="font-size: 8pt">OK, das sieht doch schon ganz
    gut aus:</FONT></P>
    <P><FONT SIZE=1 STYLE="font-size: 8pt">Ich werde dir mal den Code zu
    Ende schreiben und noch ein paar Anmerkungen dazu machen. (In 2
    Teilen)</FONT></P>
    <P><FONT FACE="Courier, monospace"><FONT SIZE=1 STYLE="font-size: 8pt"><FONT COLOR="#0000ff">class</FONT>
    <B>Auto</B>{<BR><BR> <FONT COLOR="#0000ff">int</FONT> sitze = <FONT COLOR="#ff0000">4</FONT>;<BR>
    String name = <FONT COLOR="#008080">&quot;unbekannt&quot;</FONT>;<BR><BR>
    <FONT COLOR="#0000ff">public</FONT> <B>Auto(</B><FONT COLOR="#0000ff">int</FONT>
    i<B>)</B>{ <FONT COLOR="#008000">// 1.Konstruktor (nur Angabe der
    Sitze)</FONT><BR> sitze = i; <FONT COLOR="#008000">// Zuweisung
    der initialen Anzahl der Sitze</FONT><BR> System.out.<B>println(</B><FONT COLOR="#008080">&quot;1.
    Konstruktor von Auto(&quot;</FONT>+sitze+<FONT COLOR="#008080">&quot&quot;</FONT><B>)</B>;
    <BR> }<BR><BR> <FONT COLOR="#0000ff">public</FONT> <B>Auto(</B>String
    s<B>)</B>{ <FONT COLOR="#008000">// 2.Konstruktor (nur Angabe des
    Namens)</FONT><BR> name = s; <FONT COLOR="#008000">// Zuweisung
    des initialen Namens des Autos</FONT><BR>
    System.out.<B>println(</B><FONT COLOR="#008080">&quot;2.Konstruktor
    von Auto(&quot;</FONT>+name+<FONT COLOR="#008080">&quot&quot;</FONT><B>)</B>;<BR>
    }<BR><BR> <FONT COLOR="#0000ff">public</FONT> <B>Auto(</B>String s,
    <FONT COLOR="#0000ff">int</FONT> i<B>)</B>{ <FONT COLOR="#008000">//
    3.Konstruktor (Angabe von Namen und Sitzen)</FONT><BR> name = s;
    <FONT COLOR="#008000">// Der Aufruf des 1. oder 2. Konstruktors kann
    sich gespart werden.</FONT><BR> sitze = i; <FONT COLOR="#008000">//
    Beim Aufruf mit this(i) w&uuml;rde erst 1. und dann 3. abgearbeitet
    werden.</FONT><BR> System.out.<B>println(</B><FONT COLOR="#008080">&quot;3.Konstruktor
    von Auto(&quot;</FONT>+name+<FONT COLOR="#008080">&quot;,&quot;</FONT>+sitze+<FONT COLOR="#008080">&quot&quot;</FONT><B>)</B>;<BR>
    }<BR><BR> <FONT COLOR="#0000ff">public</FONT> <FONT COLOR="#0000ff">void</FONT>
    <B>info()</B>{ <FONT COLOR="#008000">// Schreibt eine Information zu
    dieser Klasse (zum instanzierten Objekt).</FONT><BR>
    System.out.<B>println(</B><FONT COLOR="#008080">&quot;Ich bin ein
    Auto, mein Name ist &quot;</FONT>+name+<FONT COLOR="#008080">&quot;
    und ich habe &quot;</FONT>+sitze+<FONT COLOR="#008080">&quot;
    Sitze.&quot;</FONT><B>)</B>;<BR> }<BR>}<BR></FONT></FONT><BR><BR>
    </P>
    &#10

    Comment


    • #3
      <P><FONT FACE="Courier, monospace"><FONT SIZE=1 STYLE="font-size: 8pt"><FONT COLOR="#0000ff">public</FONT>
      <FONT COLOR="#0000ff">class</FONT> <B>Audi</B> <FONT COLOR="#0000ff">extends</FONT>
      Auto{<BR><BR> String typ = <FONT COLOR="#008080">&quot;unbekannt&quot;</FONT>;<BR><BR>
      <FONT COLOR="#0000ff">public</FONT> <B>Audi(</B><FONT COLOR="#0000ff">int</FONT>
      i<B>)</B>{ <FONT COLOR="#008000">// normal w&uuml;rde hier auch
      super(i); reichen, was auch besser ist.</FONT><BR> sitze = i; <FONT COLOR="#008000">//
      Man k&ouml;nnte auch schreiben super.sitze da Audi ja die Variable
      sitze von Auto geerbt hat.<BR> n</FONT><FONT COLOR="#000000">ame =
      <FONT COLOR="#008080">&quot;Audi&quot;</FONT>;</FONT><BR> }<BR><BR>
      <FONT COLOR="#0000ff">public void</FONT> <B>info()</B>{ <FONT COLOR="#008000">//
      &Uuml;berschreibt Auto.info() und nutzt hier auch den Code von Auto.</FONT><BR>
      System.out.<B>println(</B><FONT COLOR="#008080">&quot;Ich bin ein
      Audi, mein Typ ist &quot;</FONT>+typ+<FONT COLOR="#008080">&quot; und
      ich habe &quot;</FONT>+sitze+<FONT COLOR="#008080">&quot; Sitze.&quot;</FONT><B>)</B>;<BR>
      System.out.<B>println(</B><FONT COLOR="#008080">&quot;Und noch
      was:<FONT COLOR="#008080">&quot;</FONT><B>)</B>;<BR></FONT><FONT COLOR="#000000">
      super.<B>info()</B>;</FONT><BR> }<BR><BR> <FONT COLOR="#0000ff">public
      static void</FONT> <B>main(</B>String [] args<B>)</B>{ <FONT COLOR="#008000">//
      Diese Methode k&ouml;nnte auch in einer andern Klasse definiert
      sein.<BR></FONT><FONT COLOR="#000000"> Auto auto1 = new <B>Auto(</B><FONT COLOR="#ff0000">4</FONT><B>)</B>;<BR>
      Auto auto2 = new <B>Auto(</B><FONT COLOR="#008080">&quot;Ford&quot;</FONT><FONT COLOR="#000000">,<FONT COLOR="#ff0000">4</FONT><B>)</B>;<BR><BR></FONT><FONT COLOR="#008080">
      </FONT><FONT COLOR="#000000"> System.out.<B>println(</B>&quot;Auto1:&quot;<B>)</B>;<BR>
      auto1.<B>info()</B>;<BR> System.out.<B>println(</B>&quot;Auto2:&quot;<B>)</B>;<BR>
      auto2.<B>info()</B>;<BR><BR> Audi audi = new <B>Audi(</B><FONT COLOR="#ff0000">4</FONT><B>)</B>;<BR>
      System.out.<B>println(</B>&quot;Auto2:&quot;<B>)</B>;<BR>
      audi.<B>info()</B>;</FONT><BR> }</FONT></FONT></FONT></P>
      <P><FONT COLOR="#000000"><FONT FACE="Courier, monospace"><FONT SIZE=1 STYLE="font-size: 8pt">Mit
      freundlichen Gr&uuml;&szlig;en Tobias Oelgarte</FONT></FONT></FONT></P&gt

      Comment


      • #4
        Hi!<br>
        Danke Tobias!!<br>
        Jetzt blicke ich in diesem Thema endlich durch!<br>
        Othma

        Comment

        Working...
        X