所以我想说我有这个界面:
public interface IBox
{
public void setSize(int size);
public int getSize();
public int getArea();
//...and so on
}
Run Code Online (Sandbox Code Playgroud)
我有一个实现它的类:
public class Rectangle implements IBox
{
private int size;
//Methods here
}
Run Code Online (Sandbox Code Playgroud)
如果我想使用IBox接口,我实际上无法创建它的实例,方式如下:
public static void main(String args[])
{
Ibox myBox=new Ibox();
}
Run Code Online (Sandbox Code Playgroud)
对?所以我实际上必须这样做:
public static void main(String args[])
{
Rectangle myBox=new Rectangle();
}
Run Code Online (Sandbox Code Playgroud)
如果这是真的,那么接口的唯一目的是确保实现接口的类如接口所描述的那样在其中获得了正确的方法?或者是否还有其他任何接口用途?