简体   繁体   English

同一个包中的类

[英]Classes in same package

I love the Intellij IDEA but i have been stacked on one little problem with Java imports. 我喜欢Intellij IDEA,但我已经堆积在Java导入的一个小问题上。 So for example there is a package with name "example" and two different classes in it: A.java and B.java. 例如,有一个名为“example”的包,其中包含两个不同的类:A.java和B.java。

And i wanna get access to class "A" from class "B" without imports. 我想从“B”级进入“A”级而无需进口。 Like this: 像这样:

class A: A类:

package example;

public class A{ ... some stuff here ...}

class B: B级:

package example;

public class B{
    public static void main(String[] args){
        A myVar = new A();
    }
}

This code may not work, but it's doesn't matter. 此代码可能无效,但无关紧要。 Trouble just with IDE and with its mechanism of importing classes. 使用IDE及其导入类的机制很麻烦。

So, problem is that i can't see A class from B. Idea says 'Cant resolve symbol' but i actually know that class A exists in package. 所以,问题是我看不到来自B的A类。想法说'不能解决符号',但实际上我知道A类存在于包中。 Next strange is that complier works fine and there are no exceptions. 下一个奇怪的是编译器工作正常,没有例外。 Just IDEA can't see the class in the same package. 只是IDEA无法在同一个包中看到该类。

Does anybody has any ideas? 有人有什么想法吗?

If they are in the same package, you can access A in class B without import: 如果它们在同一个包中,您可以在B类中访问A而无需导入:

package example;

public class B{
    public static void main(String[] args){
        A myA = new A();
    }
}

Maybe this will help you, or somebody else using IntelliJ that is getting a "Cannot resolve symbol" error but can still compile their code. 也许这会帮助你,或者其他人使用IntelliJ获得“无法解决符号”错误,但仍然可以编译他们的代码。

Lets say you have the two files that buymypies wrote up, the standard Java convention is that the two files would exist in an Example directory in your source code area, like /myprojectpath/src/Example . 假设你有两个buymypies写的文件,标准的Java约定是两个文件存在于源代码区的Example目录中,比如/myprojectpath/src/Example But it is technically not a requirement to reflect the package structure in the source directory structure, just a best practice sort of thing. 但从技术上讲,不需要在源目录结构中反映包结构,这只是一种最佳实践。

So, if you don't mimic the package structure, and the two files are just in /myprojectpath/src , IntelliJ will give you the "Cannot resolve symbol" error because it expects the source code structure to reflect the package structure, but it will compile okay. 因此,如果您不模仿包结构,并且这两个文件只在/myprojectpath/src ,IntelliJ将为您提供“无法解析符号”错误,因为它希望源代码结构能够反映包结构,但它会编译好的。

I'm not sure if this is your problem, but I do use IntelliJ and have seen this, so it's something to look at. 我不确定这是不是你的问题,但是我确实使用了IntelliJ并且看过这个,所以这是值得关注的。

I have the same problem as this: 2 classes in the same package, yet when one tries to call the other, Intellij underlines it in red and says Cannot resolve symbol 'Classname', eg Cannot resolve symbol 'LocalPreferencesStore'. 我有同样的问题:在同一个包中有2个类,但当一个人试图调用另一个时,Intellij用红色表示它并且说无法解析符号'Classname',例如无法解析符号'LocalPreferencesStore'。

It then wants to add the fully qualified name in situ - so it clearly knows the path - so why can't it just access the class? 然后它想要在原位添加完全限定名称 - 所以它清楚地知道路径 - 所以为什么它不能只访问该类?

The module still compiles and runs, so it's just the IDE behaving oddly - and all that red is very distracting, since it isn't actually an error, it's just IDEA throwing a weird wobbly. 该模块仍然编译和运行,所以它只是IDE表现得很奇怪 - 所有红色都非常分散注意力,因为它实际上并不是一个错误,它只是IDEA投掷一个奇怪的摇摆。

This is also recent. 这也是最近的。 Two weeks ago I wasn't having this problem at all, and now suddenly it's started up. 两个星期前,我根本没有遇到这个问题,现在突然间它开始了。 Of course, it could go away again on its own soon, but it's really annoying. 当然,它可能很快就会自行消失,但它真的很烦人。

Same issue and I just fixed it. 同样的问题,我刚刚解决了。

I don't know your folder structure. 我不知道你的文件夹结构。 However, if your package example was added manually.That's the problem. 但是,如果您的package example是手动添加的。那就是问题所在。 The package should be the same as your folder structure,which means if you want your class file to be stored in package example ,you must store you java file in the src's subfolder named example. 包应该与文件夹结构相同,这意味着如果您希望将类文件存储在package example ,则必须将java文件存储在src的名为example的子文件夹中。

Please ensure that both A.java and B.java are in the same package. 请确保A.java和B.java都在同一个包中。 Since Both are in same package, no need to specify package example; 由于两者都在同一个包中,因此无需指定package example;

Class A: A类:

public class A{ ... some stuff here ...}

Class B: B级:

public class B{
    public static void main(String[] args){
        A myVar = new A();
    }
}

You need to learn the basics about Java i think. 你需要学习关于Java的基础知识。

Here is a basic example of what i think you are trying: 以下是我认为您正在尝试的基本示例:

package Example;

public class A
{
    String myVar;

    public String getMyVar()
    {
        return myVar;
    }

    public void setMyVar(String myVar)
    {
        this.myVar = myVar;
    }
}

You need to create an instance of A. 您需要创建一个A的实例。

package Example;

public class B
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        A myA = new A();

        myA.setMyVar("Hello World!");

        System.out.println(myA.getMyVar);
    }

}

Look up java 'getters' and 'setters'. 查找java'getters'和'setters'。

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

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