JSD-2204-面向对象-潜艇游戏--Day09

本文介绍Java中的引用类型数组、继承概念及其应用。通过潜艇游戏的实例,详细讲解如何创建Battleship、Bomb、Mine、MineSubMarine、ObserverSubMarine、TorpedoSubmarine等类,并实现SeaObject超类。文章还强调了super关键字在构造方法中的使用,并预告了明日的学习内容。

1. 引用类型数组

  • 给引用类型数组的元素赋值时,必须得new个对象
  • 若想访问对象的数据/调用方法,需要通过数组元素去打点
   1)Student[] stus = new Student[3];
     stus[0] = new Student("zhangsan",25,"LF");
     stus[1] = new Student("lisi",24,"JMS");
     stus[2] = new Student("wangwu",27,"SD");
     System.out.println(stus[0].name); //输出第1个学生的名字
     stus[1].age = 27; //修改第2个学生的年龄为27
     stus[2].sayHi(); //第3个学生跟大家问好
     for(int i=0;i<stus.length;i++){ //遍历所有学生
         System.out.println(stus[i].name); //输出每个学生的名字
         stus[i].sayHi(); //每个学生跟大家问好
     }
       
   2)Student[] stus = new Student[]{
       new Student("zhangsan",25,"LF"),
       new Student("lisi",24,"JMS"),
       new Student("wangwu",27,"SD")
     };

2. 继承:

  • 作用:代码复用
  • 通过extends来实现继承
  • 超类/父类:共有的属性和行为
  • 派生类/子类:特有的属性和行为
  • 派生类可以访问:派生类的+超类的,而超类不能访问派生类的
  • 一个超类可以有多个派生类,但一个派生类只能继承一个超类-----单一继承
  • 具有传递性
  • java规定:构造派生类之前必须先构造超类
    • 在派生类构造方法中若没有调用超类构造方法,则默认super()调超类无参构造方法
    • 在派生类构造方法中若自己调用了超类构造方法,则不再默认提供
      • 注意:super()调用超类构造方法,必须位于派生类构造方法的第一行

3. super:指代当前对象的超类对象

super的用法:

  • super.成员变量名------------------------访问超类的成员变量(一般不用,了解)
  • super.方法名()----------------------------调用超类的方法------------------明天下午讲
  • super()--------------------------------------调用超类的构造方法
   public class SuperDemo {
       public static void main(String[] args) {
           Boo o = new Boo();
       }
   }
   
   class Aoo{
       Aoo(){
           System.out.println("超类构造...");
       }
   }
   class Boo extends Aoo{
       Boo(){
           //super(); //默认的,写不写都在
           System.out.println("派生类构造...");
       }
   }
   
   class Coo{
       Coo(int a){
       }
   }
   class Doo extends Coo{
       Doo(){
           super(5); //调用超类的有参构造
       }
       /*
       //如下代码为默认的
       Doo(){
           super(); //超类若没有无参构造,则编译错误
       }
        */
   }

4.潜艇游戏第三天

  1. 设计侦察潜艇数组、鱼雷潜艇数组、水雷潜艇数组、水雷数组、炸弹数组,并测试
  2. 设计SeaObject超类,设计6个类继承超类
  3.  在超类中设计两个构造方法,6个类分别调用

4.1代码展示

4.1.1Battleship类

package cn.tedu.submarine;

import java.util.Objects;

/**
 * 战舰
 */
public class Battleship extends SeaObject{
    /**
     * 命
     */
    private int life;


    public Battleship() {
        super(66,26,270,124,5);
        this.life = 5;
    }


    @Override
    public void move() {
        System.out.println("战舰移动");
    }

    /**
     * 发射炸弹的方法
     */
    public void shootBomb(){

    }


}

4.1.2Bomb类

package cn.tedu.submarine;

import java.util.Objects;

/**
 * 炸弹
 */
public class Bomb extends SeaObject{

    public Bomb(int x , int y) {
        super(9,12,x,y,3);
    }


    /**
     * 炸弹移动的方法
     */
    @Override
    public void move(){
        System.out.println("炸弹移动");
    }

}

4.1.3Mine类

package cn.tedu.submarine;

import java.util.Objects;

/**
 * 水雷
 */
public class Mine extends SeaObject{

