1.构造方法:

构造函数、构造器、构建器-----------复用给成员变量初始化代码

  • 作用:给成员变量赋初始值
  • 与类同名,没有返回值类型(连void都没有)
  • 在创建(new)对象时被自动调用
  • 若自己不写构造方法,则编译器默认提供一个无参构造方法,
  • 若自己写了构造方法,则不再默认提供
  • 构造方法可以重载

2.this

  • 指代当前对象,哪个对象调用方法它指的就是哪个对象
  • 只能用在方法中,方法中访问成员变量之前默认有个this.

2.1this的用法

  • this.成员变量名--------------访问成员变量

    • 当成员变量和局部变量同名时,若想访问成员变量则this不能省略,其它一般省略
  • this.方法名()------------------调用方法(一般不用,了解)
  • this()----------------------------调用构造方法(一般不用,了解)
   class Student {//成员变量String name;int age;String address;//构造方法Student(String name,int age,String address){this.name = name;       //zs.name="zhangsan"this.age = age;         //zs.age=25this.address = address; //zs.address="LF"}//方法void study(){System.out.println(name+"在学习...");}void sayHi(){System.out.println("大家好,我叫"+name+",今年"+age+"岁了,家住"+address);}}//构造方法和this的演示public class ConsDemo {public static void main(String[] args) {//Student zs = new Student(); //编译错误,Student类没有无参构造Student zs = new Student("zhangsan",25,"LF");Student ls = new Student("lisi",26,"JMS");zs.sayHi();ls.sayHi();}}

3.NULL

表示空,没有指向任何对象。若引用的值为null,则该引用不能再进行任何点操作了,若操作则发生NullPointerException空指针异常。

4.潜艇游戏第二天

图解(潜艇y坐标):

图解(坐标图):

4.1给6个类添加构造方法,并测试

4.1.1Battleship类

package cn.tedu.submarine;import java.util.Objects;/*** 战舰*/
public class Battleship {/*** 宽*/private int width;/*** 高*/private int height;/*** x轴*/private int x;/*** y轴*/private int y;/*** 命*/private int life;/*** 移动的速度*/private int speed;public Battleship() {this.width = 66;this.height = 26;this.x = 270;this.y = 124;this.life = 5;this.speed = 20;}public Battleship(int width, int height, int x, int y, int life, int speed) {this.width = width;this.height = height;this.x = x;this.y = y;this.life = life;this.speed = speed;}/*** 移动战舰的方法*/public void move(){System.out.println("战舰移动");}/*** 发射炸弹的方法*/public void shootBomb(){}@Overridepublic String toString() {return "Battleship{" +"width=" + width +", height=" + height +", x=" + x +", y=" + y +", life=" + life +", speed=" + speed +'}';}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Battleship that = (Battleship) o;return width == that.width &&height == that.height &&x == that.x &&y == that.y &&life == that.life &&speed == that.speed;}@Overridepublic int hashCode() {return Objects.hash(width, height, x, y, life, speed);}public int getWidth() {return width;}public void setWidth(int width) {this.width = width;}public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public int getLife() {return life;}public void setLife(int life) {this.life = life;}public int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;}
}

4.1.2Bomb类

package cn.tedu.submarine;import java.util.Objects;/*** 炸弹*/
public class Bomb {/*** 宽*/private int width;/*** 高*/private int height;/*** x轴*/private int x;/*** y轴*/private int y;/*** 速度*/private int speed;public Bomb(int x , int y) {this.width = 9;this.height = 12;this.x = x;this.y = y;this.speed = 3;}public Bomb(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 void move(){System.out.println("炸弹移动");}@Overridepublic String toString() {return "Bomb{" +"width=" + width +", height=" + height +", x=" + x +", y=" + y +", speed=" + speed +'}';}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Bomb bomb = (Bomb) o;return width == bomb.width &&height == bomb.height &&x == bomb.x &&y == bomb.y &&speed == bomb.speed;}@Overridepublic int hashCode() {return Objects.hash(width, height, x, y, speed);}public int getWidth() {return width;}public void setWidth(int width) {this.width = width;}public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;}
}

4.1.3Mine类

package cn.tedu.submarine;import java.util.Objects;/*** 水雷*/
public class Mine {/*** 宽*/private int width;/*** 高*/private int height;/*** x轴*/private int x;/*** y轴*/private int y;/*** 速度*/private int speed;public Mine(int x , int y) {this.width = 11;this.height = 11;this.x = x;this.y = y;this.speed = 1;}public Mine(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 void move(){System.out.println("水雷移动");}@Overridepublic String toString() {return "Mine{" +"width=" + width +", height=" + height +", x=" + x +", y=" + y +", speed=" + speed +'}';}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Mine mine = (Mine) o;return width == mine.width &&height == mine.height &&x == mine.x &&y == mine.y &&speed == mine.speed;}@Overridepublic int hashCode() {return Objects.hash(width, height, x, y, speed);}public int getWidth() {return width;}public void setWidth(int width) {this.width = width;}public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;}
}

4.1.4MineSubMarine类

