Hi I am trying to access a public object in another package within a project.
嗨,我试图访问项目中的另一个包中的公共对象。
I am trying to access the 'opponent' object which is of type 'Character' in the Attribute class.
我试图访问属性类中'对象'类型'对象'的'对手'对象。
public class Engine {
public static Character opponent;
}
Class I am trying to access object in. "This class is in another package".
我试图访问对象的类。“这个类在另一个包中”。
public int opponentAttackDamage() {
int attack = opponent.getAttribute().getAttack();
}
2 个解决方案
#1
3
In order to access an static attribute from anywhere even in the same class where it's declared (as a good practice) you should use the name of the class follow by dot an the name of the attribute:
为了从任何地方访问静态属性,即使在声明它的同一个类中(作为一种好习惯),你应该使用类的名称后跟点和属性的名称:
Engine.opponent.getAttribute().getAttack();
Also you should have in mind that opponent object must be initialized in somewhere in your class (opponent = new Opponent() - I guess - ).
另外你应该记住,对手对象必须在你班级的某个地方进行初始化(对手=新对手() - 我猜 - )。
#2
0
If opponent is a static attribute of the Engine class, and the method you are accessing it from is not in the same class, you need to mention Engine.opponent to access it. Also, you need to import the package where Engine class is defined.
如果opponent是Engine类的静态属性,并且您从中访问它的方法不在同一个类中,则需要提及Engine.opponent来访问它。此外,您需要导入定义Engine类的包。
#1
3
In order to access an static attribute from anywhere even in the same class where it's declared (as a good practice) you should use the name of the class follow by dot an the name of the attribute:
为了从任何地方访问静态属性,即使在声明它的同一个类中(作为一种好习惯),你应该使用类的名称后跟点和属性的名称:
Engine.opponent.getAttribute().getAttack();
Also you should have in mind that opponent object must be initialized in somewhere in your class (opponent = new Opponent() - I guess - ).
另外你应该记住,对手对象必须在你班级的某个地方进行初始化(对手=新对手() - 我猜 - )。
#2
0
If opponent is a static attribute of the Engine class, and the method you are accessing it from is not in the same class, you need to mention Engine.opponent to access it. Also, you need to import the package where Engine class is defined.
如果opponent是Engine类的静态属性,并且您从中访问它的方法不在同一个类中,则需要提及Engine.opponent来访问它。此外,您需要导入定义Engine类的包。