简体   繁体   English

Java 类扩展无法正常工作

[英]Java class extends not working properly

Am playing a bit around with java and am learning step by step.我正在玩 Java 并逐步学习。 To not write my whole life story, here it comes.为了不写我的整个人生故事,它来了。

I am making a text game with some stats, a player, enemies, etc. For this I am using classes.我正在制作一个带有一些统计数据、玩家、敌人等的文本游戏。为此,我使用了类。 Lately, I came accross the "extends" function and am trying to implement it.最近,我遇到了“扩展”功能并正在尝试实现它。 I made a class character, which extends to player and enemy.我制作了一个类角色,它扩展到玩家和敌人。 When I execute the code it seems as it wouldn't inherit anything.当我执行代码时,它似乎不会继承任何东西。 Would appreciate any advice.将不胜感激任何建议。 Thanks!谢谢!

PS which tags would be ok to use? PS 哪些标签可以使用?

import java.util.Random;

public class Character
{
    Random rand = new Random();

    int cc;
    int strength;
    int life;

    //getters and setters
}

public class Player extends Character
{
    int cc = rand.nextInt(20)+51;
    int strength = rand.nextInt(3)+4;
    int life = rand.nextInt(5)+16;
}

public class Enemy extends Character
{
    int cc = rand.nextInt(10)+31;
    int strength = rand.nextInt(3)+1;
    int life = rand.nextInt(5)+6;
}

class myClass
{
    public static void main(String[] args)                                                       
    {
    Player argens = new Player();

    System.out.println("This is you:\n");
    System.out.println("Close Combat " + argens.getCC());
    System.out.println("Strength " + argens.getStrength());
    System.out.println("Life " + argens.getLife());


    Enemy kobold = new Enemy();

    fight (argens, kobold);

    fight (argens, kobold);
    }

    static void fight(Player p, Enemy e)
    {

        p.setLife(p.getLife() - e.getStrength());

System.out.println("\nRemaining life");

System.out.println(p.getLife());

System.out.println(e.getLife());

    }

}

This code:这段代码:

public class Player extends Character
{
    int cc = rand.nextInt(20)+51;
    int strength = rand.nextInt(3)+4;
    int life = rand.nextInt(5)+16;
}

does not set fields of the superclass.不设置超类的字段。 It declares and sets new fields in the subclass, without touching fields of the superclass.它在子类中声明和设置新字段,而不涉及超类的字段。

To set fields of the superclass, make the protected and set them in the constructor of the subclass:要设置超类的字段,请在子类的构造函数中进行protected并设置它们:

public class Player extends Character
{
    public Player()
    {
        cc = rand.nextInt(20)+51;
        strength = rand.nextInt(3)+4;
        life = rand.nextInt(5)+16;
    }
}

The thing is that you are not overwriting these values in the base class but in the inherited.问题是您不是在基类中而是在继承中覆盖这些值。

You should initialize these values in a constructor.您应该在构造函数中初始化这些值。

Example:例子:

public class Character {
  int cc;
  // ...
}

public class Player extends Character {
  public Player() {
    cc = 5;
    // ...
  }
}

What you did was declaring variables in the base class and not initializing them and declaring variables in the sub class with same name at the same time.你所做的是在基类中声明变量而不是初始化它们并同时在子类中声明具有相同名称的变量。

more reading: https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html更多阅读: https : //docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

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

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