接口中的属性/成员变量?
我想知道是否有任何方法可以使实现器类强制声明对象句柄/基元,就像它们对方法所做的那样。例如:
public interface Rectangle {
int height = 0;
int width = 0;
public int getHeight();
public int getWidth();
public void setHeight(int height);
public void setWidth(int width);
}
public class Tile implements Rectangle{
@Override
public int getHeight() {
return 0;
}
@Override
public int getWidth() {
return 0;
}
@Override
public void setHeight(int height) {
}
@Override
public void setWidth(int width) {
}
}
在上面的方法中,我们如何强制Tile类使用接口声明高度和宽度属性?出于某种原因,我希望仅使用界面来执行此操作!
我最初考虑将其与继承一起使用。但问题是我必须处理3个班级!
- 矩形
- 瓦
- JLabel.!
class Tile extends JLabel implements Rectangle {}
会工作.!
但
class Tile extends JLabel extends Rectangle {}
哇,不是。!