[关闭]
@Hubertoo 2017-07-17T05:10:05.000000Z 字数 3267 阅读 781

OOP.Unit01

达内


知识框架

对象和类

面向对象程序设计

  1. public class Emp{
  2. String name;
  3. int age;
  4. char gender;
  5. double salary;
  6. public void sayHi(){
  7. sysout("I am" + name);
  8. }
  9. public void print(){
  10. ...
  11. }
  12. }

定义一个类

创建和使用对象

经典案例

打印员工信息表

类Emp

  1. package day02;
  2. public class Emp {
  3. String name;
  4. int age;
  5. char gender;
  6. double salary;
  7. public void printInfo(){
  8. System.out.println("------------------------");
  9. System.out.println("姓名:" + name);
  10. System.out.println("年龄:" + age);
  11. System.out.println("性别:" + gender);
  12. System.out.println("薪水:" + salary);
  13. }
  14. }

类EmpManager

  1. package day02;
  2. public class EmpManager {
  3. public static void main(String[] args) {
  4. Emp emp = new Emp();
  5. emp.name = "白发魔女";
  6. emp.age = 21;
  7. emp.salary = 8900;
  8. emp.printInfo();
  9. Emp emp2 = new Emp();
  10. emp2.printInfo();
  11. }
  12. }

定义Tetris项目中的Cell类,并实现下落,左移和获取格子信息的功能

  1. package day02;
  2. public class Cell {
  3. int row;
  4. int col;
  5. //下落一行
  6. public void drop(){
  7. row++;
  8. }
  9. //左移d列
  10. public void moveLeft(int d){
  11. col -= d;
  12. }
  13. //获取格子信息
  14. public String getCellInfo(){
  15. return row + ",,," + col;
  16. }
  17. }

创建Cell对象,添加打印的方法

  1. package day02;
  2. public class CellGame {
  3. public static void main(String[] args) {
  4. //创建Cell对象,并打印
  5. Cell cell = new Cell();
  6. cell.row = 15;
  7. cell.col = 6;
  8. printCell(cell);
  9. //调用drop方法并打印
  10. cell.drop();
  11. printCell(cell);
  12. }
  13. public static void printCell(Cell cell){
  14. int totalRow = 20;
  15. int totalCol = 10;
  16. //打印场地
  17. System.out.println("---------------- 分界线 ---------------");
  18. System.out.println("Cell的位置是:" + cell.getCellInfo());
  19. for(int row = 0; row<totalRow; row++){
  20. for(int col = 0; col<totalCol; col++){
  21. //满足条件打印*
  22. if(cell.row == row && cell.col == col){
  23. System.out.print("* ");
  24. }else{
  25. System.out.print("- ");
  26. }
  27. }
  28. System.out.println();
  29. }
  30. }
  31. }

课后作业

笔记

通过用户输入的数字(1-下落,2-向左,3-向右,0-退出),来实现格子的控制

Cell类

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

CellGamePlus类

  1. package day02;
  2. import java.util.Scanner;
  3. public class CellGamePlus {
  4. public static void main(String[] args) {
  5. Scanner scan = new Scanner(System.in);
  6. //初始化Cell的位置
  7. Cell cell = new Cell();
  8. cell.row = 2;
  9. cell.col = 3;
  10. printCell(cell);
  11. while(true){
  12. System.out.println("请输入你的操作:(1-下落,2-向左,3-向右,0-退出)");
  13. int num = scan.nextInt();
  14. if(num == 0){
  15. System.out.println("游戏结束");
  16. break;
  17. }else if(num == 1){
  18. cell.drop();
  19. printCell(cell);
  20. }else if(num == 2){
  21. cell.moveLeft();
  22. printCell(cell);
  23. }else if(num == 3){
  24. cell.moveRight();
  25. printCell(cell);
  26. }
  27. }
  28. }
  29. public static void printCell(Cell cell){
  30. int totalRow = 20;
  31. int totalCol = 10;
  32. //打印场地
  33. System.out.println("---------------- 分界线 ---------------");
  34. System.out.println("Cell的位置是:" + cell.getCellInfo());
  35. for(int row = 0; row<totalRow; row++){
  36. for(int col = 0; col<totalCol; col++){
  37. //满足条件打印*
  38. if(cell.row == row && cell.col == col){
  39. System.out.print("* ");
  40. }else{
  41. System.out.print("- ");
  42. }
  43. }
  44. System.out.println();
  45. }
  46. }
  47. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注