对象村出生人数
public class Person {
private final String name; // 姓名
private final int height; // 身高
private static int numberOfPeople = 0; // 人数
public Person(String name, int height) {
this.name = name;
this.height = height;
numberOfPeople++; // !!!由于设置为private,本代码下只能通过构造器修改numberOfPeople字段
}
// getNumberOfPeople通过Generate...生成
public static int getNumberOfPeople() { // 这是一个静态方法
return numberOfPeople;
}
public static void main(String[] args) {
// 方便起见,直接在Person类中添加一个main方法测试Person类
// 而不再引入PersonTest类
System.out.printf("对象村已出生 %d 人%n", Person.getNumberOfPeople());
// 无需创建对象,也可以直接使用静态方法访问静态变量! 静态的东西先于对象存在!
Person p = new Person("张三", 175);
Person p2 = new Person("李四", 185);
System.out.printf("对象村已出生 %d 人%n", Person.getNumberOfPeople());
for (int i = 0; i != 20; ++i) {
new Person("王五", 180);
}
System.out.printf("对象村已出生 %d 人%n", Person.getNumberOfPeople());
}
}
自动回合对战改进版
Hero.java
public class Hero {
private static final int MIN_HP = 0;
private static final int MAX_HP = 100; // 限制小兵的最大生命值
private static final int MAX_MAX_DAMAGE = 50; // 限制小兵的最大伤害值
private static int randomCharacterId = 0; // 保存角色id
public static boolean isDebugMode = true; // 控制是否打印对战细节,不开启则只打印对战结果
private final String heroName; // 英雄名称
private int hp; // 血条、生命值
private final int maxDamage; // 表示角色能够造成的最大伤害值
private Hero(String heroName, int hp, int maxDamage) {
// 请注意,这里将构造器设置为private使得无法在其它类中调用Hero构造器创建对象
this.heroName = heroName;
this.hp = hp;
this.maxDamage = maxDamage;
}
public static Hero of(String heroName, int hp, int maxDamage) {
return new Hero(heroName, hp, maxDamage);
}
public static Hero randomCharacter() {
String name = "小兵#" + randomCharacterId; // 小兵名字
randomCharacterId++; // id需要保持唯一,自增1
int hp = 10 + (int)(Math.random() * MAX_HP); // 小兵hp在10~110
int maxDamage = 10 + (int)(Math.random() * MAX_MAX_DAMAGE); // 小兵攻击在10~60
return new Hero(name, hp, maxDamage);
}
public String getHeroName() {
return heroName;
}
public int getHp() {
return hp;
}
public void attack(Hero otherHero) {
int damage = (int)(Math.random() * maxDamage); // 生成伤害值,随机造成0~maxDamage的伤害
// 对应英雄承受该伤害
otherHero.takeDamage(damage);
if (isDebugMode) {
System.out.printf("%s 对 %s 造成了 %d 点伤害", this.heroName, otherHero.heroName, damage);
System.out.printf("(%s 还剩 %d 生命值)\n", otherHero.heroName, otherHero.getHp());
}
}
public void takeDamage(int damage) {
this.hp -= damage;
if (this.hp < MIN_HP) {
this.hp = MIN_HP;
}
}
public boolean isAlive() {
return this.hp > MIN_HP;
}
public static void battle(Hero hero1, Hero hero2) {
System.out.printf("%s(HP: %d) 与 %s(HP: %d) 开始对战\n", hero1.getHeroName(), hero1.getHp(), hero2.getHeroName(), hero2.getHp());
int round = 1;
while (hero1.isAlive() && hero2.isAlive()) {
if (isDebugMode) {
System.out.printf("********* 回合 %d *********\n", round);
}
round++;
hero1.attack(hero2);
hero2.attack(hero1);
}
System.out.printf("经过 %d 回合对战 ", round - 1);
if (hero1.isAlive()) {
System.out.printf("%s 胜出%n", hero1.getHeroName());
} else if (hero2.isAlive()) {
System.out.printf("%s 胜出%n", hero2.getHeroName());
} else {
System.out.printf("%s 与 %s 同归于尽%n", hero1.getHeroName(), hero2.getHeroName());
}
System.out.println();
}
}
HeroTest.java
public class HeroTest {
public static void main(String[] args) {
Hero hero1 = Hero.of("齐天大圣", 250, 105);
Hero hero2 = Hero.of("二郎神", 110, 45);
Hero.battle(hero1, hero2);
Hero.isDebugMode = false;
for (int i = 0; i != 10; ++i) {
if (hero1.isAlive()) {
Hero.battle(hero1, Hero.randomCharacter());
}
}
}
}
标签:静态方法,静态,hp,private,int,字段,hero1,Hero,public From: https://www.cnblogs.com/xkxf/p/18440956