如何在Java中使用boolean编写构造函数?

时间:2018-10-13 09:34:57

标签: java constructor boolean

我的代码如下:

public class Cell {

    private boolean north;
    private boolean east;
    private boolean south;
    private boolean west;

    /** Construct a Cell, using the parameters given to initialize
     * the wall instance variables.
     * 
     * @param north
     * @param south
     * @param east
     * @param west
     */
    public Cell(boolean north, boolean east, boolean south, boolean west) {
       ?
    }

如何用布尔值编写构造函数?谢谢!

1 个答案:

答案 0 :(得分:0)

boolean没什么不同,您可以像其他类型一样初始化它们,如下所示: 您必须使用关键字this(即解决阴影问题)来引用字段变量,因为参数变量具有与字段相同的名称。

public Cell(boolean north, boolean east, boolean south, boolean west) {
  this.north = north;
  this.east=east;
  this.south=south;
  this.west=west;
}