package cn.tedu.submarine;import java.util.Objects;
import java.util.Random;/*** 水雷潜艇*/
public class MineSubmarine {/*** 宽*/private int width;/*** 高*/private int height;/*** x轴*/private int x;/*** y轴*/private int y;/*** 速度*/private int speed;public MineSubmarine() {this.width = 63;this.height = 19;Random rand = new Random();x = -width;y = rand.nextInt(479-height-150+1 ) +150;this.speed = rand.nextInt(3)+1;}public MineSubmarine(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 void move(){System.out.println("水雷潜艇移动");}@Overridepublic String toString() {return "MineSubmarine{" +"width=" + width +", height=" + height +", x=" + x +", y=" + y +", speed=" + speed +'}';}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;MineSubmarine that = (MineSubmarine) o;return width == that.width &&height == that.height &&x == that.x &&y == that.y &&speed == that.speed;}@Overridepublic int hashCode() {return Objects.hash(width, height, x, y, speed);}public int getWidth() {return width;}public void setWidth(int width) {this.width = width;}public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;}
}

4.1.5ObserverSubMarine类

package cn.tedu.submarine;import java.util.Objects;
import java.util.Random;/*** 侦察潜艇*/
public class ObserverSubmarine {/*** 宽*/private int width;/*** 高*/private int height;/*** x轴*/private int x;/*** y轴*/private int y;/*** 速度*/private int speed;public ObserverSubmarine() {this.width = 63;this.height = 19;Random rand = new Random();x = -width;y = rand.nextInt(479-height-150+1 ) +150;this.speed = rand.nextInt(3)+1;}public ObserverSubmarine(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 void move(){System.out.println("侦察潜艇移动");}@Overridepublic String toString() {return "ObserverSubmarine{" +"width=" + width +", height=" + height +", x=" + x +", y=" + y +", speed=" + speed +'}';}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;ObserverSubmarine that = (ObserverSubmarine) o;return width == that.width &&height == that.height &&x == that.x &&y == that.y &&speed == that.speed;}@Overridepublic int hashCode() {return Objects.hash(width, height, x, y, speed);}public int getWidth() {return width;}public void setWidth(int width) {this.width = width;}public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;}
}

4.1.6TorpedoSubmarine类

package cn.tedu.submarine;import java.util.Objects;
import java.util.Random;/*** 鱼类潜艇*/
public class TorpedoSubmarine {/*** 宽*/private int width;/*** 高*/private int height;/*** x轴*/private int x;/*** y轴*/private int y;/*** 速度*/private int speed;public TorpedoSubmarine() {this.width = 64;this.height = 20;Random rand = new Random();x = -width;y = rand.nextInt(479-height-150+1 ) +150;this.speed = rand.nextInt(3)+1;}public TorpedoSubmarine(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 void move(){System.out.println("鱼类潜艇移动");}@Overridepublic String toString() {return "TorpedoSubmarine{" +"width=" + width +", height=" + height +", x=" + x +", y=" + y +", speed=" + speed +'}';}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;TorpedoSubmarine that = (TorpedoSubmarine) o;return width == that.width &&height == that.height &&x == that.x &&y == that.y &&speed == that.speed;}@Overridepublic int hashCode() {return Objects.hash(width, height, x, y, speed);}public int getWidth() {return width;}public void setWidth(int width) {this.width = width;}public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;}
}

4.1.7World(测试类)

package cn.tedu.submarine;/*** 整个游戏世界*/
public class World {public static void main(String[] args) {Battleship s = new Battleship();ObserverSubmarine os1 = new ObserverSubmarine();ObserverSubmarine os2 = new ObserverSubmarine();ObserverSubmarine os3 = new ObserverSubmarine();ObserverSubmarine os4 = new ObserverSubmarine();TorpedoSubmarine ts1 = new TorpedoSubmarine();TorpedoSubmarine ts2 = new TorpedoSubmarine();MineSubmarine ms1 = new MineSubmarine();MineSubmarine ms2 = new MineSubmarine();Mine m1 = new Mine(ms1.getX(),ms1.getY());Mine m2 = new Mine(ms2.getX(),ms2.getY());Bomb b1 = new Bomb(s.getX(),s.getY());Bomb b2 = new Bomb(s.getX(),s.getY());System.out.println(s.toString());System.out.println(os1.toString());System.out.println(os2.toString());System.out.println(os3.toString());System.out.println(os4.toString());System.out.println(ts1.toString());System.out.println(ts2.toString());System.out.println(ms1.toString());System.out.println(ms2.toString());System.out.println(m1.toString());System.out.println(m2.toString());System.out.println(b1.toString());System.out.println(b2.toString());}
}

5.补充

成员变量和局部变量是可以同名的,但是使用的时候默认采取的是就近原则

5.1内存管理:由JVM管理

  • 堆:new出来的对象(包括成员变量)
  • 栈:局部变量(包括方法的参数)
  • 方法区:------------周五再讨论

图解:

基本类型变量中装的是具体的数,引用类型变量中装的是堆中对象的地址

5.2明日单词

   1)reference:引用2)extends:继承3)super:超级4)Sea:海洋5)object:对象