    public Mine(int x , int y) {
        super(11,11,x,y,1);
    }


    /**
     * 水雷移动的方法
     */
    @Override
    public void move(){
        System.out.println("水雷移动");
    }

}

4.1.4MineSubMarine类

package cn.tedu.submarine;

import java.util.Objects;
import java.util.Random;

/**
 * 水雷潜艇
 */
public class MineSubmarine extends SeaObject{


    public MineSubmarine() {
        super(63,19);
    }



    /**
     * 水雷潜艇移动的方法
     */
    public void move(){
        System.out.println("水雷潜艇移动");
    }
}

4.1.5ObserverSubMarine类

package cn.tedu.submarine;

import java.util.Objects;
import java.util.Random;

/**
 * 侦察潜艇
 */
public class ObserverSubmarine extends SeaObject{


    public ObserverSubmarine() {
        super(63,19);
    }



    @Override
    public void move() {
        System.out.println("侦察潜艇移动");
    }
}

4.1.6TorpedoSubmarine类

package cn.tedu.submarine;

import java.util.Objects;
import java.util.Random;

/**
 * 鱼类潜艇
 */
public class TorpedoSubmarine extends SeaObject{


    public TorpedoSubmarine() {
        super(64,20);
    }


    /**
     * 鱼类潜艇移动的方法
     */
    @Override
    public void move(){
        System.out.println("鱼类潜艇移动");
    }

}

4.1.7SeaObject

package cn.tedu.submarine;

import java.util.Random;

/**
 * 海洋对象
 */
public abstract class SeaObject {
    /**
     * 宽
     */
    protected int width;
    /**
     * 高
     */
    protected int height;
    /**
     * x轴
     */
    protected int x;
    /**
     * y轴
     */
    protected int y;
    /**
     * 速度
     */
    protected int speed;


    public SeaObject(int width,int height,int x , int y,int speed){
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }

    public SeaObject(int width,int height){
        this.width = width;
        this.height = height;
        Random rand = new Random();
        x = -width;
        y = rand.nextInt(479-height-150+1 ) +150;
        this.speed = rand.nextInt(3)+1;
    }

    public abstract void move();
}

4.1.8World(测试类)

package cn.tedu.submarine;

/**
 * 整个游戏世界
 */
public class World {
    public static void main(String[] args) {
        ObserverSubmarine[] oses = new ObserverSubmarine[3];
        oses[0] = new ObserverSubmarine();
        oses[1] = new ObserverSubmarine();
        oses[2] = new ObserverSubmarine();
        for (int i = 0; i < oses.length; i++) {
        //输出每个侦察挺的x和y坐标                                  
        System.out.println(oses[i].x+","+oses[i].y+","+oses[i].speed);
            oses[i].move();
        }
        Bomb[] bs = new Bomb[3];
        bs[0] = new Bomb(100,522);
        bs[1] = new Bomb(145,243);
        bs[2] = new Bomb(113,443);
        for (int i = 0; i < bs.length; i++) {
            System.out.println(bs[i].x+","+bs[i].y+","+bs[i].speed);
            bs[i].move();
        }

        TorpedoSubmarine[] tses = new TorpedoSubmarine[3];
        tses[0] = new TorpedoSubmarine();
        tses[1] = new TorpedoSubmarine();
        tses[2] = new TorpedoSubmarine();
        for (int i = 0; i < tses.length; i++) {
            System.out.println(tses[i].x+","+tses[i].y+","+tses[i].speed);
            tses[i].move();
        }
        MineSubmarine[] mses = new MineSubmarine[3];
        mses[0] = new MineSubmarine();
        mses[1] = new MineSubmarine();
        mses[2] = new MineSubmarine();
        for (int i = 0; i < mses.length; i++) {
            System.out.println(mses[i].x+","+mses[i].y+","+mses[i].speed);
            mses[i].move();
        }

        Mine[] ms = new Mine[2];
        ms[0] = new Mine(241,22);
        ms[1] = new Mine(341,112);
        for (int i = 0; i < ms.length; i++) {
            System.out.println(ms[i].x+","+ms[i].y+","+ms[i].speed);
            ms[i].move();
        }

    }
}

5.补充

5.1明日单词

   1)up:向上
   2)load:加载
   3)animal:动物
   4)tiger:老虎
   5)override:重写

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值