类名 HeroConstruction
类代码
public class HeroConstruction {
String name;
float Hp;
float armor;
int moveSpeed;
// 打印内存中的虚拟地址
public void showAddressInMemory() {
System.out.println("打印this看到的虚拟地址:" + this);
}
public HeroConstruction(String heroName, float heroHp, float heroArmor, int heroMoveSpeed) {
name = heroName;
Hp = heroHp;
armor = heroArmor;
moveSpeed = heroMoveSpeed;
System.out.println("英雄:" + name + " 血量:" + Hp + " 护甲:" + armor + " 移动速度:" + moveSpeed);
}
public HeroConstruction(String heroName, float heroHp) {
this.name = heroName;
this.Hp = heroHp;
System.out.println("英雄:" + this.name + " 血量:" + this.Hp);
}
// 复活
public void revive(HeroConstruction h) {
h = new HeroConstruction("提莫", 383);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
HeroConstruction hero1 = new HeroConstruction("李四", 38, 55, 25);
HeroConstruction hero2 = new HeroConstruction("张三", 38, 55, 25);
System.out.println("打印对象看到的虚拟地址:" + hero1);
hero1.showAddressInMemory();
System.out.println("打印对象看到的虚拟地址:" + hero2);
hero2.showAddressInMemory();
HeroConstruction teemo = new HeroConstruction("提莫", 383);
// 受到400伤害,挂了
teemo.Hp = teemo.Hp - 400;
teemo.revive(teemo);
System.out.println(teemo.Hp);
}
}
运行结果
英雄:李四 血量:38.0 护甲:55.0 移动速度:25
英雄:张三 血量:38.0 护甲:55.0 移动速度:25
打印对象看到的虚拟地址:test20200825.HeroConstruction@15db9742
打印this看到的虚拟地址:test20200825.HeroConstruction@15db9742
打印对象看到的虚拟地址:test20200825.HeroConstruction@6d06d69c
打印this看到的虚拟地址:test20200825.HeroConstruction@6d06d69c
英雄:提莫 血量:383.0
英雄:提莫 血量:383.0
-17.0