[英]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
无论如何,我在这里有三个代码类,我想从cb然后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
所以
GoobyPls
是a
, Stats
是b
, ViewStats
是c
also i cant put it all in one class because eventually there will be a modifier class to edit CHealth
, MHealth
etc 我也不能把它全部放在一个类中,因为最终会有一个修饰符类来编辑
CHealth
, MHealth
等
In your class GoobyPls
add getters for each item as following: 在你的班级
GoobyPls
为每个项目添加getter,如下所示:
class GoobyPls {
{
private int CHealth = 20;
public int getHealth(){
return Chealth;
}
}
and then in viewStats
do this: 然后在
viewStats
执行此操作:
GoodyPls gp = new GoobyPls();
System.out.println(gp.getHealth());
just call getters whenever when you want to use a private variable. 只要你想使用私有变量,就可以调用getter。 Or you can also define the variables public, so that you can directly call them in viewStats.
或者您也可以将变量定义为public,以便您可以在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. 我强烈建议您阅读有关类和对象的Java教程 ,这将为您提供良好的工作基础。
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". 名称如
getX()
返回值的方法称为“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".
这些被称为“setter”,是“getters”的对应物。 Building on
Person
from above: 建立在
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;
}
}
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. 如果要访问类外部的私有变量,则必须使用如下所示的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;
}
}
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
在您的
viewstat
类的foo()
方法中,您可以通过State.Health
, State.Agility
和State.Defence
访问about值。
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
而不是字符串
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. 使用IDE进行编码它将帮助您自己识别这类问题。
Put getters and setters to your class.Or make fields public.(i don't suggest the second) 把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;
}
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.