java 俄罗斯方块

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

  1. public class ExerciseTest {
  2.     public static void main(String[] args){
  3.         ExerciseTest f = new ExerciseTest();
  4.      System.out.println(f.add("4", "5"));
  5.     }
  6.     
  7.      public int add(int x, int y) {
  8.          return x + y;
  9.     }
  10.     public String add(String x,String y) {
  11.         return x + y;
  12.     }
  13. }

参考答案

上述代码运行后,将打印显示 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的类,代码如下所示:

步骤二:定义属性

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

  1. public class O {
  2.      Cell[] cells;
  3. }

步骤三:定义构造方法

首先,在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属性进行初始化了。代码如下所示:

 
  1. public class O {
  2.     Cell[] cells;
  3.     /**
  4.      * 构造方法,为属性cells进行初始化
  5.      *
  6.      * @param row顺时针方向
  7.      * ,第一个坐标的行
  8.      * @param col顺时针方向
  9.      * ,第一个坐标的列
  10.      */
  11.      public O(int row, int col) {
  12.         cells = new Cell[4];
  13.         // 按顺时针方向初始化Cell
  14.         cells[0] = new Cell(row, col);
  15.         cells[1] = new Cell(row, col + 1);
  16.         cells[2] = new Cell(row + 1, col);
  17.         cells[3] = new Cell(row + 1, col + 1);
  18.     }
  19. }

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

 
  1. public class O {
  2.     Cell[] cells;// 属性,用来存储一个方块的四个格子的坐标
  3.     /**
  4.      * 构造方法,为属性cells进行初始化
  5.      */
  6.     public O() {
  7.         this(0, 0);
  8.     }
  9.     /**
  10.      * 构造方法,为属性cells进行初始化
  11.      *
  12.      * @param row
  13.      * 顺时针方向 ,第一个坐标的行
  14.      * @param col
  15.      * 顺时针方向 ,第一个坐标的列
  16.      */
  17.     public O(int row, int col) {
  18.         cells = new Cell[4];
  19.         // 按顺时针方向初始化Cell
  20.         cells[0] = new Cell(row, col);
  21.         cells[1] = new Cell(row, col + 1);
  22.         cells[2] = new Cell(row + 1, col);
  23.         cells[3] = new Cell(row + 1, col + 1);
  24.     }
  25. }

步骤四:定义打印方法

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

 
  1. public class O {
  2.     Cell[] cells;// 属性,用来存储一个方块的四个格子的坐标
  3.     /**
  4.      * 构造方法,为属性cells进行初始化
  5.      */
  6.     public O() {
  7.         this(0, 0);
  8.     }
  9.     /**
  10.      * 构造方法,为属性cells进行初始化
  11.      *
  12.      * @param row
  13.      * 顺时针方向 ,第一个坐标的行
  14.      * @param col
  15.      * 顺时针方向 ,第一个坐标的列
  16.      */
  17.     public O(int row, int col) {
  18.         cells = new Cell[4];
  19.         // 按顺时针方向初始化Cell
  20.         cells[0] = new Cell(row, col);
  21.         cells[1] = new Cell(row, col + 1);
  22.         cells[2] = new Cell(row + 1, col);
  23.         cells[3] = new Cell(row + 1, col + 1);
  24.     }
  25.     /**
  26.      * 按顺时针方向,打印方块中四个格子所在的坐标
  27.      */
  28.     public void print() {
  29.         String str = "";
  30.         for (int i = 0; i < cells.length - 1; i++) {
  31.             str += "(" + cells[i].getCellInfo() + "), ";
  32.         }
  33.         str += "(" + cells[cells.length - 1].getCellInfo() + ")";
  34.         System.out.println(str);
  35.     }
  36. }

步骤五:测试打印方法

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

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

打印结果如下所示:

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

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

图- 3

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

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

 
  1. public class O {
  2.     Cell[] cells;// 属性,用来存储一个方块的四个格子的坐标
  3.     /**
  4.      * 构造方法,为属性cells进行初始化
  5.      */
  6.     public O() {
  7.         this(0, 0);
  8.     }
  9.     /**
  10.      * 构造方法,为属性cells进行初始化
  11.      *
  12.      * @param row
  13.      * 顺时针方向 ,第一个坐标的行
  14.      * @param col
  15.      * 顺时针方向 ,第一个坐标的列
  16.      */
  17.     public O(int row, int col) {
  18.         cells = new Cell[4];
  19.         // 按顺时针方向初始化Cell
  20.         cells[0] = new Cell(row, col);
  21.         cells[1] = new Cell(row, col + 1);
  22.         cells[2] = new Cell(row + 1, col);
  23.         cells[3] = new Cell(row + 1, col + 1);
  24.     }
  25.     /**
  26.      * 按顺时针方向,打印方块中四个格子所在的坐标
  27.      */
  28.     public void print() {
  29.         String str = "";
  30.         for (int i = 0; i < cells.length - 1; i++) {
  31.             str += "(" + cells[i].getCellInfo() + "), ";
  32.         }
  33.         str += "(" + cells[cells.length - 1].getCellInfo() + ")";
  34.         System.out.println(str);
  35.     }
  36.     /**
  37.      * 使方块下落一个格子
  38.      */
  39.     public void drop() {
  40.         for (int i = 0; i < cells.length; i++) {
  41.             cells[i].row++;
  42.         }
  43.     }
  44. }

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

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

 
  1. public class TestO {
  2.     public static void main(String[] args) {
  3.         O o=new O(0,5);
  4.         //测试print方法
  5.         System.out.println("原始坐标为:");
  6.         o.print();
  7.         
  8.         //测试drop方法
  9.         o.drop();
  10.         System.out.println("调用drop方法后的坐标:");
  11.         o.print();
  12.     }
  13. }

控制台的输出结果为:

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

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

下落前 下落后

图- 4

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

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

 
  1. public class O {
  2.     Cell[] cells;// 属性,用来存储一个方块的四个格子的坐标
  3.     /**
  4.      * 构造方法,为属性cells进行初始化
  5.      */
  6.     public O() {
  7.         this(0, 0);
  8.     }
  9.     /**
  10.      * 构造方法,为属性cells进行初始化
  11.      *
  12.      * @param row
  13.      * 顺时针方向 ,第一个坐标的行
  14.      * @param col
  15.      * 顺时针方向 ,第一个坐标的列
  16.      */
  17.     public O(int row, int col) {
  18.         cells = new Cell[4];
  19.         // 按顺时针方向初始化Cell
  20.         cells[0] = new Cell(row, col);
  21.         cells[1] = new Cell(row, col + 1);
  22.         cells[2] = new Cell(row + 1, col);
  23.         cells[3] = new Cell(row + 1, col + 1);
  24.     }
  25.     /**
  26.      * 按顺时针方向,打印方块中四个格子所在的坐标
  27.      */
  28.     public void print() {
  29.         String str = "";
  30.         for (int i = 0; i < cells.length - 1; i++) {
  31.             str += "(" + cells[i].getCellInfo() + "), ";
  32.         }
  33.         str += "(" + cells[cells.length - 1].getCellInfo() + ")";
  34.         System.out.println(str);
  35.     }
  36.     /**
  37.      * 使方块下落一个格子
  38.      */
  39.     public void drop() {
  40.         for (int i = 0; i < cells.length; i++) {
  41.             cells[i].row++;
  42.         }
  43.     }
  44.     /**
  45.      * 使方块左移一个格子
  46.      */
  47.     public void moveLeft() {
  48.         for (int i = 0; i < cells.length; i++) {
  49.             cells[i].col--;
  50.         }
  51.     }
  52. }

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

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

 
  1. public class TestO {
  2.     public static void main(String[] args) {
  3.         O o=new O(0,5);
  4.         //测试print方法
  5.         System.out.println("原始坐标为:");
  6.         o.print();
  7.         
  8.         //测试drop方法
  9. //        o.drop();
  10. //        System.out.println("调用drop方法后的坐标:");
  11. //        o.print();
  12.         //测试moveLeft方法
  13.         o.moveLeft();
  14.         System.out.println("调用moveLeft方法后的坐标:");
  15.         o.print();
  16.     }
  17. }

控制台的输出结果为:

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

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

左移前 左移后

图- 5

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

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

 
  1. public class O {
  2.     Cell[] cells;// 属性,用来存储一个方块的四个格子的坐标
  3.     /**
  4.      * 构造方法,为属性cells进行初始化
  5.      */
  6.     public O() {
  7.         this(0, 0);
  8.     }
  9.     /**
  10.      * 构造方法,为属性cells进行初始化
  11.      *
  12.      * @param row
  13.      * 顺时针方向 ,第一个坐标的行
  14.      * @param col
  15.      * 顺时针方向 ,第一个坐标的列
  16.      */
  17.     public O(int row, int col) {
  18.         cells = new Cell[4];
  19.         // 按顺时针方向初始化Cell
  20.         cells[0] = new Cell(row, col);
  21.         cells[1] = new Cell(row, col + 1);
  22.         cells[2] = new Cell(row + 1, col);
  23.         cells[3] = new Cell(row + 1, col + 1);
  24.     }
  25.     /**
  26.      * 按顺时针方向,打印方块中四个格子所在的坐标
  27.      */
  28.     public void print() {
  29.         String str = "";
  30.         for (int i = 0; i < cells.length - 1; i++) {
  31.             str += "(" + cells[i].getCellInfo() + "), ";
  32.         }
  33.         str += "(" + cells[cells.length - 1].getCellInfo() + ")";
  34.         System.out.println(str);
  35.     }
  36.     /**
  37.      * 使方块下落一个格子
  38.      */
  39.     public void drop() {
  40.         for (int i = 0; i < cells.length; i++) {
  41.             cells[i].row++;
  42.         }
  43.     }
  44.     /**
  45.      * 使方块左移一个格子
  46.      */
  47.     public void moveLeft() {
  48.         for (int i = 0; i < cells.length; i++) {
  49.             cells[i].col--;
  50.         }
  51.     }
  52.     /**
  53.      * 使用方块右移一个格子
  54.      */
  55.     public void moveRight() {
  56.         for (int i = 0; i < cells.length; i++) {
  57.             cells[i].col++;
  58.         }
  59.     }
  60. }

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

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

 
  1. public class TestO {
  2.     public static void main(String[] args) {
  3.         O o=new O(0,5);
  4.         //测试print方法
  5.         System.out.println("原始坐标为:");
  6.         o.print();
  7.         
  8.         //测试drop方法
  9. //        o.drop();
  10. //        System.out.println("调用drop方法后的坐标:");
  11. //        o.print();
  12.         
  13.         //测试moveLeft方法
  14. //        o.moveLeft();
  15. //        System.out.println("调用moveLeft方法后的坐标:");
  16. //        o.print();
  17.         
  18.         //测试moveRight方法
  19.         o.moveRight();
  20.         System.out.println("调用moveRight方法后的坐标:");
  21.         o.print();
  22.     }
  23. }

控制台的输出结果为:

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

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

右移前 右移后

图- 6

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

 
  1. public class O {
  2.     Cell[] cells;// 属性,用来存储一个方块的四个格子的坐标
  3.     /**
  4.      * 构造方法,为属性cells进行初始化
  5.      */
  6.     public O() {
  7.         this(0, 0);
  8.     }
  9.     /**
  10.      * 构造方法,为属性cells进行初始化
  11.      *
  12.      * @param row
  13.      * 顺时针方向 ,第一个坐标的行
  14.      * @param col
  15.      * 顺时针方向 ,第一个坐标的列
  16.      */
  17.     public O(int row, int col) {
  18.         cells = new Cell[4];
  19.         // 按顺时针方向初始化Cell
  20.         cells[0] = new Cell(row, col);
  21.         cells[1] = new Cell(row, col + 1);
  22.         cells[2] = new Cell(row + 1, col);
  23.         cells[3] = new Cell(row + 1, col + 1);
  24.     }
  25.     /**
  26.      * 按顺时针方向,打印方块中四个格子所在的坐标
  27.      */
  28.     public void print() {
  29.         String str = "";
  30.         for (int i = 0; i < cells.length - 1; i++) {
  31.             str += "(" + cells[i].getCellInfo() + "), ";
  32.         }
  33.         str += "(" + cells[cells.length - 1].getCellInfo() + ")";
  34.         System.out.println(str);
  35.     }
  36.     /**
  37.      * 使方块下落一个格子
  38.      */
  39.     public void drop() {
  40.         for (int i = 0; i < cells.length; i++) {
  41.             cells[i].row++;
  42.         }
  43.     }
  44.     /**
  45.      * 使方块左移一个格子
  46.      */
  47.     public void moveLeft() {
  48.         for (int i = 0; i < cells.length; i++) {
  49.             cells[i].col--;
  50.         }
  51.     }
  52.     /**
  53.      * 使用方块右移一个格子
  54.      */
  55.     public void moveRight() {
  56.         for (int i = 0; i < cells.length; i++) {
  57.             cells[i].col++;
  58.         }
  59.     }
  60. }
 

TestO类的完整代码如下:

 
  1. public class TestO {
  2.     public static void main(String[] args) {
  3.         O o=new O(0,5);
  4.         //测试print方法
  5.         System.out.println("原始坐标为:");
  6.         o.print();
  7.         
  8.         //测试drop方法
  9. //        o.drop();
  10. //        System.out.println("调用drop方法后的坐标:");
  11. //        o.print();
  12.         
  13.         //测试moveLeft方法
  14. //        o.moveLeft();
  15. //        System.out.println("调用moveLeft方法后的坐标:");
  16. //        o.print();
  17.         
  18.         //测试moveRight方法
  19.         o.moveRight();
  20.         System.out.println("调用moveRight方法后的坐标:");
  21.         o.print();
  22.     }
  23. }
 
posted @ 2018-01-22 16:45  清风已来  阅读(4891)  评论(0编辑  收藏  举报