2017-01-27 158 views
0

當編譯下面的代碼時,得到下面的錯誤:CircleObject不是抽象並且在圖不重寫抽象方法

Abstrct.java:7: error: CircleObject is not abstract and does not override abstract method changePosition(int,int) in Figure 
class CircleObject extends Figure 
^ 
Abstrct.java:14: error: Kd is not abstract and does not override abstract method draw() in Figure 
class Kd extends Figure 
^ 
2 errors 

使用

abstract class Figure { 

    int x, y; 

    abstract void changePosition(int newX, int newY); 

    abstract void draw(); 
} 

class CircleObject extends Figure { 

    void draw() { 
     System.out.println("Draw method called"); 
    } 
} 

class Kd extends Figure { 

    void changePosition(int newX, int newY) { 
     System.out.println("Change Position method called"); 
    } 
} 

class Abstrct { 

    public static void main(String args[]) { 
     Figure q = new CircleObject(); 
     q.draw(); 
     Figure p = new Kd(); 
     p.changePosition(2, 4); 
    } 
} 
+0

'Circle'或者需要是'abstract'或者實現'changePosition'方法 – MadProgrammer

回答

0

甲類延伸的抽象類具有任實現其父類的所有抽象方法,或者自行抽象。

所以你的CircleObject類也必須實現changePosition(),或者是一個抽象類本身。

類似於Kd類。

0

當您將類定義爲抽象類時,所有擴展它的類都需要實現所有方法。

abstract class Figure { 
    int x, y; 
    abstract void changePosition(int newX, int newY); 
    abstract void draw(); 
} 

class CircleObject extends Figure { 
    void draw() { 
     System.out.println("Draw method called"); 
    } 
    void changePosition(int newX, int newY){   
    } 
} 

class Kd extends Figure { 
    void changePosition(int newX, int newY) { 
     System.out.println("Change Position method called"); 
    } 
    void draw(){   
    } 
} 
相關問題