1 请描述下列代码的运行结果

public class ExerciseTest {
	public static void main(String[] args){
		ExerciseTest f = new ExerciseTest();
	    System.out.println(f.add("4", "5"));
	}
	
	 public int add(int x, int y) {
		 return x + y;
	}
	public String add(String x,String y) {
		return x + y;
	}
}

参考答案

上述代码运行后,将打印显示 45。这是因为,调用 add 方法时,传入的是 String 类型的变量,因此,执行第二个 add 方法,实现字符串的连接,得到字符串“45”。

2 关于构造方法,下面说法正确的是

A.构造方法不能带有参数

B.构造方法的名称必须和类名相同

C.构造方法可以定义返回值

D.构造方法不能重载

参考答案

B 选项的说法正确。

在Java语言中,可以通过构造方法实现对对象成员变量的初始化。构造方法是在类中定义的方法,但不同于其他的方法,构造方法的定义有如下规则:

1. 构造方法的名称必须与类名同名;

2. 构造方法没有返回值,但也不能写void,如果有返回值就是方法了。

另外,为了使用方便,可以对一个类定义多个构造方法,这些构造方法都有相同的名称,方法的参数不同,称之为构造方法的重载。在创建对象时,Java编译器会根据不同的参数调用不同构造方法。

3 定义Tetris项目中的O类并测试

在今天的课上案例中已经定义了T型和J型方块对应的类,本案例要求模仿课上定义的T型方块的类T和J型方块的类J,来定义O型方块对应的类O。O型方块的形状如图-1中的红色方块。

图- 1

参考答案

首先,定义名为O的类,由于每个方块有四个格子,因此,在O类中添加属性cells,cells属性的类型为Cell数组类型,即,Cell[]。这样,cells数组中就可以存储四个格子来表示一个方块。

其次,为O类添加构造方法。可以提供无参数构造方法,及按顺时针方向,方块中第一个格子的行和列作为参数的构造方法。

第三,为了方便查看方块中四个格子的坐标,因此,定义print方法,实现按顺时针方向,打印方块中四个格子所在的坐标,并对print进行测试。

第四,每个方块都可以下落,左移和右移,因此,在O类中,定义drop方法实现方块下落;定义moveLeft方法实现方块左移;定义moveRight方法实现方块右移,并对这三个方法进行测试。

实现此案例需要按照如下步骤进行。

步骤一:定义O类

首先定义一个名为 O的类,代码如下所示:

public class O {
}

步骤二:定义属性

由于每个方块有四个格子,因此,在类 O中定义一个名为cells的属性,cells属性的类型为Cell数组类型,即,Cell[]。代码如下所示:

public class O {
#cold_bold	  Cell[] cells;
}

步骤三:定义构造方法

首先,在O类中添加第一个格子的行和列作为参数的构造方法,并在构造方法中按顺时针方向初始化O型方块。

图- 2

从图-2中,按顺时针方向查看O型方块,可以看出,第一个格子的行列坐标是(0,5),第二个格子的行列坐标是(0,6),第三个格子的行列坐标是(1,5),第四个格子的行列坐标是(1,6)。假设把第一个格子的行列坐标用(row,col)来表示,那么,第二个格子的行列坐标为(row,col+1), 第三个格子的行列坐标为(row+1,col), 第四个格子的行列坐标为(row+1,col+1),这样,O型方块就可以用传入的参数对cells属性进行初始化了。代码如下所示:

public class O {
	Cell[] cells;
#cold_bold	/**
#cold_bold	 * 构造方法,为属性cells进行初始化
#cold_bold	 * 
#cold_bold	 * @param row顺时针方向
#cold_bold	 *            ,第一个坐标的行
#cold_bold	 * @param col顺时针方向
#cold_bold	 *            ,第一个坐标的列
	 */
#cold_bold	 public O(int row, int col) {
#cold_bold		cells = new Cell[4];
#cold_bold		// 按顺时针方向初始化Cell
#cold_bold		cells[0] = new Cell(row, col);
#cold_bold		cells[1] = new Cell(row, col + 1);
#cold_bold		cells[2] = new Cell(row + 1, col);
#cold_bold		cells[3] = new Cell(row + 1, col + 1);
#cold_bold	}

}

然后,在O类中添加无参数构造方法,在该构造方法中,使用this关键字调用参数为O(int row, int col)的构造方法,并设置方块的顺时针方向的第一个格子的行和列为(0,0),代码如下所示:

