简体   繁体   English

尝试调用超类方法,但传递子类的参数?

[英]Trying to call on a superclass method, but pass a subclass's parameters?

Sorry new to Java may not have described it quite right. 对不起,Java的新手可能描述得不对。 Creating a text based adventure 创建基于文本的冒险

Basically I have a super class named GameCharacter. 基本上,我有一个名为GameCharacter的超类。 Two subclasses Player and Monster. 两个子类Player和Monster。 Monster has four subclasses Ork, Troll, Goblin, and the Boss Monster有四个子类,分别是Ork,Troll,Goblin和Boss

I have an abstract method called takeTurn() found in the super class. 我在超类中找到了一个名为takeTurn()的抽象方法。 I have the fleshed out methods within the subclasses. 我在子类中有充实的方法。 So when it comes to calling take turn, how do I know it will access the right subclass? 因此,在轮流调用时,我怎么知道它将访问正确的子类?

My super class 我的超级班

public abstract class GameCharacter{

    public abstract String getName();
    public abstract int getHealth();
    public abstract int getAttackPower();

   protected String gameCharacterName;
   protected int gameCharacterHealth;
   protected int gameCharacterAttack;

     public GameCharacter(String name, int health, int attack){
            this.gameCharacterName = name;
            this.gameCharacterHealth = health;
            this.gameCharacterAttack = attack;
     }

    public abstract void takeTurn(GameCharacter character);

My take turn method 我的转弯方法

public static void runCombatLoop(Monster monster){

    while (isPlayerAlive()&& isMonsterAlive(monster))
    {
        GameCharacter player = new Player();
        Monster enemy = new Monster(){};
        enemy.Monster();

        GameCharacter.takeTurn(player);
        GameCharacter.takeTurn(enemy);
    }


}// run combat loop end

In Java, all inheritance passes from superclass to subclass, including instance variables and methods. 在Java中,所有继承都从超类传递到子类,包括实例变量和方法。

Meaning, if you wanted to "call a superclass method" with "subclass parameters", you simply create an object in the subclass and call your method. 意思是,如果您想用“子类参数”“调用超类方法”,则只需在子类中创建一个对象并调用您的方法。


For example, in your superclass, you have 例如,在您的超类中,您有

public abstract void takeTurn(GameCharacter character);

And you have, hypothetically, two subclasses of GameCharacter: 假设有GameCharacter的两个子类:

public class Player extends GameCharacter { ... }

public class Enemy extends GameCharacter { ... }

Now, if you wanted to call takeTurn with a Player parameter or an Enemy parameter, you would put them in their respective subclasses. 现在,如果要使用Player参数或Enemy参数调用takeTurn ,则可以将它们放在各自的子类中。

//In Player.java
public class Player extends GameCharacter {
    //instance variables

    //methods (only takeTurn shown)
    void takeTurn( Player p ) {
         //take turn definition for Player here
    }
}

//In Enemy.java
public class Enemy extends GameCharacter {
    //instance variables

    //methods (only takeTurn shown)
    void takeTurn( Enemy e ) {
         //take turn definition for Enemy here
    }
}

An abstract method is NOT defined until you specifically define it in each respective subclass. 一个抽象方法, 没有限定,直到特别是在每个相应的子类中定义它。

You can think of an abstract class (methods, too) as a blueprint for a class. 您可以将抽象类(方法)也视为类的蓝图

In other words, since both Player and Enemy are subclasses of GameCharacter , and takeTurn( GameCharacter character) is an abstract method, you can create and call takeTurn in each individual class. 换句话说,由于PlayerEnemy都是GameCharacter子类,并且takeTurn( GameCharacter character)是抽象方法,因此可以在每个单独的类中创建并调用takeTurn

Note: You can do this because the abstract method takeTurn takes a GameCharacter , and both Player and Enemy extend GameCharacter 注意:您可以这样做,因为抽象方法takeTurn需要一个GameCharacter ,并且PlayerEnemy扩展了GameCharacter


EDIT : In order for you to call takeTurn( ... ) in main without creating an object, you must make takeTurn static . 编辑 :为了让您在main中调用takeTurn(...)而不创建对象,您必须使takeTurn为static

General practice recommends you making a GameCharacter object with new , and calling takeTurn( ... ) via that object, as I believe you would want multiple characters. 一般实践建议您使用new制作一个GameCharacter对象,并通过该对象调用takeTurn( ... ) ,因为我相信您会需要多个字符。

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

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