JSD-2204-面向对象-潜艇游戏--Day08相关推荐

  1. Java面向对象学习练习———潜艇游戏(飞机游戏)

    潜艇游戏需求: 所参与的角色: 战舰.深水炸弹.侦察潜艇.鱼雷潜艇.水雷潜艇.水雷 角色间的关系: 战舰发射深水炸弹 深水炸弹可以打潜艇(侦察潜艇.鱼雷潜艇.水雷潜艇),若打中: 潜艇消失.深水炸弹消 ...

  2. JSD-2204-面向对象-潜艇游戏--Day07

    1.对象or类 1.1什么是类?什么是对象? 现实生活是由很多很多对象组成的,基于对象抽出了类 对象:软件中真实存在的单个个体/东西 类:类型/类别,代表一类个体 类是对象的模板,对象是类的具体的实例 ...

  3. Silverlight C# 游戏开发:面向对象在游戏中的实例(一)

    本系列所有代码都是使用Microsoft Visual Studio 2008开发,为基于Silverlight的游戏开发技术,如果您看完之后觉得不错,回复顶一下,万分感激:) 今天,我将带来一个非常 ...

  4. 潜艇游戏-第13届蓝桥杯Scratch选拔赛真题精选

    [导读]:超平老师计划推出Scratch蓝桥杯真题解析100讲,这是超平老师解读Scratch蓝桥真题系列的第71讲. 蓝桥杯选拔赛每一届都要举行4~5次,和省赛.国赛相比,题目要简单不少,再加上篇幅 ...

  5. 【潜艇游戏开发】最全的源代码

    ## 潜艇游戏第一天: 1. 创建了6个类,创建World类并测试 ## 潜艇游戏第二天: 1. 给6个类设计构造方法,并测试 ## 潜艇游戏第三天: 1. 设计侦察潜艇数组.鱼雷潜艇数组.水雷潜艇数 ...

  6. JSD-2204-面向对象-接口-潜艇游戏--Day14

    1.接口 - 是一种数据类型(引用数据类型)     - 由interface定义    - 只能包含常量和抽象方法    - 接口不能被实例化    - 接口是需要被实现/继承的,实现类/派生类:必 ...

  7. JSD-2204-面向对象-方法重写-潜艇游戏--Day10

    1. 向上造型: 超类型的引用指向派生类的对象 能点出来什么,看引用的类型-------------这是规定,记住就可以了 package ooday04;//向上造型的演示public class ...

  8. java面向对象跑马游戏_面向“对象”和“过程”

    在学习软工视频的时候总是想不明白,面向对象和面向过程,这两个东西字面上肯定能分得清,这个是没有问题的,但是具体到怎样算是面向对象,怎样又是面向过程?这样一问,大概模糊的想法还是有的,毕竟是模糊的. 面 ...

  9. 如何在C加加的面向对象写游戏 我的世界

    可以使用 C++ 的面向对象特性来开发 "我的世界" 游戏. 首先,可以创建一个游戏世界类,这个类中包含了游戏中所有物品.实体.地形和其他元素的信息.然后,可以创建一个玩家类,该类 ...

最新文章

  1. 使用定制的NSDictionary的方法,对NSArray进行排序(附:数组排序两种常见方法)
  2. 基于SSM实现小区物业管理系
  3. 《算法导论》中动态规划求解钢条切割问题
  4. java同步转化成异步_Java 如何把异步调用模拟成同步调用
  5. 脱离标准文档流(1)---浮动
  6. Excel表格数据生成sql插入语句
  7. win10中bochs仿真linux0.11环境快速搭建方法
  8. 声音特征提取方法:综述【线性声谱图(Line Spectrum)、对数梅尔谱(Log-mel)、梅尔频率倒谱系数(MFCCs)】
  9. 如何判断Activity是否在前台显示
  10. 【Matlab三维路径规划】A_star算法机器人栅格地图三维路径规划【含源码 190期】
  11. cad图片怎么转换成pdf格式
  12. ecm、ppp、ndis 拨号
  13. 手把手教你使用Typecho搭建自己的个人博客
  14. 硬件速攻-AS608指纹识别模块
  15. 【SpringCloud】微服务笔记
  16. 【linux】 下根目录,家目录区别
  17. 这可能是全网最全的数据仓库建设方法论!
  18. html鼠标悬停图片释义文字消失,鼠标悬停,在图片上显示文字~
  19. 性能工具之Jmeter脚本python启动
  20. Python打不开、Python 安装时发生严重错误 “A newer version of the Python launcher is already installed“

热门文章

  1. 区块链开发公司浅析区块链的核心价值
  2. JQuery中的九大选择器及其应用(中)
  3. cermany rust_.CLUB团队在德国小镇Rust参加WHD.Global
  4. ss fping.php,Cacti进阶应用篇
  5. 戴尔t430服务器性能,不止双路E5 这款塔式服务器有点强
  6. 基础算法-Xgboost
  7. .html 打开方式注册表,注册表怎么打开
  8. c语言中eof的作用,深入解读C语言中的符号常量EOF
  9. 鸿蒙os骁龙845,天玑820相当于骁龙什么处理器 对比骁龙845哪个好
  10. linux下下载优酷等的视频