Java超類與子類

小达不溜w發表於2024-11-06

//角色
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

相關文章