小练习

 

  

1 请看下列代码:

  1. public class SuperClass {
  2.     public static void main(String[] args) {
  3.         SuperClass sup=new SuperClass();
  4.         SubClass sub=new SubClass();
  5.         SuperClass supSub=new SubClass();
  6.     }
  7. }
  8. class SubClass extends SuperClass{
  9. }

根据上述代码,判断下列选项中,赋值语句错误的是:

A. sup=sub;

B. sub=sup;

C. supSub=sub;

D. supSub=sup;

参考答案

B 选项的赋值语句是错误的。

这是因为,一个类的对象可以向上造型的类型有:父类的类型或是其实现的接口类型,而B选项将sup(SuperClass类型)直接赋值给sub(SubClass类型),属于向下造型,向下造型要使用强制类型转换,即代码改为:sub=(SubClass)sup; 来实现向下造型。

2 射击游戏

现有一个射击游戏中的场景如图- 1所示。在图- 1中有飞机、蜜蜂,这两种飞行物都可以被炮弹击中。如果击中的是敌人(即飞机),则获得5分;如果击中的是蜜蜂则获得奖励。本案例的详细需求如下:

1. 奖励有两种类型,一种奖励为双倍火力、另一种奖励为手弹。

2. 空中现有3架飞机、两只蜜蜂,判断某一炮弹能否击中它们。

图-1

参考答案

实现题目所述的射击游戏,步骤如下:

1. 定义接口Enemy表示敌人。在该接口中定义getScore方法,用于实现分数的获取。代码如下:

 
  1. public class Shoot {
  2.     public static void main(String[] args) {
  3.         
  4.     }
  5. }
  6. // 敌人
  7. interface Enemy {
  8.     int getScore();
  9. }

2. 定义接口Award表示奖励。在该接口中定义常量表示奖励的类型、定义getType方法用于实现获取奖励的类型。代码如下:

 
  1. public class Shoot {
  2.     public static void main(String[] args) {
  3.     
  4.     }
  5. }
  6. // 敌人
  7. interface Enemy {
  8.     int getScore();
  9. }
  10. // 奖励
  11. interface Award {
  12.     int DOUBLE_FIRE = 2;// 双倍火力
  13.     int BOMB = 1; // 手雷
  14.     int getType();// 获取奖励类型 返回值是整数
  15. }

3. 定义抽象类FlyingObject表示飞行物。在类中定义属性 x 、y 表示飞行物所在的坐标;定义shootBy方法表示飞行物是否被击中。代码如下:

 
  1. public class Shoot {
  2.     public static void main(String[] args) {
  3.         
  4.     }
  5. }
  6. abstract class FlyingObject {
  7.     int x;
  8.     int y;
  9.     abstract boolean shootBy(int x, int y);
  10. }
  11. // 敌人
  12. interface Enemy {
  13.     int getScore();
  14. }
  15. // 奖励
  16. interface Award {
  17.     int DOUBLE_FIRE = 2;// 双倍火力
  18.     int BOMB = 1; // 手雷
  19.     int getType();// 获取奖励类型 返回值是整数
  20. }

4. 定义类Bee表示蜜蜂。蜜蜂属于飞行物,因此Bee类继承自FlyingObject,并重写FlyingObject类的shootBy方法,根据蜜蜂被击中的规则进行重写;蜜蜂被击中后,可以获得奖励,因此,Bee类实现Award接口,并实现getType方法,获得奖励。代码如下:

 
  1. public class Shoot {
  2.     public static void main(String[] args) {
  3.         
  4.     }
  5. }
  6. /** 小蜜蜂 是飞行物 也是奖励 */
  7. class Bee extends FlyingObject implements Award {
  8.     int r;
  9.     int type;
  10.     public Bee(int x, int y, int r) {
  11.         this.x = x;
  12.         this.y = y;
  13.         this.r = r;
  14.         type = Award.DOUBLE_FIRE;
  15.     }
  16.     public int getType() {
  17.         return type;
  18.     }
  19.     public boolean shootBy(int x, int y) {
  20.         int a = x - this.x;
  21.         int b = y - this.y;
  22.         return Math.sqrt(a * a + b * b) < r;
  23.     }
  24. }
  25. abstract class FlyingObject {
  26.     int x;
  27.     int y;
  28.     abstract boolean shootBy(int x, int y);
  29. }
  30. // 敌人
  31. interface Enemy {
  32.     int getScore();
  33. }
  34. // 奖励
  35. interface Award {
  36.     int DOUBLE_FIRE = 2;// 双倍火力
  37.     int BOMB = 1; // 手雷
  38.     int getType();// 获取奖励类型 返回值是整数的
  39. }

