简体   繁体   中英

How to access individual values of an object that is stored in an array of similar objects in Java

I am having a problem with trying to keep my coding organized and as simple as possible. I basically have an array of similar objects that hold multiple values. I am wanting to access those individual values and be able to modify them at will but cannot seem to acess them. This is what the code basically looks like...

    //In file Champion.java
    package Champions;
    public interface Champion{}

    //In another file ChoGath.java
    package Champions;
    public class ChoGath implements Champion{
    public static double health = 440.0;
    }

    //Yet another file Ahri.java
    package Champions;
    public class Ahri implements Champion{
    public static double health = 380.0;
    }

    //In the main build file LOLChampBuilder.java
    package LOLChampBuilder;
    import Champions.*;
    public class LOLChampBuilder{
        public static Champion[] listOfChampions = {new ChoGath(), new Ahri()};
        public static void main(String args[]){
            //This next line works
            System.out.println(new ChoGath().health);
            //This next line does not work
            System.out.println(listOfChampions[0].health);
        }
    }

There are more variables and whatnot but this is the basic problem.

ChoGath and Ahri are part of the group Champions and each has their own unique value for health. I want to be able to combine it all into an array for ease of grabbing values because I know where ChoGath (as an example) is in the array.

I get the error Cannot find symbol 'health' in Champions.Champion . I have gone and created the value in Champion and that fixes it (and also change it to class and then extends instead of implements) but then when I go to print the value is always 380.0 as it was the most recent edit to the value health in Champion.

I want it so that I can group all the "Champions" together under a single array (so they need to be the same object type ie: Champion, correct me if I'm wrong ) and access their individual values. I cannot seem to do this so I don't know if I need to use ArrayList (which I've never used) or something else entirely. Now I know I could fix this and put it all into a massive file but I am trying to use multiple files for organizational purposes as well as cleanliness. Thoughts on what to do?

You need to add getHealth() to your interface. That's what getters are for.

Also avoid the use of static variables. They tend to produce programming errors.

You have to use getters and setters in the interface to get this functionality, or use a base class instead of an interface for Champion , eg:

interface Champion
{
    public int getHealth();
    public void setHealth(int health);
}

You need to have health variable in your interface. That is similar to concept of Subclass and Superclass. You can't access the variables of subclass using a superclass type because all superclass are not subclass(vice versa is true).You can only access them if its defined in the superclass. If you use a health variable in interface it has to be final static. So its better you use a setter and getter method in the interface to get the value.

interface Champion { public int getHealth(); }

Now implement this method in the ChoGath and Ahri classes

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