문제

I am just beginning to understand the concept of Enumeration in Java, but I'm still kind of skeptical of my understanding.

For something such as: Direction, I can understand its use, where I use numbers to represent a direction, but never really need to do arithmetic with it.

However, given something like sizes, where the sizes are constants, but I will need to do arithmetic, would final declarations be more appropriate? Or attempting to do an enum with

public int getSize()
{
    return size;
}

Here is my code for my current declarations:

private static class Size
{
    private static final int BOX = 5; //5 pixels
    private static final int GRID = 30; //amount of boxes
    private static final int GAME = BOX * GRID; //total size in pixels
}

Note that this is a private nested class, I also use these values when doing drawing, and a parameter is an integer, I simply do Size.BOX, however, is Enumeration still more appropriate for this? and how should I go about it?

도움이 되었습니까?

해결책

The entire point of enums is to make your code more readable and maintainable. You should use them anytime you need to represent an enumerated set of elements.

However, in this case, final/constant is more appropriate since you are representing a value (number of boxes, and number of pixels) not an enumerated set. If, say, you wanted to store types of boxes (metal box, cardboard box, ect) then you would use enums.

다른 팁

You are correct in believing that enums are inappropriate when dealing with arithmetic values. Enums are useful for representing a set (in the programming and the mathematical sense) of uniquely defined items that relate to each other.

Using an enum to replace a size value in this way is nonsensical in context. If your item is 30 units large, then you need to represent it as such, using an integer or the like.

An example of where to use an enum in a similar context would be if you had 5 strictly defined types of items of different sizes - in this case, you could make each of the 5 an instance of your enum - but they would still have a size variable internally, with a different value for each.

Takes a bit of interface chicanery, but this works. Not necessarily the best approach in your situation, but this was a neat little exercise.

/**
   <P>{@code java EnumXmpl}</P>
 **/
public class EnumXmpl  {
   public static final void main(String[] igno_red)  {
      System.out.println("BOX: " + eShape.BOX.getSize());
      System.out.println("GRID: " + eShape.GRID.getSize());
      System.out.println("GAME: " + eShape.GAME.getSize());
   }
}

interface ShapeConstants  {
   int iBOX_PXLS = 5;
   int iGRID_BOXES = 30;
}
enum eShape implements ShapeConstants  {
   BOX(iBOX_PXLS),
   GRID(iGRID_BOXES),
   GAME(iBOX_PXLS * iGRID_BOXES);

   private final int iSz;

   eShape(int i_size)  {
      iSz = i_size;
   }
   public final int getSize()  {
      return  iSz;
   }
}

Output:

[C:\java_code\]java EnumXmpl
BOX: 5
GRID: 30
GAME: 150
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top