把蜜蜂所在的区域看出一个圆形,蜜蜂被击中的规则为:

  1.     int a = x - this.x;
  2.     int b = y - this.y;
  3. return Math.sqrt(a * a + b * b) < r;

上述代码中,x 、y表示炮弹的坐标位置,this.x 、this.y表示蜜蜂的坐标位置,r表示半径。如果表达式“ Math.sqrt(a * a + b * b) < r ”的结果返回true,则蜜蜂被炮弹击中。

5. 定义类Airplane表示飞机。飞机属于飞行物,因此Airplane类继承自FlyingObject,并重写FlyingObject类的shootBy方法,根据飞机被击中的规则进行重写;飞机被击中后,可以获得分数,因此,Airplane 类实现Enemy接口,并实现 getScore方法,用于获得分数。代码如下:

 
  1. public class Shoot {
  2.     public static void main(String[] args) {
  3.         
  4.     }
  5. }
  6. /** 飞机是飞行物也是敌人 */
  7. class Airplane extends FlyingObject implements Enemy {
  8.     int width;
  9.     int height;
  10.     public Airplane(int x, int y, int w, int h) {
  11.         this.x = x;
  12.         this.y = y;
  13.         width = w;
  14.         height = h;
  15.     }
  16.     public boolean shootBy(int x, int y) {
  17.         int dx = x - this.x;
  18.         int dy = y - this.y;
  19.         return (dx > 0 && dx < width) && (dy > 0 && dy < height);
  20.     }
  21.     @Override
  22.     public int getScore() {
  23.         return 5;
  24.     }
  25. }
  26. /** 小蜜蜂 是飞行物 也是奖励 */
  27. class Bee extends FlyingObject implements Award {
  28.     int r;
  29.     int type;
  30.     public Bee(int x, int y, int r) {
  31.         this.x = x;
  32.         this.y = y;
  33.         this.r = r;
  34.         type = Award.DOUBLE_FIRE;
  35.     }
  36.     public int getType() {
  37.         return type;
  38.     }
  39.     public boolean shootBy(int x, int y) {
  40.         int a = x - this.x;
  41.         int b = y - this.y;
  42.         return Math.sqrt(a * a + b * b) < r;
  43.     }
  44. }
  45. abstract class FlyingObject {
  46.     int x;
  47.     int y;
  48.     abstract boolean shootBy(int x, int y);
  49. }
  50. // 敌人
  51. interface Enemy {
  52.     int getScore();
  53. }
  54. // 奖励
  55. interface Award {
  56.     int DOUBLE_FIRE = 2;// 双倍火力
  57.     int BOMB = 1; // 手雷
  58.     int getType();// 获取奖励类型 返回值是整数的
  59. }

把飞机所在的区域看出一个矩形,飞机被击中的规则为:

 
  1. int dx = x - this.x;
  2. int dy = y - this.y;
  3. return (dx > 0 && dx < width) && (dy > 0 && dy < height);

上述代码中,x 、y表示炮弹的坐标位置,this.x 、this.y表示飞机的坐标位置,width、height表示矩形的宽和高。如果表达式“(dx > 0 && dx < width) && (dy > 0 && dy < height) ”的结果返回true,则飞机被炮弹击中。

