所谓对象就是真实世界中的实体,对象与实体是一一对应的,也就是说现实世界中每一个实体都是一个对象,它是一种具体的概念。类是具备某些共同特征的实体的集合,它是一种抽象的概念,用程序设计的语言来说,类是一种抽象的数据类型,它是对所具有相同特征实体的抽象。
抱歉,你的悬赏值不够
public class Monster {
int level;
int hp=level*1000;
public Monster(int level, int hp) {
super();
this.level = level;
this.hp = hp;
}
public Monster(int level) {
super();
this.level = level;
this.hp=level*1000;
}
}
public class Hero {
int ep;
int level;
int attack;
public void hit(Monster monster){
monster.hp-=attack;
System.out.println("英雄攻击了怪物,怪物掉血"+attack);
}
public Hero(int ep, int level, int attack) {
super();
this.ep = ep;
this.level = level;
this.attack = attack;
}
}
public class TextNew {
public static void main(String[] args) {
Monster monster = new Monster(2);
Hero w = new Hero(0, 10, 100);
System.out.println("英雄等级"+w.level+",攻击力为"+w.attack);
System.out.println("怪物血量"+monster.hp);
for (int i = 0; i < 10; i++) {
w.hit(monster);
}
System.out.println("血量剩余"+monster.hp);
}
}