将被matrix引用的布尔值的二维数组将用于模拟基于led的显示,可以容纳LETTERS_PER_DISPLAY字母。
修改构造函数,创建一个具有FONT_LETTER_HEIGHT行和(FONT_LETTER_WIDTH乘以LETTERS_PER_DISPLAY)列的二维布尔数组,并将其赋值给实例变量matrix。
public class LEDDisplay
{
private boolean[] = matrix;
private static final int FONT_LETTER_HEIGHT = 5;
private static final int FONT_LETTER_WIDTH = 6;
private static final int LETTERS_PER_DISPLAY = 10;
public LEDDisplay()
{
boolean[][] matrix = new boolean[FONT_LETTER_HEIGHT][FONT_LETTER_WIDTH]
}
请告诉我我的构造函数是否正确?
这实际上是不正确的-您通过定义构造函数的局部来隐藏成员变量matrix。下面是正确的方法:
public class LEDDisplay
{
private boolean[][] matrix;
private static final int FONT_LETTER_HEIGHT = 5;
private static final int FONT_LETTER_WIDTH = 6;
private static final int LETTERS_PER_DISPLAY = 10;
public LEDDisplay()
{
matrix = new boolean[FONT_LETTER_HEIGHT][FONT_LETTER_WIDTH * LETTERS_PER_DISPLAY];
}
你的一维数组矩阵是类变量,而构造函数中的矩阵对构造函数是局部的,在构造函数之外不可见。
关于代码的两个小问题:
-
矩阵定义为类的成员变量,应该定义为二维数组,然后不需要在构造函数中重新定义
-
原帖子说应该有
"(FONT_LETTER_WIDTH times LETTERS_PER_DISPLAY) columns"
和构造函数只包含FONT_LETTER_WIDTH
像这样:
public class LEDDisplay
{
private boolean[][] matrix;
private static final int FONT_LETTER_HEIGHT = 5;
private static final int FONT_LETTER_WIDTH = 6;
private static final int LETTERS_PER_DISPLAY = 10;
public LEDDisplay()
{
matrix = new boolean[FONT_LETTER_HEIGHT][FONT_LETTER_WIDTH * LETTERS_PER_DISPLAY];
}
FONT_LETTER_HEIGHT行和(FONT_LETTER_WIDTH timesLETTERS_PER_DISPLAY)列
应该是:
new boolean[FONT_LETTER_HEIGHT][FONT_LETTER_WIDTH * LETTERS_PER_DISPLAY]
并赋值给实例变量矩阵
public LEDDisplay()
{
matrix = new boolean[FONT_LETTER_HEIGHT][FONT_LETTER_WIDTH]
}
然而boolean[][] matrix = new boolean[FONT_LETTER_HEIGHT][FONT_LETTER_WIDTH]
创建一个局部变量matrix
,该变量仅在构造函数中可见。这对实例变量matrix
没有影响。