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=25
this.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(){
}
@Override
public String toString() {
return "Battleship{" +
"width=" + width +
", height=" + height +
", x=" + x +
", y=" + y +
", life=" + life +
", speed=" + speed +
'}';
}
@Override
public 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;
}
@Override
public 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("炸弹移动");
}
@Override
public String toString() {
return "Bomb{" +
"width=" + width +
", height=" + height +
", x=" + x +
", y=" + y +
", speed=" + speed +
'}';
}
@Override
public 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;
}
@Override
public 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("水雷移动");
}
@Override
public String toString() {
return "Mine{" +
"width=" + width +
", height=" + height +
", x=" + x +
", y=" + y +
", speed=" + speed +
'}';
}
@Override
public 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;
}
@Override
public 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("水雷潜艇移动");
}
@Override
public String toString() {
return "MineSubmarine{" +
"width=" + width +
", height=" + height +
", x=" + x +
", y=" + y +
", speed=" + speed +
'}';
}
@Override
public 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;
}
@Override
public 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("侦察潜艇移动");
}
@Override
public String toString() {
return "ObserverSubmarine{" +
"width=" + width +
", height=" + height +
", x=" + x +
", y=" + y +
", speed=" + speed +
'}';
}
@Override
public 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;
}
@Override
public 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("鱼类潜艇移动");
}
@Override
public String toString() {
return "TorpedoSubmarine{" +
"width=" + width +
", height=" + height +
", x=" + x +
", y=" + y +
", speed=" + speed +
'}';
}
@Override
public 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;
}
@Override
public 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:对象