1 请看下列代码:
- public class SuperClass {
- public static void main(String[] args) {
- SuperClass sup=new SuperClass();
- SubClass sub=new SubClass();
- SuperClass supSub=new SubClass();
- }
- }
- class SubClass extends SuperClass{
- }
根据上述代码,判断下列选项中,赋值语句错误的是:
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方法,用于实现分数的获取。代码如下:
- public class Shoot {
- public static void main(String[] args) {
- }
- }
- // 敌人
- interface Enemy {
- int getScore();
- }
2. 定义接口Award表示奖励。在该接口中定义常量表示奖励的类型、定义getType方法用于实现获取奖励的类型。代码如下:
- public class Shoot {
- public static void main(String[] args) {
- }
- }
- // 敌人
- interface Enemy {
- int getScore();
- }
- // 奖励
- interface Award {
- int DOUBLE_FIRE = 2;// 双倍火力
- int BOMB = 1; // 手雷
- int getType();// 获取奖励类型 返回值是整数
- }
3. 定义抽象类FlyingObject表示飞行物。在类中定义属性 x 、y 表示飞行物所在的坐标;定义shootBy方法表示飞行物是否被击中。代码如下:
- public class Shoot {
- public static void main(String[] args) {
- }
- }
- abstract class FlyingObject {
- int x;
- int y;
- abstract boolean shootBy(int x, int y);
- }
- // 敌人
- interface Enemy {
- int getScore();
- }
- // 奖励
- interface Award {
- int DOUBLE_FIRE = 2;// 双倍火力
- int BOMB = 1; // 手雷
- int getType();// 获取奖励类型 返回值是整数
- }
4. 定义类Bee表示蜜蜂。蜜蜂属于飞行物,因此Bee类继承自FlyingObject,并重写FlyingObject类的shootBy方法,根据蜜蜂被击中的规则进行重写;蜜蜂被击中后,可以获得奖励,因此,Bee类实现Award接口,并实现getType方法,获得奖励。代码如下:
- public class Shoot {
- public static void main(String[] args) {
- }
- }
- /** 小蜜蜂 是飞行物 也是奖励 */
- class Bee extends FlyingObject implements Award {
- int r;
- int type;
- public Bee(int x, int y, int r) {
- this.x = x;
- this.y = y;
- this.r = r;
- type = Award.DOUBLE_FIRE;
- }
- public int getType() {
- return type;
- }
- public boolean shootBy(int x, int y) {
- int a = x - this.x;
- int b = y - this.y;
- return Math.sqrt(a * a + b * b) < r;
- }
- }
- abstract class FlyingObject {
- int x;
- int y;
- abstract boolean shootBy(int x, int y);
- }
- // 敌人
- interface Enemy {
- int getScore();
- }
- // 奖励
- interface Award {
- int DOUBLE_FIRE = 2;// 双倍火力
- int BOMB = 1; // 手雷
- int getType();// 获取奖励类型 返回值是整数的
- }
把蜜蜂所在的区域看出一个圆形,蜜蜂被击中的规则为:
- int a = x - this.x;
- int b = y - this.y;
- 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方法,用于获得分数。代码如下:
- public class Shoot {
- public static void main(String[] args) {
- }
- }
- /** 飞机是飞行物也是敌人 */
- class Airplane extends FlyingObject implements Enemy {
- int width;
- int height;
- public Airplane(int x, int y, int w, int h) {
- this.x = x;
- this.y = y;
- width = w;
- height = h;
- }
- public boolean shootBy(int x, int y) {
- int dx = x - this.x;
- int dy = y - this.y;
- return (dx > 0 && dx < width) && (dy > 0 && dy < height);
- }
- @Override
- public int getScore() {
- return 5;
- }
- }
- /** 小蜜蜂 是飞行物 也是奖励 */
- class Bee extends FlyingObject implements Award {
- int r;
- int type;
- public Bee(int x, int y, int r) {
- this.x = x;
- this.y = y;
- this.r = r;
- type = Award.DOUBLE_FIRE;
- }
- public int getType() {
- return type;
- }
- public boolean shootBy(int x, int y) {
- int a = x - this.x;
- int b = y - this.y;
- return Math.sqrt(a * a + b * b) < r;
- }
- }
- abstract class FlyingObject {
- int x;
- int y;
- abstract boolean shootBy(int x, int y);
- }
- // 敌人
- interface Enemy {
- int getScore();
- }
- // 奖励
- interface Award {
- int DOUBLE_FIRE = 2;// 双倍火力
- int BOMB = 1; // 手雷
- int getType();// 获取奖励类型 返回值是整数的
- }
把飞机所在的区域看出一个矩形,飞机被击中的规则为:
- int dx = x - this.x;
- int dy = y - this.y;
- 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架飞机和两只蜜蜂作为数组的元素; 然后,设置炮弹的坐标位置;最后,循环判断数组中的飞行物是否被击中,如果被击中,则再判断是蜜蜂还是飞机,如果是飞机,则输出得分,如果是蜜蜂则输出奖励类型。代码如下:
- public class Shoot {
- public static void main(String[] args) {
- FlyingObject[] objects = new FlyingObject[5];
- objects[0] = new Airplane(103, 68, 20, 20);
- objects[1] = new Airplane(243, 102, 20, 20);
- objects[2] = new Airplane(153, 166, 20, 20);
- objects[3] = new Bee(85, 256, 20);
- objects[4] = new Bee(256, 287, 20);
- int x = 82;
- int y = 253;
- for (int i = 0; i < objects.length; i++) {
- FlyingObject obj = objects[i];
- if (obj.shootBy(x, y)) {
- if (obj instanceof Enemy) {
- Enemy enemy = (Enemy) obj;
- System.out.println("加分:" + enemy.getScore());
- }
- if (obj instanceof Award) {
- Award award = (Award) obj;
- System.out.println("奖励" + award.getType());
- }
- }
- }
- }
- }
- /** 飞机是飞行物也是敌人 */
- class Airplane extends FlyingObject implements Enemy {
- int width;
- int height;
- public Airplane(int x, int y, int w, int h) {
- this.x = x;
- this.y = y;
- width = w;
- height = h;
- }
- public boolean shootBy(int x, int y) {
- int dx = x - this.x;
- int dy = y - this.y;
- return (dx > 0 && dx < width) && (dy > 0 && dy < height);
- }
- @Override
- public int getScore() {
- return 5;
- }
- }
- /** 小蜜蜂 是飞行物 也是奖励 */
- class Bee extends FlyingObject implements Award {
- int r;
- int type;
- public Bee(int x, int y, int r) {
- this.x = x;
- this.y = y;
- this.r = r;
- type = Award.DOUBLE_FIRE;
- }
- public int getType() {
- return type;
- }
- public boolean shootBy(int x, int y) {
- int a = x - this.x;
- int b = y - this.y;
- return Math.sqrt(a * a + b * b) < r;
- }
- }
- abstract class FlyingObject {
- int x;
- int y;
- abstract boolean shootBy(int x, int y);
- }
- // 敌人
- interface Enemy {
- int getScore();
- }
- // 奖励
- interface Award {
- int DOUBLE_FIRE = 2;// 双倍火力
- int BOMB = 1; // 手雷
- int getType();// 获取奖励类型 返回值是整数的
- }
本案例的完整代码如下所示:
- public class Shoot {
- public static void main(String[] args) {
- FlyingObject[] objects = new FlyingObject[5];
- objects[0] = new Airplane(103, 68, 20, 20);
- objects[1] = new Airplane(243, 102, 20, 20);
- objects[2] = new Airplane(153, 166, 20, 20);
- objects[3] = new Bee(85, 256, 20);
- objects[4] = new Bee(256, 287, 20);
- int x = 82;
- int y = 253;
- for (int i = 0; i < objects.length; i++) {
- FlyingObject obj = objects[i];
- if (obj.shootBy(x, y)) {
- if (obj instanceof Enemy) {
- Enemy enemy = (Enemy) obj;
- System.out.println("加分:" + enemy.getScore());
- }
- if (obj instanceof Award) {
- Award award = (Award) obj;
- System.out.println("奖励" + award.getType());
- }
- }
- }
- }
- }
- /** 飞机是飞行物也是敌人 */
- class Airplane extends FlyingObject implements Enemy {
- int width;
- int height;
- public Airplane(int x, int y, int w, int h) {
- this.x = x;
- this.y = y;
- width = w;
- height = h;
- }
- public boolean shootBy(int x, int y) {
- int dx = x - this.x;
- int dy = y - this.y;
- return (dx > 0 && dx < width) && (dy > 0 && dy < height);
- }
- @Override
- public int getScore() {
- return 5;
- }
- }
- /** 小蜜蜂 是飞行物 也是奖励 */
- class Bee extends FlyingObject implements Award {
- int r;
- int type;
- public Bee(int x, int y, int r) {
- this.x = x;
- this.y = y;
- this.r = r;
- type = Award.DOUBLE_FIRE;
- }
- public int getType() {
- return type;
- }
- public boolean shootBy(int x, int y) {
- int a = x - this.x;
- int b = y - this.y;
- return Math.sqrt(a * a + b * b) < r;
- }
- }
- abstract class FlyingObject {
- int x;
- int y;
- abstract boolean shootBy(int x, int y);
- }
- // 敌人
- interface Enemy {
- int getScore();
- }
- // 奖励
- interface Award {
- int DOUBLE_FIRE = 2;// 双倍火力
- int BOMB = 1; // 手雷
- int getType();// 获取奖励类型 返回值是整数
- }