6. 在main方法中,首先,构造长度为5的FlyingObject类型的数组,将3架飞机和两只蜜蜂作为数组的元素; 然后,设置炮弹的坐标位置;最后,循环判断数组中的飞行物是否被击中,如果被击中,则再判断是蜜蜂还是飞机,如果是飞机,则输出得分,如果是蜜蜂则输出奖励类型。代码如下:

 
  1. public class Shoot {
  2.     public static void main(String[] args) {
  3.         FlyingObject[] objects = new FlyingObject[5];
  4.         objects[0] = new Airplane(103, 68, 20, 20);
  5.         objects[1] = new Airplane(243, 102, 20, 20);
  6.         objects[2] = new Airplane(153, 166, 20, 20);
  7.         objects[3] = new Bee(85, 256, 20);
  8.         objects[4] = new Bee(256, 287, 20);
  9.         int x = 82;
  10.         int y = 253;
  11.         for (int i = 0; i < objects.length; i++) {
  12.             FlyingObject obj = objects[i];
  13.             if (obj.shootBy(x, y)) {
  14.                 if (obj instanceof Enemy) {
  15.                     Enemy enemy = (Enemy) obj;
  16.                     System.out.println("加分:" + enemy.getScore());
  17.                 }
  18.                 if (obj instanceof Award) {
  19.                     Award award = (Award) obj;
  20.                     System.out.println("奖励" + award.getType());
  21.                 }
  22.             }
  23.         }
  24.     }
  25. }
  26. /** 飞机是飞行物也是敌人 */
  27. class Airplane extends FlyingObject implements Enemy {
  28.     int width;
  29.     int height;
  30.     public Airplane(int x, int y, int w, int h) {
  31.         this.x = x;
  32.         this.y = y;
  33.         width = w;
  34.         height = h;
  35.     }
  36.     public boolean shootBy(int x, int y) {
  37.         int dx = x - this.x;
  38.         int dy = y - this.y;
  39.         return (dx > 0 && dx < width) && (dy > 0 && dy < height);
  40.     }
  41.     @Override
  42.     public int getScore() {
  43.         return 5;
  44.     }
  45. }
  46. /** 小蜜蜂 是飞行物 也是奖励 */
  47. class Bee extends FlyingObject implements Award {
  48.     int r;
  49.     int type;
  50.     public Bee(int x, int y, int r) {
  51.         this.x = x;
  52.         this.y = y;
  53.         this.r = r;
  54.         type = Award.DOUBLE_FIRE;
  55.     }
  56.     public int getType() {
  57.         return type;
  58.     }
  59.     public boolean shootBy(int x, int y) {
  60.         int a = x - this.x;
  61.         int b = y - this.y;
  62.         return Math.sqrt(a * a + b * b) < r;
  63.     }
  64. }
  65. abstract class FlyingObject {
  66.     int x;
  67.     int y;
  68.     abstract boolean shootBy(int x, int y);
  69. }
  70. // 敌人
  71. interface Enemy {
  72.     int getScore();
  73. }
  74. // 奖励
  75. interface Award {
  76.     int DOUBLE_FIRE = 2;// 双倍火力
  77.     int BOMB = 1; // 手雷
  78.     int getType();// 获取奖励类型 返回值是整数的
  79. }

本案例的完整代码如下所示:

 
  1. public class Shoot {
  2.     public static void main(String[] args) {
  3.         FlyingObject[] objects = new FlyingObject[5];
  4.         objects[0] = new Airplane(103, 68, 20, 20);
  5.         objects[1] = new Airplane(243, 102, 20, 20);
  6.         objects[2] = new Airplane(153, 166, 20, 20);
  7.         objects[3] = new Bee(85, 256, 20);
  8.         objects[4] = new Bee(256, 287, 20);
  9.         int x = 82;
  10.         int y = 253;
  11.         for (int i = 0; i < objects.length; i++) {
  12.             FlyingObject obj = objects[i];
  13.             if (obj.shootBy(x, y)) {
  14.                 if (obj instanceof Enemy) {
  15.                     Enemy enemy = (Enemy) obj;
  16.                     System.out.println("加分:" + enemy.getScore());
  17.                 }
  18.                 if (obj instanceof Award) {
  19.                     Award award = (Award) obj;
  20.                     System.out.println("奖励" + award.getType());
  21.                 }
  22.             }
  23.         }
  24.     }
  25. }
  26. /** 飞机是飞行物也是敌人 */
  27. class Airplane extends FlyingObject implements Enemy {
  28.     int width;
  29.     int height;
  30.     public Airplane(int x, int y, int w, int h) {
  31.         this.x = x;
  32.         this.y = y;
  33.         width = w;
  34.         height = h;
  35.     }
  36.     public boolean shootBy(int x, int y) {
  37.         int dx = x - this.x;
  38.         int dy = y - this.y;
  39.         return (dx > 0 && dx < width) && (dy > 0 && dy < height);
  40.     }
  41.     @Override
  42.     public int getScore() {
  43.         return 5;
  44.     }
  45. }
  46. /** 小蜜蜂 是飞行物 也是奖励 */
  47. class Bee extends FlyingObject implements Award {
  48.     int r;
  49.     int type;
  50.     public Bee(int x, int y, int r) {
  51.         this.x = x;
  52.         this.y = y;
  53.         this.r = r;
  54.         type = Award.DOUBLE_FIRE;
  55.     }
  56.     public int getType() {
  57.         return type;
  58.     }
  59.     public boolean shootBy(int x, int y) {
  60.         int a = x - this.x;
  61.         int b = y - this.y;
  62.         return Math.sqrt(a * a + b * b) < r;
  63.     }
  64. }
  65. abstract class FlyingObject {
  66.     int x;
  67.     int y;
  68.     abstract boolean shootBy(int x, int y);
  69. }
  70. // 敌人
  71. interface Enemy {
  72.     int getScore();
  73. }
  74. // 奖励
  75. interface Award {
  76.     int DOUBLE_FIRE = 2;// 双倍火力
  77.     int BOMB = 1; // 手雷
  78.     int getType();// 获取奖励类型 返回值是整数
  79. }
 
原文地址:https://www.cnblogs.com/xyk1987/p/8330907.html