简体   繁体   中英

Java class extends not working properly

Am playing a bit around with java and am learning step by step. 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?

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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