简体   繁体   中英

Accessing data from other classes

Im a beginner so please be VERY specific. Anyway i have three code classes here and i want to acces data from cb then ba Eg

class GoobyPls {
{
    private int CHealth = 20;
    private int MHealth = 20;
    private int CAgility = 10;
    private int MAgility = 10;
    private int CDefence = 5;
    private int MDefence = 5;
}   
}

class Stats {
public static void foo() {
    string Health =  CHealth + "/" + MHealth ;
    string Agility = CAgility + "/" + MAgility;
    string Defence = CDefence + "/" + MDefence;
}

}

class ViewStats {
public static void foo() {
System.out.println("Health");
                System.out.println(Health);
                System.out.println(" ");
                System.out.println("Agility");
                System.out.println(Agility);
                System.out.println(" ");
                System.out.println("Defence");
                System.out.println(Defence);
                System.out.println(" ");
  }
}

So GoobyPls is a , Stats is b and ViewStats is c

also i cant put it all in one class because eventually there will be a modifier class to edit CHealth , MHealth etc

In your class GoobyPls add getters for each item as following:

class GoobyPls {
{
   private int CHealth = 20;

   public int getHealth(){
      return Chealth;
   }
}

and then in viewStats do this:

GoodyPls gp = new GoobyPls();
System.out.println(gp.getHealth());

just call getters whenever when you want to use a private variable. Or you can also define the variables public, so that you can directly call them in viewStats. But getters and setters are better as a design point of view, since hiding information is better. Fields should be declared private unless there is a good reason for not doing so.

I highly suggest reading through the Java tutorial on classes and objects , which will give you a good basis to work from.

The most basic way to pass data between classes is to define public methods in your class that other objects can call to get access to your data. For example:

public class Person {

    private String firstName; 
    private String lastName;

    public String getFirstName () {
        return firstName;
    }

    public String getLastName () {
        return lastName;
    }

}

Methods with names like getX() that return values are called "getters". Then, in another class, you can access that data, for example:

public void elsewhere () {
    Person p = new Person();
    System.out.println(p.getFirstName() + " " + p.getLastName());
}

Another way to communicate with classes is to write methods that take parameters, eg:

public void printFullName (Person p) {
    System.out.println(p.getFirstName() + " " + p.getLastName());
}

public void elsewhere () {
    Person p = new Person();
    printFullName(p);
}

You may also want to provide methods to set data in an object. These are called "setters" and are the counterpart to "getters". Building on Person from above:

public class Person {

    private String firstName; 
    private String lastName;

    public String getFirstName () {
        return firstName;
    }

    public String getLastName () {
        return lastName;
    }

    public void setFirstName (String firstName) {
        this.firstName = firstName;
    }

    public void setLastName (String lastName) {
        this.lastName = lastName;
    }

}

Then, other objects can modify a person's data, eg:

public void elsewhere () {
    Person p = new Person();
    p.setFirstName("Bob");
    System.out.println(p.getFirstName()); // prints "Bob"
}

Here's an example using everything from above:

public void swapPersonFirstAndLastName (Person p) {
    String temporary = p.getFirstName();
    p.setFirstName(p.getLastName());
    p.setLastName(temporary);
}

public void printFullName (Person p) {
    System.out.println(p.getFirstName() + " " + p.getLastName());
}

public void example () {
    Person p = new Person();
    p.setFirstName("John");
    p.setLastName("Smith");
    swapPersonFirstAndLastName(p);
    printFullName(p); // prints Smith John
}

Hope that helps and good luck. Read those tutorials!

If you want to access the private variable outside the class, you must use the getter method like below.

class GoobyPls {
private int CHealth = 20;
private int MHealth = 20;
private int CAgility = 10;
private int MAgility = 10;
private int CDefence = 5;
private int MDefence = 5;
public int getCHealth() {
    return CHealth;
}
public int getMHealth() {
    return MHealth;
}
public int getCAgility() {
    return CAgility;
}
public int getMAgility() {
    return MAgility;
}
public int getCDefence() {
    return CDefence;
}
public int getMDefence() {
    return MDefence;
}
}  

Below is your State class

class Stats {
public static void foo() {
GoobyPls g=new GoobyPls();
int Health =  g.getCHealth() / g.getMHealth() ;
int Agility = g.getCAgility() / g.getMAgility();
int Defence = g.getCDefence() / g.getMDefence();
}

and in your viewstat class's foo() method you can access the about value by State.Health , State.Agility and State.Defence

First of all you can't you private field like follows

 class GoobyPls {
{
    private int CHealth = 20;  // you can't use private here
    private int MHealth = 20;   // and care on java naming conventions
    private int CAgility = 10;
    private int MAgility = 10;
    private int CDefence = 5;
    private int MDefence = 5;
}
} 

and it should be String not string

   string Health =  CHealth + "/" + MHealth ; // String not string

Use IDE to do coding it will help you to identify those kind of issue by your own.

Put getters and setters to your class.Or make fields public.(i don't suggest the second)

class GoobyPls {

    public int getCHealth() {
        return CHealth;
    }
    public void setCHealth(int cHealth) {
        CHealth = cHealth;
    }
    public int getMHealth() {
        return MHealth;
    }
    public void setMHealth(int mHealth) {
        MHealth = mHealth;
    }
    public int getCAgility() {
        return CAgility;
    }
    public void setCAgility(int cAgility) {
        CAgility = cAgility;
    }
    public int getMAgility() {
        return MAgility;
    }
    public void setMAgility(int mAgility) {
        MAgility = mAgility;
    }
    public int getCDefence() {
        return CDefence;
    }
    public void setCDefence(int cDefence) {
        CDefence = cDefence;
    }
    public int getMDefence() {
        return MDefence;
    }
    public void setMDefence(int mDefence) {
        MDefence = mDefence;
    }
    private int CHealth = 20;
    private int MHealth = 20;
    private int CAgility = 10;
    private int MAgility = 10;
    private int CDefence = 5;
    private int MDefence = 5;
}  

Then you can access like this:

class Stats {
public static void foo() {
GoobyPls gbp=new GoobyPls();
    string Health =  gbp.getCHealth + "/" + MHealth ;
    string Agility = gbp.getCAgility + "/" + MAgility;
    string Defence = gbp.getCDefence + "/" + MDefence;
}

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