2014-05-04 6 views
11

Also habe ich an einer Hausaufgabe auf Abstraktion für meine Programmierklasse gearbeitet und fiel in ein Problem. Das Ziel für mich ist es, Abstraktion nutzen zu können, um später mit Rechteck und Ellipse eine einfache Stadt, wie ein rechteckiges Gebäude oder ein ovales Licht auf einem Lichtpfosten, zeichnen zu können.Klasse ist nicht abstrakt und überschreibt keine abstrakte Methode

Der Fehler, den ich beim Kompilieren erhalte, ist: MyTestApp.Rectangle ist nicht abstrakt und überschreibt nicht die abstrakte Methode drawEllipse (java.awt.Graphics) in MyTestApp.Shape. Dieser Fehler wird in der Zeile "class Rectangle extends Shape {" direkt unter der Klasse Shape angezeigt.

Meine Frage ist, was mache ich falsch mit meiner Abstraktion? Ich habe die Konstruktoren und draw() -Methoden in den Klassen Rectangle und Ellipse schon eine Weile durcheinander gebracht und immer noch kein Glück, um eine Lösung zu finden.

-Code ist unten:

import java.awt.*; 
import javax.swing.*; 

public class MyTestApp extends JPanel { 
    Rectangle rect; 
    Ellipse oval; 
    public static void main(String [] args) { 
     MyTestApp myTestApp = new MyTestApp(); 
     myTestApp.test(); 
    } 

    public MyTestApp() { //creates the jframe 
     JFrame frame = new JFrame("MyClass Driver"); 
     setBackground(new Color(200, 250, 200)); 
     setPreferredSize(new Dimension(500, 400)); 
     frame.add(this); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public void delay(int msecs) { 
     try { 
      Thread.sleep(msecs); 
     } catch (InterruptedException e) { 
     } 
    } 

    public void paint(Graphics g) {//paints the rectangle and ellipse 
     super.paint(g); 
     if (rect != null) 
      rect.drawRectangle(g); 
     if (oval != null) 
      oval.drawEllipse(g); 
    } 

    public void test() {//gives the x/y position, width/height, and fill/outline color for the rectangle and oval 
     delay(1000); 
     rect = new Rectangle(20, 30, 23, 75, Color.GREEN, Color.BLUE); 
     oval = new Ellipse(10, 10, 10 , 34, Color.RED, Color.MAGENTA); 
     repaint(); 
    } 

    public abstract class Shape{//abstract class Shape that sets the x/y, width/height, and colors for the shapes 
     private int x, y, width, height; 
     private Color fillColor; 
     private Color outlineColor; 
     public Shape(int x, int y, int width, int height, Color fillColor, Color outlineColor) { 
      setXY(x, y); 
      setSize(width, height); 
      setFillColor(fillColor); 
      setOutlineColor(outlineColor); 
     } 

     public boolean setXY(int x, int y) { 
      this.x = x; 
      this.y = y; 
      return true; 
     } 

     public void setSize(int width, int height) { 
      if (width > 0) 
       this.width = width; 
      if (height > 0) 
       this.height = height; 
     } 

     public boolean setFillColor(Color fillColor){ 
      if (fillColor == null) return false; 
      this.fillColor = fillColor; 
      return true; 
     } 

     public boolean setOutlineColor(Color outlineColor){ 
      if (outlineColor == null) return false; 
      this.outlineColor = outlineColor; 
      return true; 
     } 

     public Color getFillColor() { 
      return fillColor; 
     } 

     public Color getOutlineColor() { 
      return outlineColor; 
     } 

     public abstract void drawRectangle(Graphics g);//do i need two? 
     public abstract void drawEllipse(Graphics g);//do i need both? 
    } 
    class Rectangle extends Shape{//!!!!!!!!!! where the error shows 
     public Rectangle(int x, int y, int width, int height, Color fillColor, Color outlineColor) { 
      super(x, y, width, height, fillColor, outlineColor); 
     } 

     public void drawRectangle(Graphics g){//draws the retangle 
      g.setColor(fillColor); 
      g.fillRect(x, y, width, height); 
      g.setColor(outlineColor); 
      g.drawRect(x, y, width, height); 
     } 
    } 
    class Ellipse extends Shape{ 
     public Ellipse(int x, int y, int width, int height, Color fillColor, Color outlineColor) { 
      super(x, y, width, height, fillColor, outlineColor); 
     } 

     public void drawEllipse(Graphics g){//draws the ellipse 
      g.setColor(fillColor); 
      g.fillOval(x, y, width, height); 
      g.setColor(outlineColor); 
       g.drawOval(x, y, width, height); 
      } 
     } 
} 

Danke fürs Lesen und helfen!

Antwort

2

Beide Klassen Rectangle und Ellipse müssen beide abstrakten Methoden überschreiben.

Um dies zu umgehen, haben Sie 3 Möglichkeiten:

  • die beiden Methoden hinzufügen
  • jede Klasse machen, die haben eine einzelne Methode Form abstrakte
  • erweitert, die die Funktion der Klassen das tut verlängert Form und überschreibt diese Methode in Rechteck und Ellipse, zum Beispiel:

    abstract class Shape { 
        // ... 
        void draw(Graphics g); 
    } 
    

Und

class Rectangle extends Shape { 
     void draw(Graphics g) { 
      // ... 
     } 
    } 

Schließlich

class Ellipse extends Shape { 
     void draw(Graphics g) { 
      // ... 
     } 
    } 

Und Sie können zwischen ihnen wechseln in, etwa so:

Shape shape = new Ellipse(); 
    shape.draw(/* ... */); 

    shape = new Rectangle(); 
    shape.draw(/* ... */); 

Wieder nur ein Beispiel.

2

Wenn Sie versuchen, polymorphes Verhalten zu nutzen, müssen Sie sicherstellen, dass die für externe Klassen sichtbaren Methoden (die Polymorphie benötigen) die gleiche Signatur haben. Das bedeutet, dass sie den gleichen Namen, die gleiche Anzahl und Reihenfolge der Parameter sowie die Parametertypen haben müssen.

In Ihrem Fall könnten Sie besser tun, um eine generische draw() Methode zu haben, und verlassen sich auf die Unterklassen (Rectangle, Ellipse) die draw() Verfahren zu implementieren, was Sie denken an als „drawEllipse“ und „drawRectangle“ hatte.