[Java] Polymorphismus

Was ist Polymorphismus?

Shape.java


public class Shape {
  protected double width;
  protected double height;

  public Shape(double width, double height) {
    this.width = width;
    this.height = height;
  }
//Holen Sie sich den Bereich der Figur (überschreiben in der abgeleiteten Klasse)
  public  double getArea() {
    return 0d;
  }
  //public abstract double getArea();
}

Triangle.java


public class Triangle extends Shape {
  public Triangle(double width, double height) {
    super(width, height);
  }
//Holen Sie sich die Fläche eines Dreiecks
  @Override
  public double getArea() {
    return this.width * this.height / 2;
  }
}

Rectangle.java


public class Rectangle extends Shape {
  public Rectangle(double width, double height) {
    super(width, height);
  }
//Holen Sie sich die Fläche des Platzes
  @Override
  public double getArea() {
    return this.width * this.height;
  }
}
public class PolymorphismBasic {
  public static void main(String[] args) {
    //Weisen Sie ein Objekt vom Typ Dreieck zu (Upcast)
    Shape tri = new Triangle(10, 50);
    //Weisen Sie ein Objekt vom Typ Rechteck zu (Upcast)
    Shape rec = new Rectangle(10, 50);
    System.out.println(tri.getArea()); //250.0
    System.out.println(rec.getArea()); //500.0
  }
}

Abstrakte Methode

Wie benutzt man

Shape.java


public abstract class Shape {
  protected double width;
  protected double height;

  public Shape(double width, double height) {
    this.width = width;
    this.height = height;
  }
 //Sie können keinen Inhalt in der Basisklasse haben!
  public abstract double getArea();
}

Schnittstelle

Schnittstellendefinition

public interface Shape {
  double getArea();
}

Definierbares Mitglied

Schnittstellenimplementierung

//Implementierungsklasse für Formschnittstellen
public class Rectangle implements Shape {
  private double width;
  private double height;

  public Rectangle(double width, double height) {
    this.width = width;
    this.height = height;
  }

  @Override
  public double getArea() {
    return this.width * this.height;
  }
}

Schnittstellenmitglied

Konstantes Feld

interface MyApp{
    String TITLE = "Ritter der unsterblichen Vögel";
    double NUMBER = 5;
}

statisches Feld (Java 8 oder höher)

Standardmethode (Java 8 oder höher)

public interface Loggable {
  default void log(String msg) {
    System.out.println("Log: " + msg);
  }
}
//Überschreiben Sie die Protokollmethode nicht
public class LoggableImpl implements Loggable {
}

InterfaceDefault.java


public class InterfaceDefault {

  public static void main(String[] args) {
    var l = new LoggableImpl();
    //Die Standardmethode der Loggable-Klasse wird aufgerufen
    l.log("Harry Potter"); //Log:Harry Potter
  }
}
//Überschreiben Sie die Protokollmethode nicht
public class LoggableImpl implements Loggable {
  @Override
  public void log(String msg) {
    Loggable.super.log(msg);
    System.out.println("LogImpl: " + msg);
  }
}

InterfaceDefault.java


public class InterfaceDefault {

  public static void main(String[] args) {
    var l = new LoggableImpl();
    l.log("Harry Potter"); 
    //Log:Harry Potter
    //LogImpl:Harry Potter
  }
}

Hinweis zur Mehrfachvererbung

Doppelte abstrakte Methode

public interface IHoge {
  void foo();
  //CharSequence foo(); //OK
}
public interface IHoge2 {
  String foo();
}
public class HogeImpl implements IHoge, IHoge2 {
    @Override
    public String foo() { } //Der Rückgabetyp ist IHoge.foo()Nicht kompatibel mit
}

Konstante Felder duplizieren

public interface Hoge {
  int DATA = 0;
}
public interface Hoge2 {
  String DATA = "This is an apple.";
}
public class HogeImpl implements Hoge, Hoge2 {
  public void foo() {
    System.out.println(DATA); //Error
  }
}

Standardmethode duplizieren

public interface DHoge {
  default void log(String msg) {
    System.out.println("DHoge: " + msg);
  }
}
public interface DHoge2 {
  default void log(String msg) {
    System.out.println("DHoge2: " + msg);
  }
}
public class HogeImpl implements DHoge, DHoge2 {
  @Override
  public void log(String msg) {
    DHoge.super.log(msg); //Fehler (doppeltes Standardmethodenprotokoll mit String ist DHoge2,(Von DHoge geerbt)
    //IHoge.super.log(msg); //Explizite Referenz ist OK
  }
}

Welche zu verwenden, Schnittstelle oder abstrakte Klasse?

Recommended Posts

[Java] Polymorphismus
Java (Polymorphismus)
Polymorphismus
Über Java-Polymorphismus super ()
Java (Verdienste des Polymorphismus)
Java
Java
Polymorphismus
Java lernen (0)
Java studieren ―― 3
[Java] -Array
Polymorphismus-Quiz
Java geschützt
[Java] Anmerkung
[Java] Modul
Java-Array
Java studieren ―― 9
Java Scratch Scratch
Java-Tipps, Tipps
Java-Methoden
Java-Methode
Java (Konstruktor)
Java-Array
Java (überschreiben)
Java (Methode)
Java Day 2018
Java-Zeichenfolge
Java statisch
Java-Serialisierung
Java Anfänger 4
JAVA hat bezahlt
Java studieren ―― 4
Java (gesetzt)
[Java] compareTo
Java studieren -5
Java reflektierend 获 获 举
Java (Schnittstelle)
Java-Memorandum
Java-Array
[Java] Array
Java # 0 studieren
Java-Überprüfung
Java-Framework
Java-Funktionen
[Java] Vererbung
FastScanner Java
Über Polymorphismus
Java-Funktionen
Java Anfänger 3
Java-Memo
Java (Kapselung)
Java-Vererbung
[Java] Überladung
Java-Grundlagen
Java dekompilieren
[Java] Anmerkung
Java Note
Java Anfänger
Java (add2)
JAVA (Karte)