[英]How to access non-static class fields in a package from the (default package)?
So I have this class (let's call it "A") with this get()
method which returns a class field, and which I want to access from another class ("B"). 因此,我有一个带有此
get()
方法的类(我们称它为“ A”),该方法返回一个类字段,并且我想从另一个类(“ B”)进行访问。 Important: both classes are abstract
. 重要提示:这两个类都是
abstract
。 These classes are in the same package
( package private
for all A
and B
members). 这些类在同一
package
(所有A
和B
成员都package private
的package private
)。 The Main
class has no package (or better, the default one). Main
类没有包(或者更好的是默认包)。
[before: twisted and useless description] [之前:扭曲和无用的描述]
I need from B
class to access A
's get()
method. 我需要从
B
类访问A
的get()
方法。
Tried with: 尝试过:
1) non-static call Aobj.get()
starting from the main
-located root object, but it seems to want the static call (weird, none of the members is static
); 1)非静态呼叫
Aobj.get()
从起始main
-位于根对象,但它似乎要的静态调用(怪异,没有成员是static
);
2) tried to access it statically and I got the Cannot make a static reference to the non-static field
error (of course). 2)尝试静态访问它,但我得到了
Cannot make a static reference to the non-static field
错误(当然)。
...so I'm stucked, still getting the accessing syntax error, both ways I try. ...所以我被困住了,但仍然遇到访问语法错误,两种方法都尝试。 Any suggests?
有什么建议吗?
EDIT : 编辑:
So I came to an end. 所以我结束了。 I'll now try to explain my errors.
现在,我将尝试解释我的错误。
Basically , the problem was I wanted to access an object located in the Main
class (default package) from a class within a package. 基本上 ,问题是我想从包中的类访问位于
Main
类(默认包)中的对象。 So I got a visibility problem. 所以我遇到了可见性问题。
In other circumstances, I would have resolved importing the class, but I figured out that you cannot import a class located in the default package , so I created a main
package and imported Main
class in B
class. 在其他情况下,我将解决导入该类的问题,但是我发现您无法导入位于默认包中的类 ,因此我创建了一个
main
包并在B
类中导入了Main
类。
The worst mistake I kept making was thinking I had to access the main.main(String[])... etc ...Aobj
starting from the main
itself , which is a really really bad thought (still can't figure out how I thought it). 我一直犯的最严重错误是认为我必须访问
main.main(String[])... etc ...Aobj
从 main
本身开始 ,这确实是一个非常糟糕的想法 (仍然无法弄清楚如何我认为)。
In fact, like Stephen C pointed out: 实际上,就像斯蒂芬·C指出的那样:
The is true irrespective of the situation with packages, and irrespective of the path you take through other classes.
无论包的情况如何,以及通过其他类的路径如何,都是如此。
Finally, the code example... to be honest, the code is now really different from the days I wrote this question, and honestly I can't reproduce the faulty conditions since I don't remember all the faulty logic I was trying to achieve. 最后,是代码示例……老实说,现在的代码与我编写此问题的时代确实有所不同,并且说实话,由于我不记得我试图尝试的所有错误逻辑,因此我无法重现错误条件实现。 (My fault.) I only wish to have explained myself properly this time.
(我的错。)这次我只想对自己做正确的解释。
Thanks to everyone to tried to help. 感谢大家的帮助。
If B's get()
method is not static
then you need an instance of B
if you want to call the method; 如果B的
get()
方法不是static
的, B
调用该方法,则需要B
的实例。 eg 例如
B b = ....
b.get();
The is true irrespective of the situation with packages, and irrespective of the path you take through other classes, etcetera. 不管包的情况如何 ,以及您通过其他类的路径如何 ,等等,都是如此。 (Though I'm not sure I entirely understand your "prose" description of all of that ...)
(尽管我不确定我是否完全理解您对所有这些的“散文”描述...)
Just from what you've described, (without code), you need access to an instance of b
. 仅从您描述的内容(没有代码),您就需要访问
b
的实例。 There are a couple of ways this could go. 有几种方法可以解决。 If the method in
A
in question could use a different B
instance for each call, just add a B
argument to that method: 如果所讨论的
A
中的方法可以为每个调用使用不同的B
实例,则只需向该方法添加一个B
参数:
public class B {
public int get() { //Or whatever the return type of get is
//...
}
}
public class A {
public int methodThatNeedsABInstance(B instance) {
int x = instance.get(); //Can call get method
}
}
Which is then called by: 然后由以下方式调用:
B b = new B();
A a = new A();
a.methodThatNeedsABInstance(b);
If each A
instance should use the same B
instance for all of it's calls, make it a field of type B
: 如果每个
A
实例都应使用相同的B
实例进行所有调用,则将其设置为B
类型的字段:
public class A {
private B instance;
//Constructors, setters and getters for b as necessary
public int methodThatNeedsABInstance() {
int x = instance.get(); //Can call get method
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.