简体   繁体   English

从扩展类呼叫

[英]Calling from extended class

For my project I have to create an abstract class Character and then create a class Player that extends Character. 对于我的项目,我必须创建一个抽象类Character,然后创建一个扩展Character的Player类。 Because class, Character, is abstract I cannot call it to the main and instead I have to call the class Player to the main. 因为Character类是抽象的,所以我不能将其称为main,而必须将Player类称为main。 My attempt to call the class is in the switch case in AdventureGameV3 which is the main function. 我试图调用该类的尝试是在AdventureGameV3的switch案例中进行的,该案例是主要功能。 Any ideas on how to call it. 关于如何调用它的任何想法。 I have also attached the Weapon class, but it shouldn't be to essential to my question. 我还加入了武器课,但这对我的问题来说不是必不可少的。

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);
}

} }

Use your Player class. 使用您的Player类。

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);

You're trying to assign a enumeration "Type" to a (abstract) class "Character". 您试图将枚举“类型”分配给(抽象)类“字符”。 This is a type mismatch and results in a compile error. 这是类型不匹配,并导致编译错误。

What you want to do is create a new Player instance. 您要做的是创建一个新的Player实例。

I advise you to use an integrated development environment (IDE) such as Eclipse, IntelliJ or Netbeans that hint you to compile-time errors and help you find the bugs early on. 我建议您使用集成开发环境(IDE),例如Eclipse,IntelliJ或Netbeans,它们会提示您编译时错误并帮助您尽早发现错误。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM