当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Create an interface that implements some of its own methods

问题描述:

I want to create an interface that implements some of its own methods in Java (but the language won't allow this, as shown below):

//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

}

}

Is it possible to create the equivalent of an interface in C++ that can implement some of its own methods?

网友答案:

To answer your other question, yes it can be done in C++ through the use of the virtual keyword. To my knowledge, it is the primary method of polymorphism in C++.

This set of tutorials is great; I would recommend a solid read-through if you want to learn more about C/C++.

网友答案:

Use an abstract class:

public abstract class Square {

    public abstract int getHeight();

    public abstract int getWidth();

    public int getSize() {
        return getWidth() * getHeight();
    }
}
网友答案:

Does it have to be interface? Maybe abstract class will be better.

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;
    }
}
网友答案:

I think you're mixing up interfaces with abstract classes:

An interface describes the contract that all implementing classes must obey. It is basically a list of methods (and, importantly, documentation of what they should return, how they should behave etc.

Notice that NONE of the methods have a body. Interfaces do not do that. You can, however, define static final constants in an interface.

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

An abstract class implements some of the methods, but not all. (Technically the abstract class could implement all methods, but that wouldn't make for a good example). The intention is that extending classes will implement the abstracted methods.

/*
 * 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
}

Finally, the extending object implements all the remaining methods both in the abstract parent class and in the interface. Notice that getSize() is not implemented here (though you could override it if you wanted to).

/*
 * 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
  }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: