有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在java中用布尔值编写构造函数?

我的代码如下所示:

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) 个答案

  1. # 1 楼答案

    boolean没有什么不同,您可以类似于以下其他类型初始化它们: 您必须使用关键字this(即解析阴影)来引用字段变量,因为参数变量与字段具有相同的名称

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