从其他类访问数据

时间:2013-08-16 11:01:45

标签: java class variables private

我是初学者,所以请非常具体。无论如何,我在这里有三个代码类,我想从c-b然后b-a E.g访问数据。

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(" ");
  }
}

所以GoobyPlsaStatsbViewStatsc

我也不能把它全部放在一个类中,因为最终会有一个修饰符类来编辑CHealthMHealth

5 个答案:

答案 0 :(得分:2)

在您的班级GoobyPls中为每个项目添加getter,如下所示:

class GoobyPls {
{
   private int CHealth = 20;

   public int getHealth(){
      return Chealth;
   }
}

然后在viewStats执行此操作:

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

只要您想使用私有变量,就可以调用getter。或者您也可以将变量定义为public,以便您可以在viewStats中直接调用它们。但是作为设计观点,吸气剂和制定者更好,因为隐藏信息更好。 除非有充分理由这样做,否则应将字段声明为私有。

答案 1 :(得分:1)

我强烈建议您阅读Java tutorial on classes and objects,这将为您提供良好的工作基础。

在类之间传递数据的最基本方法是在类中定义其他对象可以调用以获取对数据的访问权限的公共方法。例如:

public class Person {

    private String firstName; 
    private String lastName;

    public String getFirstName () {
        return firstName;
    }

    public String getLastName () {
        return lastName;
    }

}

返回值的名称如getX()的方法称为“getters”。然后,在另一个类中,您可以访问该数据,例如:

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

与类通信的另一种方法是编写带参数的方法,例如:

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

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

您可能还想提供设置对象数据的方法。这些被称为“setter”,是“getters”的对应物。建立在Person上方:

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;
    }

}

然后,其他对象可以修改一个人的数据,例如:

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

以下是使用上述所有内容的示例:

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
}

希望有所帮助,祝你好运。阅读这些教程!

答案 2 :(得分:1)

如果要访问类外的私有变量,必须使用如下所示的getter方法。

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;
}
}  

以下是您的州级

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();
}

viewstat班级foo()方法中,您可以State.HealthState.AgilityState.Defence

访问约值

答案 3 :(得分:0)

首先,你不能像以下那样私有领域

 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;
}
} 

它应该是String而不是字符串

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

使用IDE进行编码,它可以帮助您自己识别这类问题。

答案 4 :(得分:-1)

将getters和setter放到你的班级。或者将字段公开。(我不建议第二个)

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;
}  

然后您可以这样访问:

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;
}