public class O {
	Cell[] cells;// 属性,用来存储一个方块的四个格子的坐标

#cold_bold	/**
#cold_bold	 * 构造方法,为属性cells进行初始化
#cold_bold	 */
#cold_bold	public O() {
#cold_bold		this(0, 0);
#cold_bold	}

	/**
	 * 构造方法,为属性cells进行初始化
	 * 
	 * @param row
	 *            顺时针方向 ,第一个坐标的行
	 * @param col
	 *            顺时针方向 ,第一个坐标的列
	 */
	public O(int row, int col) {
		cells = new Cell[4];
		// 按顺时针方向初始化Cell
		cells[0] = new Cell(row, col);
		cells[1] = new Cell(row, col + 1);
		cells[2] = new Cell(row + 1, col);
		cells[3] = new Cell(row + 1, col + 1);
	}
}

步骤四:定义打印方法

为了方便查看方块中四个格子的坐标,因此,定义print方法。在print方法中,按顺时针方向,打印方块中四个格子所在的坐标。实现此功能,循环遍历cells数组即可。代码如下所示:

public class O {
	Cell[] cells;// 属性,用来存储一个方块的四个格子的坐标

	/**
	 * 构造方法,为属性cells进行初始化
	 */
	public O() {
		this(0, 0);
	}

	/**
	 * 构造方法,为属性cells进行初始化
	 * 
	 * @param row
	 *            顺时针方向 ,第一个坐标的行
	 * @param col
	 *            顺时针方向 ,第一个坐标的列
	 */
	public O(int row, int col) {
		cells = new Cell[4];
		// 按顺时针方向初始化Cell
		cells[0] = new Cell(row, col);
		cells[1] = new Cell(row, col + 1);
		cells[2] = new Cell(row + 1, col);
		cells[3] = new Cell(row + 1, col + 1);
	}


#cold_bold	/**
#cold_bold	 * 按顺时针方向,打印方块中四个格子所在的坐标
#cold_bold	 */
#cold_bold	public void print() {
#cold_bold		String str = "";
#cold_bold		for (int i = 0; i < cells.length - 1; i++) {
#cold_bold			str += "(" + cells[i].getCellInfo() + "), ";
#cold_bold		}
#cold_bold		str += "(" + cells[cells.length - 1].getCellInfo() + ")";
#cold_bold		System.out.println(str);
#cold_bold
#cold_bold	}
}

步骤五:测试打印方法

新建名为TestO的类,在类中添加main方法,并在main方法中,首先,创建O类的对象,将该对象的cells属性的第一个格子的行和列设置为(0,5);然后调用print方法,打印cells属性中的四个格子坐标,代码如下所示:

public class TestO {
	public static void main(String[] args) {
		O o=new O(0,5);
     System.out.println("原始坐标为:");
		o.print();
	}
}

打印结果如下所示:

原始坐标为:
(0,5), (0,6), (1,5), (1,6)

根据以上结果,对照图-3中,顺时针方向看O型的每个格子的坐标,可以看出与打印结果是匹配的。

图- 3

步骤六:定义方块的下落方法

定义方块的下落的方法,即,在O类中,添加方块下落一个格子的方法。要实现下落一个格子,只需要循环cells属性,将方块中的每一个格子的行加1即可。代码如下所示:

public class O {
	Cell[] cells;// 属性,用来存储一个方块的四个格子的坐标

	/**
	 * 构造方法,为属性cells进行初始化
	 */
	public O() {
		this(0, 0);
	}

	/**
	 * 构造方法,为属性cells进行初始化
	 * 
	 * @param row
	 *            顺时针方向 ,第一个坐标的行
	 * @param col
	 *            顺时针方向 ,第一个坐标的列
	 */
	public O(int row, int col) {
		cells = new Cell[4];
		// 按顺时针方向初始化Cell
		cells[0] = new Cell(row, col);
		cells[1] = new Cell(row, col + 1);
		cells[2] = new Cell(row + 1, col);
		cells[3] = new Cell(row + 1, col + 1);
	}


	/**
	 * 按顺时针方向,打印方块中四个格子所在的坐标
	 */
	public void print() {
		String str = "";
		for (int i = 0; i < cells.length - 1; i++) {
			str += "(" + cells[i].getCellInfo() + "), ";
		}
		str += "(" + cells[cells.length - 1].getCellInfo() + ")";
		System.out.println(str);

	}

	/**
#cold_bold	 * 使方块下落一个格子
#cold_bold	 */
#cold_bold	public void drop() {
#cold_bold		for (int i = 0; i < cells.length; i++) {
#cold_bold			cells[i].row++;
#cold_bold		}
#cold_bold	}
}

步骤七:测试方块下落的方法

