2016-11-12 70 views
0

對於我的項目,我必須創建一個抽象類Character,然後創建一個擴展Character的類Player。因爲class,Character是抽象的,所以我不能把它叫做main,而是我必須把class Player叫做main。我嘗試調用該類是在主要功能AdventureGameV3中的開關情況下。關於如何調用它的任何想法。我也附加了武器類,但它不應該是我的問題必不可少的。從擴展類調用

import java.util.*; 
import java.util.Random; 

public abstract class Character { 

final int ROGUE_INIT_HP = 55; 
final int ROGUE_INIT_STRENGTH = 8; 
final int PALADIN_INIT_HP = 35; 
final int PALADIN_INIT_STRENGTH = 14; 
final int CHAN_INIT_HP = 45; 
final int CHAN_INIT_STRENGTH = 10; 

private String name; 
private int hitPoints; 
private int strength; 
private int weapon; 



public enum Type{ROGUE, PALADIN, JACKIE_CHAN, GOBLIN, SKELETON, WIZARD} 

Type cType; 

//holds data for each character 
public Character(Type cType) { 
    this.cType = cType; 
    switch(cType){ 
     case ROGUE: 
      this.name="ROGUE"; 
      this.hitPoints=ROGUE_INIT_HP; 
      this.strength=ROGUE_INIT_STRENGTH; 
      Weapon weapon1 = new Weapon(name,Weapon.SHORT_SWORD_MIN,Weapon.SHORT_SWORD_MAX); 
      this.weapon = weapon1.getDamage(); 
      break; 
     case PALADIN: 
      this.name="PALADIN"; 
      this.hitPoints=PALADIN_INIT_HP; 
      this.strength=PALADIN_INIT_STRENGTH; 
      Weapon weapon2 = new Weapon(name,Weapon.LONG_SWORD_MIN,Weapon.LONG_SWORD_MAX); 
      this.weapon = weapon2.getDamage(); 
      break; 
     case JACKIE_CHAN: 
      this.name="JACKIE CHAN"; 
      this.hitPoints=CHAN_INIT_HP; 
      this.strength=CHAN_INIT_STRENGTH; 
      Weapon weapon3 = new Weapon(name,Weapon.JUMP_KICK_MIN,Weapon.JUMP_KICK_MAX); 
      this.weapon = weapon3.getDamage(); 
      break; 

    } 
} 

public String getName() 
{ 
    return name; 
} 
public int getHitPoints() 
{ 
    return hitPoints; 
} 
public int getStrength() 
{ 
    return strength; 
} 
public void setStrength(int strength) 
{ 
    this.strength=strength; 
} 
public void setWeapon(int weapon) 
{ 
    this.weapon=weapon; 
} 
public void attack() 
{ 

} 
public void increaseHitPoints(int pointIncrease) 
{ 
    hitPoints+=pointIncrease; 
} 
public void decreaseHitPoints(int pointDecrease) 
{ 
    hitPoints-=pointDecrease; 
} 
public boolean isDefeated() 
{ 
    if(hitPoints>0) 
     return true; 
    else 
     return false; 
} 

}

public class Player extends Character{ 
private int coins; 
private String[] Potion; 

public Player(Type playerType){ 
    super(playerType); 
    coins=0; 
    String[] inventory = new String[5]; 
} 

public void increaseStrength(int strengthIncrease){ 
    super.setStrength(super.getStrength() + strengthIncrease); 
} 

public int getCoins(){ 
    return coins; 
} 
public int increaseCoins(int coins){ 
    this.coins+=coins; 
} 
public int decreaseCoins(int coins){ 
    this.coins-=coins; 
} 

}

public class AdventureGameV3 
    { 
    public static void main(String[] args) 
{ 
final int ROGUE_INIT_HP = 55; 
final int ROGUE_INIT_STRENGTH = 8; 
final int PALADIN_INIT_HP = 35; 
final int PALADIN_INIT_STRENGTH = 14; 
final int CHAN_INIT_HP = 45; 
final int CHAN_INIT_STRENGTH = 10; 

final int MINION_INIT_HP = 25; 
final int GOBLIN_INIT_STRENGTH = 4; 
final int SKELETON_INIT_STRENGTH = 3; 

int characterChoice = 0; 
Scanner keyboard = new Scanner(System.in); 

    System.out.println("\nAdventure Game - Start!\n"); 
    System.out.println("Here are the characters:"); 
    System.out.println("1. Rogue\n2. Paladin\n3. Jackie Chan\n"); 


    System.out.print("Which character do you choose?: "); 
    characterChoice = keyboard.nextInt(); 
    switch(characterChoice){ 
    case 1: 
     Character player = Type(ROGUE); 
     break; 


    } 
     System.out.printf("\nYou chose: %s\n\n", player1); 
} 

}

回答

1

使用您的播放器類。

System.out.println("\nAdventure Game - Start!\n"); 
System.out.println("Here are the characters:"); 
System.out.println("1. Rogue\n2. Paladin\n3. Jackie Chan\n"); 

Player player; 
System.out.print("Which character do you choose?: "); 
characterChoice = keyboard.nextInt(); 
switch(characterChoice){ 
case 1: 
    player = new Player(Type.ROGUE); 
    break; 


} 
System.out.printf("\nYou chose: %s\n\n", player); 
+0

GrybośI嘗試了你的編碼「player = new Player(Type.ROGUE);」當我編譯我的主,它說「錯誤:找不到符號」。有任何想法嗎? –

+0

在AdventureGameV3類中是否可以看到枚舉類型? public enum類型{ROGUE,PALADIN,JACKIE_CHAN,GOBLIN,SKELETON,WIZARD} –

+0

根據標題,公共枚舉類型{ROGUE ...}在抽象字符類中,所以它不會。這就是爲什麼我非常困惑我將如何將這稱爲主。 –

0

您試圖爲(抽象)類「Character」分配枚舉「Type」。這是一種類型不匹配並導致編譯錯誤。

你想要做的是創建一個新的播放器實例。

我建議您使用集成開發環境(IDE),如Eclipse,IntelliJ或Netbeans,這些提示您可以編譯時錯誤並幫助您儘早找到錯誤。 :)

+0

在專欄,它說: *字符(抽象類) - 公共枚舉 名稱:類型 值:盜賊,聖騎士.... 請問我能夠把這個公共枚舉在播放器類? –

+0

查看關於如何使用枚舉的https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html。我認爲這會使枚舉類型的用法和實例更清晰。 – Arman

+0

我讀過這篇文章之前,我仍然不清楚如何實現一個構造函數的枚舉(在Character類中是必需的)。任何提示? –