Klassen:Javakurs: Unterschied zwischen den Versionen

Aus Info-Theke
Zur Navigation springen Zur Suche springen
Zeile 32: Zeile 32:
       this.seed = System.currentTimeMillis();
       this.seed = System.currentTimeMillis();
   }
   }
   Random(long seed){
   public Random(long seed){
       this.seed = seed;
       this.seed = seed;
   }
   }
   long next() {
   public long next() {
       this.seed = Math.abs(this.seed * 0x20041991 + 0x11111989);
       this.seed = Math.abs(this.seed * 0x20041991 + 0x11111989);
       return this.seed;
       return this.seed;
   }
   }
   long next(long min, long max){
   public long next(long min, long max){
       long rc = next();
       long rc = next();
       rc = min == max ? min : min + rc % (max - min);
       rc = min == max ? min : min + rc % (max - min);
Zeile 52: Zeile 52:
}
}
class RealRandom extends Random {
class RealRandom extends Random {
   double next(double min, double max){
   public double next(double min, double max){
       double rc = next(0, 1000000000) / 1000000000.0;
       double rc = next(0, 1000000000) / 1000000000.0;
       rc = min == max ? min : min + rc * (max - min);
       rc = min == max ? min : min + rc * (max - min);

Version vom 24. September 2014, 01:34 Uhr


Übersicht

Theorie

  • Eine Klasse beschreibt ein Objekt.
  • Eine Klasse besteht aus Eigenschaften (Variablen) und Aktionen (Methoden)
  • Es gibt spezielle Methoden: Konstruktoren genannt.
    • Ein Konstruktor hat den Namen der Klasse
    • Ein Konstruktor hat keinen Ergebnistyp
    • Wird ein neues Objekt der Klasse erzeugt, wird ein Konstruktor aufgerufen. Daher muss ein Konstruktor alle Initialisierunngen der Klasse enthalten
    • Welcher Konstruktor genommen wird, wird aufgrund der Argumente bestimmt.
    • Ist kein Konstruktor ohne Argumente vorhanden, wird ein impliziter definiert

Syntax

Klassendefinition

'class' <name> [ 'extends' <base-class> ] '{' { <method-or-variable> }* '}'

<method-or-variable> ::= <method> | <variable>

Erzeugung eines Objekts

<class-name> <name> '=' 'new' <class-name> '(' <argument-list> ')' ';'

Beispiel

class Random {
   private long seed = 0;
   Random() {
      this.seed = System.currentTimeMillis();
   }
   public Random(long seed){
      this.seed = seed;
   }
   public long next() {
      this.seed = Math.abs(this.seed * 0x20041991 + 0x11111989);
      return this.seed;
   }
   public long next(long min, long max){
      long rc = next();
      rc = min == max ? min : min + rc % (max - min);
      return rc;
   }
   public static void main(String[] args) {
      Random random = new Random();
      for (int ix = 0; ix < 10; ix++)
         System.out.print(String.format("%d ", random.next(-100, 100));
      System.out.println();
   }
}
class RealRandom extends Random {
   public double next(double min, double max){
      double rc = next(0, 1000000000) / 1000000000.0;
      rc = min == max ? min : min + rc * (max - min);
      return rc;
   }
   public static void main(String[] args) {
      Random random = new RealRandom();
      for (int ix = 0; ix < 10; ix++)
         System.out.print(String.format("%.3f ", random.next(-1.0, 1.0));
      System.out.println();
   }
}