1

我是初学者,所以请非常具体。无论如何,我在这里有三个代码类,我想从 cb 然后 ba 访问数据

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

如此,GoobyPls如此,如此,如此aStatsbViewStatsc

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

4

5 回答 5

2

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

class GoobyPls {
{
   private int CHealth = 20;

   public int getHealth(){
      return Chealth;
   }
}

然后viewStats这样做:

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

每当您想使用私有变量时,只需调用 getter 即可。或者也可以定义变量public,这样就可以直接在viewStats中调用。但是从设计的角度来看,getter 和 setter 更好,因为隐藏信息更好。除非有充分的理由这样做,否则应将字段声明为私有。

于 2013-08-16T11:11:22.127 回答
1

我强烈建议阅读有关类和对象的 Java 教程,这将为您提供良好的工作基础。

在类之间传递数据的最基本方法是在您的类中定义公共方法,其他对象可以调用这些方法来访问您的数据。例如:

public class Person {

    private String firstName; 
    private String lastName;

    public String getFirstName () {
        return firstName;
    }

    public String getLastName () {
        return lastName;
    }

}

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

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”,是“getter”的对应物。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
}

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

于 2013-08-16T11:07:06.130 回答
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.Health,State.AgilityState.Defence

于 2013-08-16T11:11:09.607 回答
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 进行编码将帮助您自己识别这类问题。

于 2013-08-16T11:08:27.253 回答
-1

将 getter 和 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;
}
于 2013-08-16T11:04:03.180 回答