2012-06-08 21 views
0

我想創建一個實現一些自己的方法在Java中(但語言不會允許這樣做,如下圖所示)的接口的接口:創建實現一些自己的方法

//Java-style pseudo-code 
public interface Square { 
    //Implement a method in the interface itself 
    public int getSize(){//this can't be done in Java; can it be done in C++? 
     //inherited by every class that implements getWidth() 
     //and getHeight()  
     return getWidth()*getHeight(); 
    } 
    public int getHeight(); 
    public int getWidth(); 
} 

//again, this is Java-style psuedocode 
public class Square1 implements Square{ 

    //getSize should return this.getWidth()*this.getHeight(), as implemented below 

    public int getHeight(){ 
     //method body goes here 
    } 

    public int getWidth{ 
     //method body goes here 
    } 
} 

是否有可能在C++中創建等價的接口來實現一些自己的方法?

+0

是否有任何理由你不能只擴展類而不是實現它? –

+11

改爲創建抽象類。 –

回答

1

爲了回答您的其他問題,是的,它可以用C++實現通過使用virtual關鍵字。據我所知,它是C++多態性的主要方法。

This set of tutorials很棒;如果您想了解更多關於C/C++的信息,我會推薦一個可靠的通讀。

5

它必須是接口嗎?也許抽象類會更好。

public abstract class Square { 
    public int getSize() { 
     return getWidth() * getHeight(); 
    } 

    //no body in abstract methods 
    public abstract int getHeight(); 
    public abstract int getWidth(); 
} 

public class Square1 extends Square { 

    public int getHeight() { 
     return 1; 
    } 

    public int getWidth() { 
     return 1; 
    } 
} 
+0

這適用於不需要多重繼承的情況。 –

7

使用abstract class

public abstract class Square { 

    public abstract int getHeight(); 

    public abstract int getWidth(); 

    public int getSize() { 
     return getWidth() * getHeight(); 
    } 
} 
1

我想你混淆接口與抽象類:

接口描述了所有實現類必須遵守合同。 它基本上是方法(和,重要的是,他們應該返回什麼文件,他們應該做的等

注意的方法NONE有一個機構。接口不這樣做。 您的列表但是,可以在接口定義靜態最終常量。

public interface Shape 
{ 
    /** 
    * returns the area of the shape 
    * throws NullPointerException if either the height or width of the shape is null 
    */ 
    int getSize(); 
    /** 
    * returns the height of the shape 
    */ 
    int getHeight(); 
    /** 
    * returns the width of the shape 
    */ 
    int getWidth(); 
} 

抽象類實現的一些方法,但不是全部。(技術上抽象類可以實現所有方法,但止跌」爲了一個好的e xample)。其意圖是擴展類將實現抽象的方法。

/* 
* A quadrilateral is a 4 sided object. 
* This object has 4 sides with 90' angles i.e. a Square or a Rectangle 
*/ 
public abstract class Quadrilateral90 implements Shape 
{ 
    public int getSize() 
    { 
    return getHeight() * getWidth(); 
    } 
    public abstract int getHeight(); // this line can be omitted 
    public abstract int getWidth(); // this line can be omitted 
} 

最後,擴展對象實現了抽象父類和接口中的所有其餘方法。注意getSize()在這裏沒有實現(儘管你可以覆蓋它,如果你想的話)。

/* 
* The "implements Shape" part may be redundant here as it is declared in the parent, 
* but it aids in readability, especially if you later have to do some refactoring. 
*/ 
public class Square extends Quadrilateral90 implements Shape 
{ 
    public int getHeight() 
    { 
    // method body goes here 
    } 
    public int getWidth() 
    { 
    // method body goes here 
    } 
} 
+0

之前,有人提到它,我知道Quadrilateral90是一個可怕的,可怕的名字對於一個對象 KidTempo