我正在为Android开发一个工程应用程序。问题是我需要绘制:矩形,由矩形组成的图形及其尺寸。然后,如果您触摸一个维度的一个极端,则可以使矩形的维度更长或更短。
我正在实施下一个计划以实现我的目标:
class DrawFigureWithDimensions extends View{
// implementation of the draw methods
// ...
class Rectangles{
// characterization of the attributes needed for each "rectangle"
// ...
class DimensionPositionType{ ... }
class DimensionExtremeType{ ... }
}
class DrawRectangleWithDimension extends DrawFiguresWithDimensions{ ... }
class DrawBoxWithDimension extends DrawFiguresWithDimensions{ ... }
...
然后,我在实现DimensionPositionType和DimensionExtremeType的内部类时遇到问题,我无法找到如何以合适的方式声明它们。我需要能够在扩展类中决定,比如DrawRectangleWithDimensions,例如,我需要什么类型的极端类型:固定或可移动。像这样:
public class DrawRectangleWithDimensions extends DrawFiguresWithDimensions {
public DrawRectangleWithDimensions(Context context) {
super(context);
}
public void setFigure(double width, double height) {
figureRectangles = new Rectangle[1];
figureRectangles[0] = new Rectangle(0, 0, width, height);
figureRectangles[0].setHorizontalDimensionLeftExtremeType(FIXED);
figureRectangles[0].setHorizontalDimensionRightExtremeType(MOVABLE);
}
例如,这是我对内部类DimensionExtremeType:
的代码 class DimensionExtremeType{
boolean FIXED;
boolean MOVABLE;
DimensionExtremeType(String arg){
if(arg == "FIXED"){
setFixedExtreme();
}else if(arg == "MOVABLE"){
setMovableExtreme();
}
}
public void setFixedExtreme(){
FIXED = true;
MOVABLE = false;
}
public void setMovableExtreme(){
FIXED = false;
MOVABLE = true;
}
public String getDimensionExtremeType(){
if(FIXED==true){
return "FIXED";
}else if(MOVABLE==true){
return "MOVABLE";
}else {
return null;
}
}
}
答案 0 :(得分:0)
我只知道类enum的存在,它解决了我的设计问题。这是一种更容易实现的方式。根据我在问题的措辞中写的示例,这是使用类枚举的代码:
static class Rectangle{
// ...
DimensionExtremeType horizontalDimensionLeftExtremeType;
DimensionExtremeType horizontalDimensionRightExtremeType;
DimensionExtremeType verticalDimensionUpperExtremeType;
DimensionExtremeType verticalDimensionLowerExtremeType;
// =-=-=-= DIMENSION POSITION TYPES =-=-=-=
public enum DimensionExtremeType{FIXED, MOVABLE}
}
然后,在扩展类中,例如DrawRectanglesWithDimensions:
public class DrawRectangleWithDimensions extends DrawFiguresWithDimensions {
public DrawRectangleWithDimensions(Context context) {
super(context);
}
public void setFigure(float width, float height) {
figureRectangles = new Rectangle[1];
figureRectangles[0] = new Rectangle(0, 0, width, height);
figureRectangles[0].horizontalDimensionLeftExtremeType = Rectangle.DimensionExtremeType.FIXED;
figureRectangles[0].horizontalDimensionRightExtremeType = Rectangle.DimensionExtremeType.MOVABLE;
figureRectangles[0].verticalDimensionUpperExtremeType = Rectangle.DimensionExtremeType.MOVABLE;
figureRectangles[0].horizontalDimensionRightExtremeType = Rectangle.DimensionExtremeType.MOVABLE;
}
}