简体   繁体   English

如何定义具有布尔值true或false的名称

[英]How to define a name that has boolean value true or false

My program has many functions with a boolean input true or false. 我的程序有很多函数,布尔输入为true或false。 And the logic is when it's left, it's true; 逻辑是它何时离开,这是真的; when it's right, it's false. 当它是对的时候,这是错误的。

So instead of typing 所以不要打字

myFuncton(true) //input is left
myFunction(false) //input is right

I want to be able to call myFunction like this: 我希望能够像这样调用myFunction:

myFunction(LEFT)
myFunction(RIGHT)

So it's much easier to understand my program 所以理解我的程序要容易得多

In short, how do I define a name with boolean value in JAVA? 简而言之,如何在JAVA中使用布尔值定义名称?

It doesn't seem to me that a boolean is the best fit for your situation, since your goal is not to check for true or false, but rather to see what direction is desired. 在我看来,布尔值最适合您的情况,因为您的目标不是检查是真是假,而是查看所需的方向。 And if you did use a boolean, which direction should be considered "true"? 如果你确实使用了布尔值,那么哪个方向应该被认为是“真”? which false? 哪个假? And what if you instead wanted to add UP and DOWN? 如果你想要添加UP和DOWN怎么办? Again, a boolean doesn't seem like a great fit. 同样,布尔值似乎不太适合。

It looks like your best bet is to instead use an enum: 看起来你最好的选择是使用枚举:

enum Direction {
  LEFT, RIGHT
}

And then define your method: 然后定义您的方法:

public void myFunction(Direction dir) {
  if (dir == Direction.LEFT) {
    // go left
  } else {
    // go right
  }
}

This gives you the advantage of compile time type checking and self-documentation (as per @sqrfv in comments) and so is much better than using a String or a boolean. 这为您提供了编译时类型检查和自我文档的优势(根据注释中的@sqrfv),因此比使用String或布尔值要好得多。 Also note that enums can have parameters and methods, and thus are much more powerful than String or boolean types. 另请注意,枚举可以包含参数和方法,因此比String或boolean类型更强大。


For example, I wanted to use an enum to associate key strokes with directions for a Swing Key Bindings project, and created an enum actually called Direction like so: 例如,我想使用枚举将键击与Swing Key Bindings项目的方向相关联,并创建一个实际上称为Direction的枚举,如下所示:

enum Direction {
   UP("Up", KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0)),
   DOWN("Down", KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0)),
   LEFT("Left", KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0)),
   RIGHT("Right", KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0));

   Direction(String text, KeyStroke keyStroke) {
      this.text = text;
      this.keyStroke = keyStroke;
   }
   private String text;
   private KeyStroke keyStroke;

   public String getText() {
      return text;
   }

   public KeyStroke getKeyStroke() {
      return keyStroke;
   }

   @Override
   public String toString() {
      return text;
   }
}

And then used it in my key bindings like so: 然后在我的键绑定中使用它,如下所示:

class Board2 extends JPanel {
   private static final int PREF_W = 200;
   private static final int PREF_H = 400;
   public static final String DIRECTION = "direction";
   private Direction direction = null;

   public Board2() {
      setBorder(BorderFactory.createTitledBorder("Board2"));
      InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
      ActionMap actionMap = getActionMap();

      for (Direction dir : Direction.values()) {
         inputMap.put(dir.getKeyStroke(), dir.getText());
         actionMap.put(dir.getText(), new MyArrowBinding(dir));
      }
   }

   private class MyArrowBinding extends AbstractAction {
      private Direction dir;

      public MyArrowBinding(Direction dir) {
         super(dir.getText());
         this.dir = dir;
         putValue(ACTION_COMMAND_KEY, dir);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         setDirection(dir);
      }
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   public void setDirection(Direction direction) {
      Direction oldValue = this.direction;
      Direction newValue = direction;
      this.direction = newValue;

      firePropertyChange(DIRECTION, oldValue, newValue);
   }

   public Direction getDirection() {
      return direction;
   }
}

Use final variables, final makes sure you cant change the variable's values. 使用final变量,final确保您无法更改变量的值。

final boolean LEFT = true; 
final boolean RIGHT = false;

then you can use them like this 然后你可以像这样使用它们

myFunction(LEFT)
myFunction(RIGHT)

Look in to defining constants. 查看定义常量。 You'll want them public/protected/private depending on your use. 根据您的使用情况,您需要公开/受保护/私密。

static final boolean LEFT = true;

static final boolean RIGHT = false;

Edit: made RIGHT false. 编辑:做错了。

Final int LEFT = 0
Final int RIGHT = 1

That's how I did my keystrokes, except there was forward and backwards included. 这就是我按键的方式,除了包含前进和后退。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM