2016-11-12 24 views
0

私のプロジェクトでは、抽象クラスCharacterを作成し、Characterを拡張するクラスPlayerを作成する必要があります。クラスCharacterは抽象であるため、メインに呼び出すことはできず、クラスPlayerをメインに呼び出す必要があります。クラスを呼び出す私の試みは、主な機能であるAdventureGameV3のスイッチケースにあります。どのようにそれを呼び出すかに関する任意のアイデア。私はWeaponクラスも添付しましたが、私の質問に不可欠ではありません。拡張クラスからの呼び出し

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śあなたのコーディング "player = new Player(Type.ROGUE);"私のメインをコンパイルすると、 "エラー:シンボルを見つけることができません"と表示されます。何か案は? –

+0

AdventureGameV3クラスで列挙型が表示されていますか? public enum Type {ROGUE、PALADIN、JACKIE_CHAN、GOBLIN、SKELETON、WIZARD} –

+0

ルーブリックに触れると、パブリック列挙型{ROGUE ...}は抽象的な文字クラスにあるため、そうではありません。このため、私はこれをメインにどのように呼び出すのか非常に混乱しています。 –

0

「抽象クラス」の「文字」に列挙型「型」を割り当てようとしています。これは型の不一致で、コンパイルエラーが発生します。

新しいプレーヤインスタンスを作成します。

Eclipse、IntelliJ、Netbeansなどの統合開発環境(IDE)を使用することをお勧めします。これは、コンパイル時のエラーを早期に発見するのに役立ちます。 :)

+0

は、それは言う: *キャラクター(抽象クラ​​ス) -PUBLIC列挙 名:タイプ 値:ROGUEを、PALADIN .... 私は選手クラスでこのパブリック列挙型を置くことができるだろうか? –

+0

enumを使用する方法については、https://docs.oracle.com/javase/tutorial/java/javaOO/enum.htmlを参照してください。私はenum型の使い方とインスタンス化をより明確にすると思います。 – Arman

+0

私は前にこの記事を読んできましたが、コンストラクタでEnumを実装する方法についてはまだ不明です(Characterクラスで必須です)。何かヒント? –

関連する問題