简体   繁体   English

我的主要方法无法在Java的不同包中看到所有公共方法

[英]My main method can't see all public methods in a different package in java

My main method in a package by itself can only see some of the methods in the other package even though they are all public. 我自己的包中的主要方法本身只能看到另一个包中的某些方法,即使它们都是公共的。 This is an example of my code. 这是我的代码的示例。

    package stuff.code;
    public class AObject
    {
        public AObject();
        public AObject(String str);
        public int getLength();
        public int getHeight();
    }

    package stuff.code;
    public class BObject extends AObject
    {
        public BObject();
        public BObject(String str);
    }

    package stuff.test;
    import stuff.code.AObject;
    import stuff.code.BObject;
    public class tester
    {
        AObject a = new AObject(); //no red underline
        AObject aa = new AObject("some string"); //no red underline
        BObject b = new BObject(); //no red underline
        BObject bb = new BObject("some string"); //tells me there is no such constructor
        b.getLength(); //tells me I need a getLine method in BObject too
    }

I've looked into setAccessible(boolean flag) method but I didn't understand most of it and it was still undefined after I imported java.lang.reflect.AccessibleObject. 我已经研究了setAccessible(boolean flag)方法,但是我大部分都不了解,并且在导入java.lang.reflect.AccessibleObject之后仍然不确定。

This is not my actual code. 这不是我的实际代码。 I wanted this to be vague so that an answer would be more useful to many other people that run into this problem. 我希望这能含糊其词,以使答案对遇到此问题的许多其他人更有用。 My methods are all concrete with a body but the body isn't the issue. 我的方法对于身体来说都是具体的,但身体不是问题。 It runs fine when everything is in the same package but the tester class must be in a different package. 当所有内容都在同一个程序包中,但测试器类必须在不同的程序包中时,它运行良好。

In the second call of BObject constructor, you're passing an argument whereas in it's declaration in the class, it doesn't take any argument. 在BObject构造函数的第二次调用中,您传递了一个参数,而在类中的声明中,它没有任何参数。 Check that. 检查一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM