[关闭]
@Hubertoo 2017-07-17T05:20:43.000000Z 字数 4613 阅读 773

OOP.Unit02

达内



知识体系

方法

方法的重载

构造方法

数组

引用类型数组

  1. Cell[] cells = new Cell[4];
  2. cells[0] = new Cell(1,3);
  3. cells[1] = new Cell(2);
  4. cells[2] = new Cell();
  5. cells[3] = new Cell(1,2);
  6. 实际在堆中开始的cells[i]也是存放的地址信息,还是要用构造方法来初始化指向的对象

经典案例

重载drop方法和moveLeft方法

  1. package day02;
  2. public class Cell {
  3. int row;
  4. int col;
  5. //下落一行
  6. public void drop(){
  7. row++;
  8. }
  9. //drop方法的重载
  10. public void drop(int d){
  11. row += d;
  12. }
  13. //左移一列
  14. public void moveLeft(){
  15. col --;
  16. }
  17. //重载moveLeft()
  18. public void moveLeft(int d){
  19. col -= d;
  20. }
  21. //右移一列
  22. public void moveRight(){
  23. col++;
  24. }
  25. //获取格子信息
  26. public String getCellInfo(){
  27. return row + ",,," + col;
  28. }
  29. }

为Cell类添加构造方法

  1. //添加构造方法以及重载
  2. public Cell(int row, int col){
  3. this.row = row;
  4. this.col = col;
  5. }
  6. public Cell(int d){
  7. this(d,d);
  8. }
  9. public Cell(){
  10. this(0,0);
  11. }

定义Tetris项目中的T类和J类并测试

T类

  1. package day02;
  2. public class T {
  3. Cell[] cells;
  4. //构造方法用于T类的初始化
  5. public T(int row, int col){
  6. cells = new Cell[4]; //顺时针初始化
  7. cells[0] = new Cell(row, col);
  8. cells[1] = new Cell(row,col++);
  9. cells[1] = new Cell(row, col+2);
  10. cells[0] = new Cell(row+1, col+1);
  11. }
  12. public T(){
  13. this(0, 0);
  14. }
  15. //打印格子中四个格子所在的坐标
  16. public void print(){
  17. String str = "";
  18. for(int i=0; i<cells.length-1; i++){
  19. str +="(" +cells[cells.length-1].getCellInfo() + ")";
  20. System.out.println(str);
  21. }
  22. System.out.println(str);
  23. }
  24. //方块的下落一个格子
  25. public void drop(){
  26. for(int i=0; i<cells.length;i++){
  27. cells[i].row++;
  28. }
  29. }
  30. //方块左移一个格子
  31. public void moveLeft(){
  32. for(int i=0; i<cells.length;i++){
  33. cells[i].col--;
  34. }
  35. }
  36. //方块右移一个格子
  37. public void moveRight(){
  38. for(int i=0; i<cells.length;i++){
  39. cells[i].col++;
  40. }
  41. }
  42. }

T测试类

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

J类

  1. package day02;
  2. public class J {
  3. Cell[] cells;
  4. //构造方法用于T类的初始化
  5. public J(int row, int col){
  6. cells = new Cell[4]; //顺时针初始化
  7. cells[0] = new Cell(row, col);
  8. cells[1] = new Cell(row, col+1);
  9. cells[2] = new Cell(row, col+2);
  10. cells[3] = new Cell(row+1, col+2);
  11. }
  12. public J(){
  13. this(0, 0);
  14. }
  15. //打印格子中四个格子所在的坐标
  16. public void print(){
  17. String str = "";
  18. for(int i=0; i<cells.length-1; i++){
  19. str += "(" + cells[i].getCellInfo() + "),";
  20. }
  21. str +="(" +cells[cells.length-1].getCellInfo() + ")";
  22. System.out.println(str);
  23. }
  24. //方块的下落一个格子
  25. public void drop(){
  26. for(int i=0; i<cells.length;i++){
  27. cells[i].row++;
  28. }
  29. }
  30. //方块左移一个格子
  31. public void moveLeft(){
  32. for(int i=0; i<cells.length;i++){
  33. cells[i].col--;
  34. }
  35. }
  36. //方块右移一个格子
  37. public void moveRight(){
  38. for(int i=0; i<cells.length;i++){
  39. cells[i].col++;
  40. }
  41. }
  42. }

J测试类

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

课后作业

笔记

  1. package day02;
  2. public class O {
  3. Cell[] cells;
  4. public O(int row, int col){
  5. cells = new Cell[4];
  6. cells[0] = new Cell(row, col);
  7. cells[1] = new Cell(row, col+1);
  8. cells[2] = new Cell(row+1, col);
  9. cells[3] = new Cell(row+1, col+1);
  10. }
  11. public O(){
  12. this(0,0);
  13. }
  14. public void print(){
  15. String str = "";
  16. for(int i=0; i<cells.length-1; i++){
  17. str += "(" + cells[i].getCellInfo() + "),";
  18. }
  19. str +="(" +cells[cells.length-1].getCellInfo() + ")";
  20. System.out.println(str);
  21. }
  22. //方块的下落一个格子
  23. public void drop(){
  24. for(int i=0; i<cells.length;i++){
  25. cells[i].row++;
  26. }
  27. }
  28. //方块左移一个格子
  29. public void moveLeft(){
  30. for(int i=0; i<cells.length;i++){
  31. cells[i].col--;
  32. }
  33. }
  34. //方块右移一个格子
  35. public void moveRight(){
  36. for(int i=0; i<cells.length;i++){
  37. cells[i].col++;
  38. }
  39. }
  40. }

TestT类

  1. package day02;
  2. public class TestO {
  3. public static void main(String[] args) {
  4. O o = new O(0, 1);
  5. //测试print方法
  6. System.out.println("原始坐标为:");
  7. o.print();
  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. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注