zoukankan      html  css  js  c++  java
  • 类和面向对象

    1 关于方法的返回值和return语句,下面说法错误的是:

    A. return 语句用于终止当前方法的执行

    B. 如果方法的返回类型为void,则方法中不能出现return语句

    C. return 关键字还会停止方法的执行;如果方法的返回类型为 void,则可使用没有值的 return 语句来停止方法的执行

    D. 定义有返回值的方法,必须使用 return 关键字返回值,且 return 关键字的后面必须是与返回类型匹配的值

    参考答案

    B 选项的说法错误。

    这是因为,如果方法有返回值,则必须使用 return 关键字向调用方返回值;如果方法所定义的返回类型为 void,则不需要使用 return 关键字来返回数据,但是,可以使用 没有值的 return 语句来停止方法的执行。

    2 请描述类和对象的关系

    参考答案

    对象是一个客观存在的实体,是面向对象编程过程中分析与解决问题的出发点与基础。对象的实质就是内存中的一块数据存储区域,其数据结构由定义它的类来决定。

    类是用于构建对象的模板,对象通过类的实例化产生,一个类可以创建多个对象,每个对象拥有自己的数据和行为。

    3 请描述引用类型和基本类型的区别

    参考答案

    除8种基本类型之外,用类名(接口、数组)声明的变量称为引用类型变量,简称“引用”。引用的功能在于访问对象。

    基本类型变量本身就包含了其实例数据,而引用类型变量中存储的是某个对象在内存中的地址信息。当一个引用类型变量指向该类的对象时,就可以通过这个变量访问对象。

    4 为 Cell 类添加右移的方法

    本案例需要实现格子的右移功能,即需要为 Cell类定义右移的方法。该方法需要使用重载来分别实现两种功能:

    功能1:调用右移方法,不需要传入任何参数,则格子向右移动一列,如图-1上中间的图形所示;

    功能2:调用右移方法,并传入需要移动的列数,则格子将向右移动相应的列数,如图-1上右边的图形所示。

    图-1

    本案例首先需要打印出游戏所在的平面(宽10格,高20格),用“-”号表示平面上的每个单元;然后假设某格子的坐标为(15,3),即,行号为15,列号为3,需要使用“*”号打印显示该格子,如图-1中左图中的蓝色圈内所示。先调用不带参数的右移方法,则格子右移一列,如图-1上中间图形上蓝色圈内所示;然后调用带参数的右移方法使得格子向右移动 4 列,并重新打印效果,如图-1中右图上蓝色圈内所示。

    参考答案

    为实现格子右移,需要为Cell类定义 moveRight 方法。该方法不接收参数,则右移一列,代码如下所示:

    1.     public void moveRight() {
    2.         col++;
    3.     }

    该方法也可以接收一个 int 类型的参数,表示向右移动的列数,然后在该方法中,将格子的列数增加相应的数值。代码如下所示:

     
    1.     public void moveRight(int d) {
    2.         col += d;
    3.     }

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

    步骤一:为 Cell 类定义 moveRight 方法

    在Cell 类中,添加方法 moveRight,实现右移一列。代码如下所示:

     
    1. public class Cell {
    2.     int row;
    3.     int col;
    4. //右移一列
    5.     public void moveRight() {
    6.         col++;
    7.     }
    8. }

    步骤二:为 Cell 类定义重载的 moveRight 方法

    在Cell 类中,重载moveRight方法,定义带有参数的 moveRight 方法,实现右移多列的功能。代码如下所示:

     
    1. public class Cell {
    2.     int row;
    3.     int col;
    4. //右移一列
    5.     public void moveRight() {
    6.         col++;
    7.     }
    8. //重载的 moveRight方法:右移多列
    9.     public void moveRight(int d) {
    10.         col += d;
    11.     }
    12. }

    步骤三:测试 moveRight 方法

    在CellGame 类的main 方法中添加代码:先创建一个位置为(15,3)的格子,并打印显示;然后调用cell 对象的 moveRight方法,实现格子右移一列,并打印显示;再调用一次 moveRight 方法,实现格子右移多列,并打印显示。代码如下所示:

     
    1. public class CellGame{
    2.     public static void main(String[] args) {
    3.         System.out.println("----------绘制Cell----------");
    4.         Cell cell1 = new Cell(15,3);        
    5.         printCell(cell1);
    6.         System.out.println("----------右移 1 列----------");
    7.         cell1.moveRight();
    8.         printCell(cell1);
    9.         System.out.println("----------右移 4 列----------");
    10.         cell1.moveRight(4);
    11.         printCell(cell1);
    12.     }
    13.     public static void printCell(Cell cell) {
    14.         int totalRow = 20;
    15.         int totalCol = 10;
    16.         //打印场地
    17.         for (int row = 0; row < totalRow; row++) {
    18.             for (int col = 0; col < totalCol; col++) {
    19.                 if (cell.row == row && cell.col == col) {
    20.                     //打印指定的格子
    21.                     System.out.print("* ");
    22.                 } else {
    23.                     System.out.print("- ");
    24.                 }
    25.             }
    26.             System.out.println();
    27.         }
    28.     }
    29. }

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

     
    1. public class Cell {
    2.     int row;
    3.     int col;
    4. //右移一列
    5.     public void moveRight() {
    6.         col++;
    7.     }
    8. //重载的 moveRight方法:右移多列
    9.     public void moveRight(int d) {
    10.         col += d;
    11.     }
    12. //其他方法...
    13. }

    类CellGame的完整代码如下所示:

     
    1. import java.util.Scanner;
    2. public class CellGame{
    3.     public static void main(String[] args) {
    4.         System.out.println("----------绘制Cell----------");
    5.         Cell cell1 = new Cell(15,3);        
    6.         printCell(cell1);
    7.         System.out.println("----------右移 1 列----------");
    8.         cell1.moveRight();
    9.         printCell(cell1);
    10.         System.out.println("----------右移 4 列----------");
    11.         cell1.moveRight(4);
    12.         printCell(cell1);
    13.     }
    14.     public static void printCell(Cell cell) {
    15.         int totalRow = 20;
    16.         int totalCol = 10;
    17.         //打印场地
    18.         for (int row = 0; row < totalRow; row++) {
    19.             for (int col = 0; col < totalCol; col++) {
    20.                 if (cell.row == row && cell.col == col) {
    21.                     //打印指定的格子
    22.                     System.out.print("* ");
    23.                 } else {
    24.                     System.out.print("- ");
    25.                 }
    26.             }
    27.             System.out.println();
    28.         }
    29.     }
    30. }

    5 完成CellGame(提高题,选作)

    本案例要求完成 CellGame,用户可以在控制台上操作格子的下落、左移和右移。

    游戏刚开始,将在界面上显示一个格子,界面效果如图-2上左图中的蓝色圈内所示,用户可以在控制台选择输入各种操作:1表示下落一行,2表示左移一列,3表示右移一列,0表示退出。如果用户录入1,则格子下落一行,并重新打印显示,界面效果如图-2上右图中的蓝色圈内所示:

    图-2

    如果用户录入2,则格子左移一列,并重新打印显示,界面效果如图-3上左图中蓝色圈内所示;如果用户录入3,则格子右移一列,并重新打印显示,界面如图-3上右图中蓝色圈内所示:

    图-3

    如果用户录入0,则游戏结束,界面效果如图-4所示:

    图-4

    参考答案

    前面的案例中,已经实现了格子的下落、左移和右移的功能,此案例中,只需要实现游戏的主要规则即可。

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

    步骤一:定义类及 main 方法

    首先定义一个名为 CellGame的类,并在类中定义Java 应用程序的入口方法main ,代码如下所示:

     
    1. public class CellGame {
    2.     public static void main(String[] args) {
    3.     }
    4. }

    步骤二:初始化 Cell并打印

    在 main 方法中添加代码,创建一个位置为(3,3)的格子,并打印显示。代码如下所示:

    1. public class CellGame {
    2.     public static void main(String[] args) {
    3.         //创建格子并打印
    4.         Cell cell1 = new Cell(3,3);        
    5.         printCell(cell1);
    6.     }
    7. }

    步骤三:输入

    在main方法中,实例化Scanner类,并调用Scanner类的nextInt()方法接收用户从控制台输入的表示操作的数值。代码如下所示:

     
    1. import java.util.Scanner;
    2. public class CellGame {
    3.     public static void main(String[] args) {
    4.         //创建格子并打印
    5.         Cell cell1 = new Cell(3,3);        
    6.         printCell(cell1);
    7.         //游戏控制
    8.         System.out.println("1 —— 下落,2——向左,3——向右,0 —— 退出");
    9.         Scanner sc = new Scanner(System.in);
    10.         int cmd = sc.nextInt();
    11.     }
    12. }

    注意:此步骤中,需要导入java.util包下的Scanner类。

    步骤四:判断输入

    判断用户的输入,以实现格子的下落、左移或者右移。直到用户录入0,则表示游戏结束。代码如下所示:

     
    1. import java.util.Scanner;
    2. public class CellGame {
    3.     public static void main(String[] args) {
    4.         //创建格子并打印
    5.         Cell cell1 = new Cell(3,3);        
    6.         printCell(cell1);
    7.         //游戏控制
    8.         System.out.println("1 —— 下落,2——向左,3——向右,0 —— 退出");
    9.         Scanner sc = new Scanner(System.in);
    10.         int cmd = sc.nextInt();
    11.         while (cmd != 0) {
    12.             switch(cmd) {                
    13.                 case 1:
    14.                     cell1.drop();
    15.                     break;
    16.                 case 2:
    17.                     cell1.moveLeft();
    18.                     break;
    19.                 case 3:
    20.                     cell1.moveRight();
    21.                     break;
    22.             }            
    23.             printCell(cell1);
    24.             System.out.println("1 —— 下落,2——向左,3——向右,0 —— 退出");
    25.             cmd = sc.nextInt();
    26.         }
    27.         System.out.println("游戏结束");
    28.         sc.close();
    29.     }
    30. }

    注意:使用完毕后,关闭 Scanner 对象。

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

     
    1. public class Cell {
    2.     int row;
    3.     int col;
    4.     public Cell(int row, int col) {
    5.         this.row = row;
    6.         this.col = col;
    7.     }
    8.     public Cell() {
    9.         this(0, 0);
    10.     }
    11.     
    12.     public Cell(Cell cell) {
    13.         this(cell.row, cell.col);
    14.     }
    15.     public void drop() {
    16.         row++;
    17.     }
    18.     
    19.     public void moveLeft(int d) {
    20.         col -= d;
    21.     }
    22.     
    23.     public String getCellInfo() {
    24.         return row + "," + col;
    25.     }
    26.     
    27.     public void moveRight(int d) {
    28.         col += d;
    29.     }
    30.     public void drop(int d) {
    31.         row += d;
    32.     }
    33.     public void moveLeft() {
    34.         col--;
    35.     }
    36.     public void moveRight() {
    37.         col++;
    38.     }
    39. }
     

    类CellGame的完整代码如下所示:

     
    1. import java.util.Scanner;
    2. public class CellGame {
    3.     public static void main(String[] args) {
    4.         //创建格子并打印
    5.         Cell cell1 = new Cell(3,3);        
    6.         printCell(cell1);
    7.         
    8.         //游戏控制
    9.         System.out.println("1 —— 下落,2——向左,3——向右,0 —— 退出");
    10.         Scanner sc = new Scanner(System.in);
    11.         int cmd = sc.nextInt();
    12.         while (cmd != 0) {
    13.             switch(cmd) {                
    14.                 case 1:
    15.                     cell1.drop();
    16.                     break;
    17.                 case 2:
    18.                     cell1.moveLeft();
    19.                     break;
    20.                 case 3:
    21.                     cell1.moveRight();
    22.                     break;
    23.             }            
    24.             printCell(cell1);
    25.             System.out.println("1 —— 下落,2——向左,3——向右,0 —— 退出");
    26.             cmd = sc.nextInt();
    27.         }
    28.         System.out.println("游戏结束");
    29.         sc.close();
    30.     }
    31.     public static void printCell(Cell cell) {
    32.         int totalRow = 20;
    33.         int totalCol = 10;
    34.         //打印格子的位置
    35.         System.out.println("Cell的位置为:(" + cell.getCellInfo() + ")");
    36.         for (int row = 0; row < totalRow; row++) {
    37.             for (int col = 0; col < totalCol; col++) {
    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. }
     
  • 相关阅读:
    LeetCode Missing Number (简单题)
    LeetCode Valid Anagram (简单题)
    LeetCode Single Number III (xor)
    LeetCode Best Time to Buy and Sell Stock II (简单题)
    LeetCode Move Zeroes (简单题)
    LeetCode Add Digits (规律题)
    DependencyProperty深入浅出
    SQL Server存储机制二
    WPF自定义RoutedEvent事件示例代码
    ViewModel命令ICommand对象定义
  • 原文地址:https://www.cnblogs.com/xyk1987/p/8329795.html
Copyright © 2011-2022 走看看