[英]ClickableSpan - Class is not abstract and does not override abstract method
[英]Class is not abstract and does not override abstract method
因此,我一直在為我的編程類從事抽象作業,但陷入一個問題。 我現在的目標是能夠使用抽象,然后能夠使用矩形和橢圓形繪制一個簡單的城市,例如矩形建築物或燈柱上的橢圓形燈。
我在編譯時收到的錯誤是:MyTestApp.Rectangle不是抽象的,並且不會覆蓋MyTestApp.Shape中的抽象方法drawEllipse(java.awt.Graphics)。 該錯誤顯示在Shape類正下方的“ Rectangle extends Shape {類”行上。
我的問題是我的抽象錯了嗎? 我一直在弄亂Rectangle和Ellipse類中的構造函數和draw()方法,有一段時間了,但仍然找不到找到解決方案的運氣。
代碼如下:
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);
}
}
}
感謝您的閱讀和幫助!
Rectangle和Ellipse這兩個類都需要覆蓋這兩個抽象方法。
要解決此問題,您有3個選擇:
有一個方法可以執行將擴展Shape的類的功能,並在Rectangle和Ellipse中覆蓋該方法,例如:
abstract class Shape { // ... void draw(Graphics g); }
和
class Rectangle extends Shape {
void draw(Graphics g) {
// ...
}
}
最后
class Ellipse extends Shape {
void draw(Graphics g) {
// ...
}
}
您可以在它們之間切換,如下所示:
Shape shape = new Ellipse();
shape.draw(/* ... */);
shape = new Rectangle();
shape.draw(/* ... */);
再次,只是一個例子。
如果您試圖利用多態行為,則需要確保外部類(需要多態)可見的方法具有相同的簽名。 這意味着它們需要具有相同的名稱,數量和參數順序以及參數類型。
在您的情況下,最好有一個通用的draw()
方法,並依靠子類( Rectangle
和Ellipse
)來實現draw()
方法,就像您一直想的那樣是“ drawEllipse”和“ drawRectangle”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.