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++ のインターフェイスに相当するものを作成することは可能ですか?

4

4 に答える 4

7

次を使用しabstract classます。

public abstract class Square {

    public abstract int getHeight();

    public abstract int getWidth();

    public int getSize() {
        return getWidth() * getHeight();
    }
}
于 2012-06-08T00:19:19.517 に答える
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;
    }
}
于 2012-06-08T00:14:13.687 に答える
1

virtual他の質問に答えるには、はい、キーワードを使用して C++ で実行できます。私の知る限り、これは C++ におけるポリモーフィズムの主要な方法です。

この一連のチュートリアルは素晴らしいです。C/C++ についてさらに学びたい場合は、しっかりと読み通すことをお勧めします。

于 2012-06-08T01:36:57.810 に答える
1

インターフェイスと抽象クラスを混同していると思います:

インターフェイスは、すべての実装クラスが従わなければならない規約を記述します。これは基本的にメソッドのリストです (そして重要なことに、メソッドが何を返すべきか、どのように振る舞うべきかなどのドキュメントです。

どのメソッドにも本体がないことに注意してください。インターフェイスはそうしません。ただし、インターフェイスで static final 定数を定義することはできます。

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();
}

抽象クラスは一部のメソッドを実装しますが、すべてではありません。(技術的には、抽象クラスはすべてのメソッドを実装できますが、それは良い例にはなりません)。クラスを拡張すると、抽象化されたメソッドが実装されるという意図があります。

/*
 * 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
  }
}
于 2012-06-08T10:38:57.997 に答える