在TestO类中的main方法中,首先,调用对象o的drop方法,然后,再调用对象o的print方法查看坐标的变化情况,代码如下所示:

public class TestO {
	public static void main(String[] args) {
		O o=new O(0,5);
		//测试print方法
#cold_bold		System.out.println("原始坐标为:");
		o.print();
		
#cold_bold		//测试drop方法
#cold_bold		o.drop();
#cold_bold		System.out.println("调用drop方法后的坐标:");
#cold_bold		o.print();
	}
}

控制台的输出结果为:

原始坐标为:
(0,5), (0,6), (1,5), (1,6)
调用drop方法后的坐标:
(1,5), (1,6), (2,5), (2,6)

从输出结果上,可以看出方块中的每个格子的行都在原有基础上增加了1。界面对比效果如图-4所示。

下落前 下落后

图- 4

步骤八:定义方块的左移方法

定义方块的左移的方法,即,在O类中,添加方块左移一个格子的方法。要实现方块左移一个格子,只需要循环cells属性,将方块中的每一个格子的列减1即可。代码如下所示:

public class O {
	Cell[] cells;// 属性,用来存储一个方块的四个格子的坐标

	/**
	 * 构造方法,为属性cells进行初始化
	 */
	public O() {
		this(0, 0);
	}

	/**
	 * 构造方法,为属性cells进行初始化
	 * 
	 * @param row
	 *            顺时针方向 ,第一个坐标的行
	 * @param col
	 *            顺时针方向 ,第一个坐标的列
	 */
	public O(int row, int col) {
		cells = new Cell[4];
		// 按顺时针方向初始化Cell
		cells[0] = new Cell(row, col);
		cells[1] = new Cell(row, col + 1);
		cells[2] = new Cell(row + 1, col);
		cells[3] = new Cell(row + 1, col + 1);
	}

	/**
	 * 按顺时针方向,打印方块中四个格子所在的坐标
	 */
	public void print() {
		String str = "";
		for (int i = 0; i < cells.length - 1; i++) {
			str += "(" + cells[i].getCellInfo() + "), ";
		}
		str += "(" + cells[cells.length - 1].getCellInfo() + ")";
		System.out.println(str);

	}

	/**
	 * 使方块下落一个格子
	 */
	public void drop() {
		for (int i = 0; i < cells.length; i++) {
			cells[i].row++;
		}
	}

#cold_bold	/**
#cold_bold	 * 使方块左移一个格子
#cold_bold	 */
#cold_bold	public void moveLeft() {
#cold_bold		for (int i = 0; i < cells.length; i++) {
#cold_bold			cells[i].col--;
#cold_bold		}
#cold_bold	}

}

步骤九:测试方块的左移方法

在TestO类中的main方法中,首先,将测试方块下落的代码注释,然后,调用对象o的moveLeft方法,最后,再调用对象o的print方法查看坐标的变化情况,代码如下所示:

public class TestO {
	public static void main(String[] args) {
		O o=new O(0,5);
		//测试print方法
		System.out.println("原始坐标为:");
		o.print();
		
		//测试drop方法
//		o.drop();
//		System.out.println("调用drop方法后的坐标:");
//		o.print();
#cold_bold		//测试moveLeft方法
#cold_bold		o.moveLeft();
#cold_bold		System.out.println("调用moveLeft方法后的坐标:");
#cold_bold		o.print();
	}
}

控制台的输出结果为:

原始坐标为:
(0,5), (0,6), (1,5), (1,6)
调用moveLeft方法后的坐标:
(0,4), (0,5), (1,4), (1,5)

从输出结果上,可以看出O型方块中的每个格子的列都在原有的基础上减去了1。界面对比效果如图-5所示。

左移前 左移后

图- 5

步骤十:定义方块的右移方法

定义方块的右移的方法,即,在O类中,添加方块右移一个格子的方法。要实现方块右移一个格子,只需要循环cells属性,将方块中的每一个格子的列加1即可。代码如下所示:

public class O {
	Cell[] cells;// 属性,用来存储一个方块的四个格子的坐标

	/**
	 * 构造方法,为属性cells进行初始化
	 */
	public O() {
		this(0, 0);
	}

	/**
	 * 构造方法,为属性cells进行初始化
	 * 
	 * @param row
	 *            顺时针方向 ,第一个坐标的行
	 * @param col
	 *            顺时针方向 ,第一个坐标的列
	 */
	public O(int row, int col) {
		cells = new Cell[4];
		// 按顺时针方向初始化Cell
		cells[0] = new Cell(row, col);
		cells[1] = new Cell(row, col + 1);
		cells[2] = new Cell(row + 1, col);
		cells[3] = new Cell(row + 1, col + 1);
	}


