简体   繁体   English

如何从(默认程序包)访问程序包中的非静态类字段?

[英]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 (所有AB成员都package privatepackage 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类访问Aget()方法。

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.

相关问题 如何从不同的类访问非静态变量和方法 - How to access non-static variables & methods from a different class 无法访问从非静态到非静态类/方法的方法调用 - Cannot access a method call from non-static to non-static class/method 从另一个类访问非静态变量 - access non-static variable from another class 非静态嵌套线程-来自另一个类的访问(Java) - Non-static nested thread - access from another class (Java) 如何在非静态方法中从另一个类调用非静态方法? (java) - How do I call a non-static method from another class in a non-static method? (java) 如何使用Java 8中的方法引用从非静态类中调用非静态方法? - How to call a non-static method from a non-static class using method reference in Java 8? 即使将静态对象定义为静态对象,如何也可以访问非静态字段? - How can a static object access non-static fields even though it's defined as static? 如何访问其他包中非公共类的非公共字段? - How can I access non public fields of non public class in other package? 如何从 jshell 访问默认 package 中的 Java class? - How to access a Java class in the default package from jshell? 使用所有非静态方法但没有非静态字段的类是否有意义? (或所有静态字段和方法以及构造函数) - Is there a point to having a class with all non-static methods but no non-static fields? (or all static fields and methods along with a constructor)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM