Ein Datentyp für Körper

Matrixmultiplicationen sind nur auf dafür geeigneten Objekten möglich, bspw. auf den Elementen eines Körpers. Dazu definieren wir ein Interface EoField ("_element of field_"):

package fft;

/* Abstrakte Klasse Körper, die das Vorhandensein der Grundrechenarten fordert. */
interface EoField {
   
    /* Diese Methoden lassen sich leider nicht static definieren */
    public EoField add(EoField other);

    public EoField multiply(EoField other);

}

RealNumber

Eine einfache Implementation dieses Interfaces kann so aussehen:
package fft;

class RealNumber implements EoField {

    private double value;

    public RealNumber(double value) {
   this.value = value;
    }

    public EoField add(EoField other) {
   return new RealNumber(value + ((RealNumber) other).value);
    }

    public EoField multiply(EoField other) {
   return new RealNumber(value * ((RealNumber) other).value);
    }

    public String toString() {
   return String.valueOf(value);
    }
}

Anpassung von Complex

Damit Complex EoField implementiert, müssen folgende Anpassungen vorgenommen werden:

package fft;

class Complex implements EoField {

...

    public EoField add(EoField other) {
   return add(this, (Complex) other);
    }

    public EoField multiply(EoField other) {
   return multiply(this, (Complex) other);
    }

-- Christo - 12 Dec 2003
Topic revision: r1 - 2003-12-12, ChristopherHuhn
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding GSI Wiki? Send feedback | Legal notice | Privacy Policy (german)