	/**
	 * 按顺时针方向,打印方块中四个格子所在的坐标
	 */
	public void print() {
		String str = "";
		for (int i = 0; i < cells.length - 1; i++) {
			str += "(" + cells[i].getCellInfo() + "), ";
		}
		str += "(" + cells[cells.length - 1].getCellInfo() + ")";
		System.out.println(str);

	}

	/**
	 * 使方块下落一个格子
	 */
	public void drop() {
		for (int i = 0; i < cells.length; i++) {
			cells[i].row++;
		}
	}

	/**
	 * 使方块左移一个格子
	 */
	public void moveLeft() {
		for (int i = 0; i < cells.length; i++) {
			cells[i].col--;
		}
	}

#cold_bold	/**
#cold_bold	 * 使用方块右移一个格子
#cold_bold	 */
#cold_bold	public void moveRight() {
#cold_bold		for (int i = 0; i < cells.length; i++) {
#cold_bold			cells[i].col++;
#cold_bold		}
#cold_bold	}
}

步骤十一:测试方块的右移方法

在TestO类中的main方法中,首先,将测试方块的左移的代码注释,然后,调用对象o的moveRight方法,最后,再调用对象o的print方法查看坐标的变化情况,代码如下所示:

public class TestO {
	public static void main(String[] args) {
		O o=new O(0,5);
		//测试print方法
		System.out.println("原始坐标为:");
		o.print();
		
		//测试drop方法
//		o.drop();
//		System.out.println("调用drop方法后的坐标:");
//		o.print();
		
		//测试moveLeft方法
//		o.moveLeft();
//		System.out.println("调用moveLeft方法后的坐标:");
//		o.print();
		
#cold_bold		//测试moveRight方法
#cold_bold		o.moveRight();
#cold_bold		System.out.println("调用moveRight方法后的坐标:");
#cold_bold		o.print();
	}
}

控制台的输出结果为:

原始坐标为:
(0,5), (0,6), (1,5), (1,6)
调用moveRight方法后的坐标:
(0,6), (0,7), (1,6), (1,7)

从输出结果上,可以看出O型方块中的每个格子的列都在原有的基础上增加了1。界面对比效果如图-6所示。

右移前 右移后

图- 6

本案例中,O类的完整代码如下所示:

public class O {
	Cell[] cells;// 属性,用来存储一个方块的四个格子的坐标

	/**
	 * 构造方法,为属性cells进行初始化
	 */
	public O() {
		this(0, 0);
	}

	/**
	 * 构造方法,为属性cells进行初始化
	 * 
	 * @param row
	 *            顺时针方向 ,第一个坐标的行
	 * @param col
	 *            顺时针方向 ,第一个坐标的列
	 */
	public O(int row, int col) {
		cells = new Cell[4];
		// 按顺时针方向初始化Cell
		cells[0] = new Cell(row, col);
		cells[1] = new Cell(row, col + 1);
		cells[2] = new Cell(row + 1, col);
		cells[3] = new Cell(row + 1, col + 1);
	}

	/**
	 * 按顺时针方向,打印方块中四个格子所在的坐标
	 */
	public void print() {
		String str = "";
		for (int i = 0; i < cells.length - 1; i++) {
			str += "(" + cells[i].getCellInfo() + "), ";
		}
		str += "(" + cells[cells.length - 1].getCellInfo() + ")";
		System.out.println(str);
	}

	/**
	 * 使方块下落一个格子
	 */
	public void drop() {
		for (int i = 0; i < cells.length; i++) {
			cells[i].row++;
		}
	}

	/**
	 * 使方块左移一个格子
	 */
	public void moveLeft() {
		for (int i = 0; i < cells.length; i++) {
			cells[i].col--;
		}
	}

	/**
	 * 使用方块右移一个格子
	 */
	public void moveRight() {
		for (int i = 0; i < cells.length; i++) {
			cells[i].col++;
		}
	}
}

TestO类的完整代码如下:

public class TestO {
	public static void main(String[] args) {
		O o=new O(0,5);
		//测试print方法
		System.out.println("原始坐标为:");
		o.print();
		
		//测试drop方法
//		o.drop();
//		System.out.println("调用drop方法后的坐标:");
//		o.print();
		
		//测试moveLeft方法
//		o.moveLeft();
//		System.out.println("调用moveLeft方法后的坐标:");
//		o.print();
		
		//测试moveRight方法
		o.moveRight();
		System.out.println("调用moveRight方法后的坐标:");
		o.print();
	}
}