//角色
package ex1;
public class Character {
private final String name; //角色名字
private int hp; //角色血量
public static int charactsum = 0; //统计次数
private static String charmask = "普通角色";//角色的职业
public Character(String name, int hp) {
this.name = name;
this.hp = hp;
charactsum++;
System.out.printf("Character类调用 %d 次\n", charactsum);
}
public String getName() {
return name;
}
public int getHp() {
return hp;
}
public String charname(){ //返回角色名字和生命值
return "我是" + getName() + "我的生命值" + getHp();
}
public String blackcharmask(){
return charmask;
}
}
//战士
package ex1;
public class Warrior extends Character {
public static int warrisum = 0;
private final String charmask = "战士";
public Warrior(String name, int hp) {
super(name,hp);
warrisum++;
System.out.printf("Warrior类调用 %d 次\n", warrisum);
}
public String blackcharmask(){
return charmask;
}
}
//法师
package ex1;
public class Mage extends Character{
private final String charmask = "法师";
public static int magesum = 0;
public Mage(String name, int hp) {
super(name, hp);
magesum++; ;
System.out.printf("Mage类调用 %d 次\n", magesum);
}
public String blackcharmask(){
return charmask;
}
}
//冰法师
package ex1;
public class IceMage extends Mage{
private final String charmask = "法师";
public static int icemagesum = 0;
public IceMage(String name, int hp) {
super(name, hp);
icemagesum++;
System.out.printf("IceMage类调用 %d 次\n", icemagesum);
}
public String blackcharmask(){
return charmask;
}
}
//火法师
package ex1;
public class FireMage extends Mage{
private final String charmask = "法师";
public static int firemagesum = 0;
public FireMage(String name, int hp) {
super(name, hp);
firemagesum++;
System.out.printf("FireMage类调用 %d 次\n", firemagesum);;
}
public String blackcharmask(){
return charmask;
}
}
//测试package ex1;
public class Main {
private static void checkCharacter(Character c) { //通过创建角色的类判断角色的职业
System.out.println("这是" + c.blackcharmask()); //c.blackcharmsk() 使用函数获取角色职业
}
public static void main(String[] args) {
System.out.println("");
Character[] characters = new Character[5];
characters[0] = new Character("aa", 100);
characters[1] = new Warrior("bb", 100);
characters[2] = new Mage("cc", 100);
characters[3] = new IceMage("dd", 100);
characters[4] = new FireMage("ee", 100);
System.out.println("");
System.out.println(characters[3].charname()); //使用charname()函数获取角色名字和HP;
System.out.println("*****************************");
for(Character c : characters) {
checkCharacter(c);
}
}
}
//结果
"C:\Program Files\Java\jdk-21\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2024.2.4\lib\idea_rt.jar=60555:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2024.2.4\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\Users\34869\IdeaProjects\untitled\out\production\untitled ex1.Main
Character类调用 1 次
Character类调用 2 次
Warrior类调用 1 次
Character类调用 3 次
Mage类调用 1 次
Character类调用 4 次
Mage类调用 2 次
IceMage类调用 1 次
Character类调用 5 次
Mage类调用 3 次
FireMage类调用 1 次
我是dd我的生命值100
这是普通角色
这是战士
这是法师
这是法师
这是法师
进程已结束,退出代码为 0
标签:Java,String,int,子类,hp,Character,超类,public,name From: https://www.cnblogs.com/wyqw/p/18530898