Vererbung und Konstruktoren

brotkruemel1

Mitglied
Hallo zusammen,

ich bin blutiger Anfänger in Sachen Java und verzweifle gerade über ein Problem.
Es geht darum dass ich eine Klasse Matrix geschrieben habe, die die Matrixoperationen beinhaltet und außerdem noch eine Klasse Complex, die Rechenoperationen mit komplexen Zahlen abwickelt.

Nun möchte ich eine neue Klasse schreiben, die Matrixoperationen aber für komplexe Matrizen beinhaltet. dazu benötige ich aber natürlich die Rechenoperationen mit komplexen Zahlen die ich bereits in Complex implementiert habe.

Ich versuche nun irgendwie diese Rechenoperationen der Klasse Complex an meine Klasse MatrixC zu vererben. Nun habe ich in meiner Klasse MatrixC aber mehrere Konstruktoren, die die Matrixelemente von der Tastatur einlesen. Mit diesen Konstruktoren hat der Compiler aber irgendein Problem und ich hab keine Ahnung warum.
Fehlermeldung: cannot find symbol
symbol: construktor Complex(), location Class Complex
public MatrixC() {
^
Java:
public class MatrixC extends Complex{

	private Complex[][] elem;

public MatrixC() {						//Einlesen der Spalten- und Zeilenanzahl von der Tastatur		

		int n = readInteger("Bitte Zeilenanzahl eingeben: ")-1;
		int m = readInteger("Bitte Spaltenanzahl eingeben: ")-1;

		elem = new Complex[n][m]; 

		for( int i = 0; i < n; i++) {
			for(int j = 0; j < m; j++) {
				elem[i][j] = readComplex();
			}
		}
	}

Was ist denn überhaupt das Problem?
Ich habe leider die sache mit der Vererbung noch nicht ganz verstanden und keine Idee wie ich das Problem lösen soll! Gibt es noch einen andere Möglichkeit außer der Vererbung die Complex-Klasse in der Matrix-Klasse zu nutzen?

Vielen Dank!
Kathrin
 
Zuletzt bearbeitet von einem Moderator:
Java:
import static Prog1Tools.IOTools.readInteger;

public class Complex {

	private double re;			//Instanzvariable
	private double im;

	public final static Complex I = new Complex(0., 1.0);			//Konstanten der Klasse Complex
	public final static Complex ZERO = new Complex(0.0);
	public final static Complex HALF =  new Complex(0.5, 0.);
	public final static Complex ONE = new Complex(1.0);

	public Complex(double r, double i) {			//Konstruktore
		this.re = r;
		this.im = i;
	}

	public Complex(double r) {				//Konstruktor
		this.re = r;
		this.im = 0;
	}

	public double real() {
		return this.re;
	}
	
	public double imag() {
		return this.im;
	}

	public double abs() {
		return Math.sqrt(this.re*this.re + this.im*this.im);
	}

	public double arg() {
		return Math.acos(this.re / this.abs() ); 
	}

	public Complex conj() {
		return new Complex(this.re,-this.im);
	}

	public Complex add(Complex z1) {
		return new Complex(this.re + z1.re, this.im + z1.im);
	}

	public Complex sub(Complex z1) {
		return new Complex(this.re - z1.re, this.im - z1.im);
	}

	public Complex mul(Complex z1) {
		return new Complex(this.re*z1.re - this.im*z1.im, this.re*z1.im + this.im*z1.re);
	}

	public Complex div(Complex z1) {
		return new Complex((this.re*z1.re + this.im*z1.re)/(z1.re*z1.re + this.im*this.im), (this.im*z1.re - this.re*z1.im)/(z1.re*z1.re + z1.im*z1.im));
	}

	public boolean equals(Complex z1) {
		if( this.re == z1.re && this.im == z1.im) {
			return true;
		}
		else {
			return false;
		}
	}

	public String toString() {
		return "(" + this.re + "," + this.im + ")";
	}

	public Complex readComplex() {
		int r = readInteger("Bitte Realteil eingeben!");
		int i = readInteger("Bitte Imaginärteil eingeben!");
	
		Complex c = new Complex(r, i);
		return c;
	}
		


	public static void test(Complex z1, Complex z2) {
    System.out.println("\nComplex Test Start");
    System.out.println("z1: " + z1 + " ... z2: " + z2);
    System.out.println("z1 == z2: " + z1.equals(z2) + " ... z2 == z1: " 
                                    + z2.equals(z1));
    System.out.println("z1.conj(): " + z1.conj() + " ... z2.conj(): " 
                                     + z2.conj());

    if ( ! z1.equals(z1.conj().conj()) ) {
      System.out.println("FEHLER in conj");
    }
    if ( ! z2.conj().conj().equals(z2) ) {
      System.out.println("FEHLER in conj");
    }

    System.out.println("z1.real(): " + z1.real() + " ... z1.imag(): " 
                                     + z1.imag());
    System.out.println("z2.real(): " + z2.real() + " ... z2.imag(): " 
                                     + z2.imag());
    System.out.println("z1.abs(): " + z1.abs() + " ... z1.arg(): " 
                                    + z1.arg());
    System.out.println("z2.abs(): " + z2.abs() + " ... z2.arg(): " 
                                    + z2.arg());

    System.out.println("Add: " + z1.add(z2) + " ... " + z2.add(z1));
    System.out.println("Sub: " + z1.sub(z2) + " ... " + z2.sub(z1));
    System.out.println("Mul: " + z1.mul(z2) + " ... " + z2.mul(z1));
    System.out.println("Div: " + z1.div(z2) + " ... " + z2.div(z1));

    System.out.println("ARI: " + z1.add(z2).sub(z1).sub(z2) );
    System.out.println("ARI: " + z1.add(z2.sub(z1)).sub(z2) );
    System.out.println("ARI: " + z1.mul(z2).div(z1).div(z2) );
    System.out.println("ARI: " + z1.mul(z2.div(z1)).div(z2) );

    System.out.println("Complex Test Ende");
  }

 					 // Test der Grundfunktionalitaet
    public static void main(String[] args) {
    test(new Complex(2.0, -3.0), new Complex(5.0, -7.0)); 
    test(new Complex(1.0), new Complex(0.0, 1.0));
    test(new Complex(Math.PI, Math.E), new Complex(Math.PI, Math.E));
    test(new Complex(3.0, 4.0), new Complex(4.0, 3.0));

    if ( ! new Complex(13.0).equals(new Complex(13.0, 0.0)) ) {
      System.out.println("FEHLER in Konstruktoren");
    }

    					// Test, ob REFERENZ I wirklich nicht aenderbar?
    					// I = I.add(new Complex(1.0, -1.0));   // Compilezeitfehler
    System.out.println("\nImaginaere Einheit: " + I);
   					// Test, ob WERT von I aenderbar?
    I.re= 5.0;
    System.out.println("Imaginaere Einheit? " + I + "\n");
  }
	
}
 
Nur mal vollständigkeitshalber Zusammenfassung aus dem IRC Chanel:
- Matrix erbt nicht von den Complex, da eine Matrix keine komplexe Zahl ist.
- die Struktur wurde aufgeteilt auf Complex und ComplexUtility. Das 1. ist nur ein Speicherobjekt, das 2. stellt die Operationen zur Verfügung. Bei der Matrix-Klasse das gleiche.
 
Hallo,

könntest du die Rechenmethoden aus Complex in eine statische Klasse auslagern, sodass du gar nicht vererben brauchst?

Gruß n.

edit: Hach, bin ich überflüssig und langsam noch dazu ;(
 
Zuletzt bearbeitet von einem Moderator:
Um eine Unterklasse zu erzeugen muss auch die Oberklasse erzeugt werden. Dies geht nur über den Konstruktor. Da die Klasse Complex über keinen Default-Konstruktor verfügt (Konstruktor ohne Parameter) weiß der Compiler nicht welcher der beiden vorhandenen Konstruktoren aufgerufen werden soll. Die Oberklasse kann folglich nicht erzeugt werden.

Du musst dies also explizit tun:
Java:
super(0.);
Dies muss die erste Anweisung in deinem Konstruktor MatrixC sein (mit super kannst du auf die Oberklasse zugreifen).
 
soo hier bin ich mal wieder.

Also ich habe jetzt die Methoden von beiden Klassen Complex und MatrixC ausgelagert und extra abgespeichert. Jetzt bekomme ich aber hunderte Fehlermeldungen, immer mit der Begründung
"Cannnot find symbol" also zum Beispiel
symbol: class MatrixC
location: class complex.MatrixCalcUtility.

Muss ich denn beim Comilieren noch was beachten?
Ich hab jetzt einfach alle 4 Klassen ins package complex gepackt. Aber irgendwie findet jeder der einelnen Klassen die Konstruktoren, Methoden etc. aus den anderen Klassen nicht?

Muss ich noch irgendwas importieren oder so?

Kathrin
 
Importieren musst du nichts wenn die Klassen alle im gleichen Package sind, du musst aber das Package angeben:
Java:
package complex;
An was das sonst noch haken könnte können wir dir nur sagen wenn du uns auch deinen Code zeigst.
 
Soo ich habs jetzt noch mal mit der vererbung probiert nachdem ich den anderen weg nicht hinbekommen habe. ich habe jetzt also wie Antoras geschrieben hat nur das super in meinen matrix-konstruktoren eingefügt und.......... es funktioniert! Juchuuuu.:toll:

Aber vielleicht könnt ihr mir trotzdem den anderen weg noch mal erläutern. Wäre ja auch interessant zu wissen!

Auf jeden Fall: DANKE!!! :applaus:
 
Java:
package complex;
public class Complex {

	private double re;			//Instanzvariable
	private double im;

	public final static Complex I = new Complex(0., 1.0);			//Konstanten der Klasse Complex
	public final static Complex ZERO = new Complex(0.0);
	public final static Complex HALF =  new Complex(0.5, 0.);
	public final static Complex ONE = new Complex(1.0);

	public Complex(double r, double i) {			//Konstruktore
		this.re = r;
		this.im = i;
	}

	public Complex(double r) {				//Konstruktor
		this.re = r;
		this.im = 0;
	}

	public double real() {
		return this.re;
	}
	
	public double imag() {
		return this.im;
	}

public String toString() {
		return "(" + this.re + "," + this.im + ")";
	}

	public Complex readComplex() {
		int r = readInteger("Bitte Realteil eingeben!");
		int i = readInteger("Bitte Imaginärteil eingeben!");
	
		Complex c = new Complex(r, i);
		return c;
	}
		

     public static void test(Complex z1, Complex z2) {
    System.out.println("\nComplex Test Start");
    System.out.println("z1: " + z1 + " ... z2: " + z2);
    System.out.println("z1 == z2: " + z1.equals(z2) + " ... z2 == z1: " 
                                    + z2.equals(z1));
    System.out.println("z1.conj(): " + z1.conj() + " ... z2.conj(): " 
                                     + z2.conj());

    if ( ! z1.equals(z1.conj().conj()) ) {
      System.out.println("FEHLER in conj");
    }
    if ( ! z2.conj().conj().equals(z2) ) {
      System.out.println("FEHLER in conj");
    }

    System.out.println("z1.real(): " + z1.real() + " ... z1.imag(): " 
                                     + z1.imag());
    System.out.println("z2.real(): " + z2.real() + " ... z2.imag(): " 
                                     + z2.imag());
    System.out.println("z1.abs(): " + z1.abs() + " ... z1.arg(): " 
                                    + z1.arg());
    System.out.println("z2.abs(): " + z2.abs() + " ... z2.arg(): " 
                                    + z2.arg());

    System.out.println("Add: " + z1.add(z2) + " ... " + z2.add(z1));
    System.out.println("Sub: " + z1.sub(z2) + " ... " + z2.sub(z1));
    System.out.println("Mul: " + z1.mul(z2) + " ... " + z2.mul(z1));
    System.out.println("Div: " + z1.div(z2) + " ... " + z2.div(z1));

    System.out.println("ARI: " + z1.add(z2).sub(z1).sub(z2) );
    System.out.println("ARI: " + z1.add(z2.sub(z1)).sub(z2) );
    System.out.println("ARI: " + z1.mul(z2).div(z1).div(z2) );
    System.out.println("ARI: " + z1.mul(z2.div(z1)).div(z2) );

    System.out.println("Complex Test Ende");
  }

 					 // Test der Grundfunktionalitaet
    public static void main(String[] args) {
    test(new Complex(2.0, -3.0), new Complex(5.0, -7.0)); 
    test(new Complex(1.0), new Complex(0.0, 1.0));
    test(new Complex(Math.PI, Math.E), new Complex(Math.PI, Math.E));
    test(new Complex(3.0, 4.0), new Complex(4.0, 3.0));

    if ( ! new Complex(13.0).equals(new Complex(13.0, 0.0)) ) {
      System.out.println("FEHLER in Konstruktoren");
    }

    					// Test, ob REFERENZ I wirklich nicht aenderbar?
    					// I = I.add(new Complex(1.0, -1.0));   // Compilezeitfehler
    System.out.println("\nImaginaere Einheit: " + I);
   					// Test, ob WERT von I aenderbar?
    I.re= 5.0;
    System.out.println("Imaginaere Einheit? " + I + "\n");
  }
	
}	
-------------------------------------------------
package complex;

public class ComplexCalcUtility {

	public static double abs() {
		return Math.sqrt(this.re*this.re + this.im*this.im);
	}

	public static double arg() {
		return Math.acos(this.re / this.abs() ); 
	}

	public static Complex conj() {
		return new Complex(this.re,-this.im);
	}

	public static Complex add(Complex z1) {
		return new Complex(this.re + z1.re, this.im + z1.im);
	}

	public static Complex sub(Complex z1) {
		return new Complex(this.re - z1.re, this.im - z1.im);
	}

	public static Complex mul(Complex z1) {
		return new Complex(this.re*z1.re - this.im*z1.im, this.re*z1.im + this.im*z1.re);
	}

	public static Complex div(Complex z1) {
		return new Complex((this.re*z1.re + this.im*z1.re)/(z1.re*z1.re + this.im*this.im), (this.im*z1.re - this.re*z1.im)/(z1.re*z1.re + z1.im*z1.im));
	}

	public static boolean equals(Complex z1) {
		if( this.re == z1.re && this.im == z1.im) {
			return true;
		}
		else {
			return false;
		}
	}
}
-----------------------------------------------------------------------
package complex;
public class MatrixC{

	private Complex[][] elem;
	
	public MatrixC() {						//Einlesen der Spalten- und Zeilenanzahl von der Tastatur		
	

		int n = readInteger("Bitte Zeilenanzahl eingeben: ")-1;
		int m = readInteger("Bitte Spaltenanzahl eingeben: ")-1;

		elem = new Complex[n][m]; 

		for( int i = 0; i < n; i++) {
			for(int j = 0; j < m; j++) {
				elem[i][j] = readComplex();
			}
		}
	}

	public MatrixC(boolean keyboardIn) {


		int n = readInteger("Bitte Zeilenanzahl eingeben: ");
		int m = readInteger("Bitte Spaltenanzahl eingeben: ");

		if(keyboardIn) {
			elem = new Complex[n][m]; 

			for( int i = 0; i < n; i++) {
				for(int j = 0; j < m; j++) {
					elem[i][j] = readComplex();
				}
			}
		}
	}

	public MatrixC(int row, int col) {

		elem = new Complex[row][col];
	}
	
	public MatrixC(int row, int col, boolean keyboardIn) {

		if(keyboardIn) {
			elem = new Complex[row][col]; 

			for( int i = 0; i < row; i++) {
				for(int j = 0; j < col; j++) {
					System.out.print("Bitte Element der " + i + ". Zeile und der " + j + ".Spalte eingeben!");
					elem[i][j] = readComplex();
				}
			}
		}
	}

	public MatrixC(MatrixC orig) {
		int n = orig.getRowCount();
		int m = orig.getColCount();

		elem = new Complex[n][m];

		for(int i = 0; i < n; i++) {
			for(int j = 0; j < m; j++) {
				elem[i][j] = orig.elem[i][j];
			}
		}
	}


	public boolean equals(MatrixC mat) {

		boolean equal = false;

		for(int i = 0; i < this.getRowCount(); i++) {
				for(int j = 0; j < this.getColCount(); j++) {
					
					if(this.elem[i][j].ComplexCalcUtility.equals(mat.elem[i][j])) {
						equal =true;
					}
					else {
						equal = false;
						break;
					}
				}
			}	
		return equal;
		}

	public boolean isSymm() {

		boolean sym = false;

		for(int i = 0; i < this.getRowCount(); i++) {
			for(int j = 0; j < this.getColCount(); j++) {
				
				if(this.elem[i][j] == this.elem[j][i]) {
					sym =true;
				}
				else {
					sym = false;
					break;
				}
			}
		}
	return sym;
	}
		
	public boolean isAntiSymm() {

		boolean antisym = true;
	
		if(this.isSymm()) {
			antisym = false;
		}
	
	return antisym;
	}

	public void print(String t) {

	System.out.println(t);

	String s = "";
    		for (int i = 0; i < this.getRowCount(); i++) {
  	    		for (int j = 0; j < this.getColCount(); j++) {
        				s += this.elem[i][j].toString() + ", ";
     			}
     		 s += "\n";
  		}
    	System.out.println(s);
  	}


	public int getRowCount() {
		return elem.length;
	}

	public int getColCount() {
		return elem[0].length;
	}



  public String toString() {
    StringBuffer ret = new StringBuffer();

    ret.append("{ ");

    for (int i=0; i < elem.length; i++) {
      ret.append("{");

      for (int j=0; j < elem[i].length-1; j++) {
        ret.append(elem[i][j]).append(", ");
      }

      ret.append(elem[i][elem[i].length-1]).append("}");
      if (i+1 < elem.length ) {
        ret.append(";\n");
      }
    }
    ret.append(" }");
    return ret.toString();
  }
 

  public static void test(MatrixC A, MatrixC B) {
    System.out.println("MatrixC Test Start"); 

    System.out.println("A ist eine " + A.getRowCount() + "x" + A.getColCount() + " Matrix");
    A.print("A");

    System.out.println("B ist eine " + B.getRowCount() + "x" + B.getColCount() + " Matrix");
    B.print("B");

    System.out.println("A==B?: " + A.equals(B) + " ... B==A?: " + B.equals(A));
    System.out.println("A.isSymm(): " + A.isSymm() + " ... A.isAntiSymm(): " + A.isAntiSymm());
    System.out.println("B.isSymm(): " + B.isSymm() + " ... B.isAntiSymm(): " + B.isAntiSymm());

    A.transpose().print("A.transpose()");
    B.transpose().print("B.transpose()");

    A.symm().print("A.symm()");
    B.symm().print("B.symm()");

    A.antiSymm().print("A.antiSymm()");
    B.antiSymm().print("B.antiSymm()");

    A.add(B).print("A+B");
    B.add(A).print("B+A");

    A.sub(B).print("A-B");
    B.sub(A).print("B-A");

    A.transpose().print("A.transpose()");
    B.transpose().print("B.transpose()");
    
    System.out.println("A==(A.symm() + A.antiSymm()): " + A.equals(A.symm().add(A.antiSymm())));
    System.out.println("B==(B.symm() + B.antiSymm()): " + B.equals(B.symm().add(B.antiSymm())));

    System.out.println("A+A==A.mul(2.0): " + A.mul(2.0).equals(A.add(A)));
    System.out.println("B+B==B.mul(2.0): " + B.mul(2.0).equals(B.add(B)));

    System.out.println("MatrixC Test Ende"); 
  }

  public static void main(String[] args) {
    MatrixC A = new MatrixC(2,2, true);
    MatrixC B = new MatrixC(2,2, true);

  	test(A, B);
  }
}
-----------------------------------------------------------
package complex;
	
public class MatrixCalcUtility {


	public static MatrixC add(MatrixC mat) {

		int n = this.getRowCount();
		int m = this.getColCount();
	
		MatrixC ad = new MatrixC(n, m);

		if(this.getRowCount() == mat.getRowCount() && this.getColCount() == mat.getColCount()) {

			for(int i = 0; i < n; i++) {
				for(int j = 0; j < m; j++) {
					ad.elem[i][j] = (elem[i][j]).ComplexCalcUtility.add(mat.elem[i][j]);
				}
			}
		}

		else {
			System.out.println("Dimension der Matrizen stimmen nicht überein! Addition nicht möglich!");
		}
	return ad;
	}

	public static MatrixC sub(MatrixC mat) {

		int n = this.getRowCount();
		int m = this.getColCount();
	
		MatrixC su = new MatrixC(n, m);

		if(this.getRowCount() == mat.getRowCount() && this.getColCount() == mat.getColCount()) {

			for(int i = 0; i < n; i++) {
				for(int j = 0; j < m; j++) {
					su.elem[i][j] = (elem[i][j]).ComplexCalcUtility.sub(mat.elem[i][j]);
				}
			}
		}

		else {
			System.out.println("Dimension der Matrizen stimmen nicht überein! Subtraktion nicht möglich!");
		}
	return su;
	}

	public static MatrixC mul(double s) {

		int n = this.getRowCount();
		int m = this.getColCount();
	
		MatrixC mu = new MatrixC(n, m);

		for(int i = 0; i < n; i++) {
			for(int j = 0; j < m; j++) {
				mu.elem[i][j] = (Complex.Complex(s)).ComplexCalcUtility.mul(this.elem[i][j]);
			}
		}
	return mu;
	}

	public static MatrixC transpose() {
	
		int n = this.getRowCount();
		int m = this.getColCount();

		MatrixC trans = new MatrixC(m, n);

		for(int i = 0; i < n; i++) {
			for(int j = 0; j < m; j++) {
				trans.elem[i][j] = this.elem[j][i];
			}
		}
	return trans;
	}

	public static MatrixC symm() {

		int n = this.getRowCount();
		int m = this.getColCount();

		MatrixC sym = new MatrixC(n, m);
		
		sym = ( this.add( this.transpose() ) ).mul(0.5);
		return sym;
		
	}	
			
	public static MatrixC antiSymm() {
	
		int n = this.getRowCount();
		int m = this.getColCount();

		MatrixC antisym = new MatrixC(n, m);
		
		antisym = ( this.sub( this.transpose() ) ).mul(0.5);
		return antisym;
		
	}

	public static boolean equals(MatrixC mat) {

		boolean equal = false;

		for(int i = 0; i < this.getRowCount(); i++) {
				for(int j = 0; j < this.getColCount(); j++) {
					
					if((this.elem[i][j]).ComplexCalcUtility.equal(mat.elem[i][j])) {
						equal =true;
					}
					else {
						equal = false;
						break;
					}
				}
			}	
		return equal;
		}

	public static boolean isSymm() {

		boolean sym = false;

		for(int i = 0; i < this.getRowCount(); i++) {
			for(int j = 0; j < this.getColCount(); j++) {
				
				if((this.elem[i][j]).ComplexCalcUtility.equal(this.elem[j][i])) {
					sym =true;
				}
				else {
					sym = false;
					break;
				}
			}
		}
	return sym;
	}
		
	public static boolean isAntiSymm() {

		boolean antisym = true;
	
		if(this.isSymm()) {
			antisym = false;
		}
	
	return antisym;
	}
}

So ich hab bestimmt noch nen paar Änderungen vergessen,aber es funktioniert sowieso nicht mal ansatzweise. Wie gesagt die Fehlermeldung hab ich ja schon gepostet. Der findet ja nicht mal Complex um das Array für die Matrix zu erstellen.
außerdem gibts irgendwie Probleme bei dem this und den static Methoden. Da meckert er auch rum!

Noch ne andere Frage: Was macht super(0.) überhaupt? D.h. warum funktionier das mit dem vererben auf einmal so?
 
Zuletzt bearbeitet:
Hast du den Code denn selber ausprobiert?
Mich wundern da ein paar Sachen, wie die super-Konstruktoren in der Matrix-Klasse (die von nichts erbt), this-verwendung bei statischen Methoden, der Aufruf der Utility-Methoden...
 
Ähn ne die super waren falsch...noch nen überbleibsel von der Vererbungslösung. Hab sie editiert.
Und das mit dem this ist ja das was ich meinte. das funktioniert nicht aber ich weiß nicht wie is das sonst lösen soll. weil meine Methoden nur ein Argument haben dürfen.
Was meinst du mit dem Aufruf der utility-Methoden?
 
MatrixC.java:4: cannot find symbol
symbol : class Complex
location: class complex.MatrixC
private Complex[][] elem;
^

das ist zum Beispiel die erste Fehlermeldung wenn ich MatrixC.java kompiliere.
 
Ich meine z.B.
[JAVA=220]this.elem[j].ComplexCalcUtility.equals(mat.elem[j])[/code]

Warum dürfen die nur ein Argument haben? Du willst 2 Zahlen miteinander addieren. Wieviele Zahlen brauchst du dafür?
 
Natürlich 2 aber in meiner Aufgabe steht ich soll eine Klassenmethode schreiben mit einem Argument und Ergebnis Complex.
Ich weiß is doof, aber das muss doch irgendiwe auch so funktionieren oder?
 
Wenn du Klassenmethoden schreiben musst, dann fällt die Utility Klasse weg, oder du machst es mit der Utility Klasse und verwendest diese in deinen Klassenmethoden (ein Parameter wäre this, der andere: 2. Zahl)
 
Java:
class Utility {
  public static Complex add(Complex z1, Complex z2) {
    // ...........
  }
}

Java:
class Complex {
  public void add(Complex z1) {
    // hier rufst du die Methode mit Utiliy.add(this, z1) auf, und änderst die Werte in der aktuellen Klasse
  }
}

So ungefähr.
 
Zuletzt bearbeitet:
Ahhhh ok. Jetzt hab ichs verstanden! Super werds gleich versuchen umzusetzen!

aber noch ne andere Frage: Was macht super(0.) denn? Also ich meine warum funktioniert dann die Sache mit der Vererbung und den Konstruktoren?
 
Ahh ok jetzt hab ich's gerafft. Also wenn ich ne Subklasse hab mit nem Konstruktor muss der auch immer den der Superklasse aufrufen?! So langsam versteh ich das mit der Vererbung. (hoffe ich)
 
Ein schönes Beispiel:
[Java]public class Test {

public static void main(String[] args) {
new SubTest(5);
}

public Test() {
System.out.println("ich bin parameterlos");
}

static class SubTest extends Test {

public SubTest(int i) {
System.out.println("ich wurde aufgerufen mit " + i);
}
}
}[/code]
gibt folgendes aus:
Code:
ich bin parameterlos
ich wurde aufgerufen mit 5
 
Sehr schön, super! Danke.

Ja ich glaube das ist das was du mir gestern schon versucht hattest schon zu erklären...Jetzt hab ich verstanden 😉
Was lange währt wird endlich gut,ne?! :toll:

Vielen Dank für die Geduld!
 

Zurück
Oben