java java中的子包类可以访问父包类吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3564663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:26:22  来源:igfitidea点击:

parent package class accessible from child packge class in java?

java

提问by Vinoth Kumar

In java parent package class accessible from child packge class? please explain me any one?

在可从子包类访问的 java 父包类中?请解释我任何人?

example package A.A1.A2 contains class sub package A contains class sup

示例包 A.A1.A2 包含类子包 A 包含类 sup

Is there anyway to access sup from sub?

无论如何可以从 sub 访问 sup 吗?

pls explain.

请解释。

i tried import it won't work Example: before the program Directory Structure is package1 contains package1.java --> package2 --> package3 contains PCheck.java

我试过导入它不会工作例如:在程序目录结构之前 package1 包含 package1.java --> package2 --> package3 contains PCheck.java

//package1.java
package package1;
public class package1{
    public static void main(String[] args) {

    }
}
class phelo{
    phelo(){
        int a;
        System.out.println("hai fun freom package 1");
    }
}

//PCheck.java;
package package1.package2.package3;
import package1.*;   //to import package1.java
public class PCheck {
    public static void main(String[] args) {
        phelo obj=new phelo();
    }
}
class helo{
    helo(){
        int a;
        System.out.println("hai fun from package 3");
    }
}

output: compile time error:package package1.package2.package3 doesnot exist;

输出:编译时错误:包 package1.package2.package3 不存在;

for import class from different directory we use import statements but here we need access parent package from subpackage.i tried import it won't work pls explain with an example.

对于来自不同目录的导入类,我们使用导入语句,但在这里我们需要从子包访问父包。我尝试导入它不会起作用,请举例说明。

回答by Stephen C

Java does not recognize the notion of a subpackage1. As far as Java is concerned packages aand a.band a.b.care unrelated. They are just names.

Java 不承认子包的概念1。就 Java 而言,包aa.ba.b.c是无关的。他们只是名字。

So, if you want to access a.b.SomeClassfrom a.b.c.SomeOtherClass, you must either use a fully qualified class name, or add an importto SomeOtherClass

因此,如果要访问a.b.SomeClassfrom a.b.c.SomeOtherClass,则必须使用完全限定的类名,或添加importSomeOtherClass

1 - From Java 9 onwards you can use modules to implement abstraction boundaries that are larger than a single package. This doesn't address thisquestion which is about package-privateaccess, but it could be viewed as an alternative to package-private.

1 - 从 Java 9 开始,您可以使用模块来实现比单个包更大的抽象边界。这并没有解决这个关于package-private访问的问题,但它可以被视为package-private的替代方案



As for your example that doesn't compile, I think we need a proper MCVE to understand that. My guess is that you have gotten the file organization for your source tree wrong ...

至于您无法编译的示例,我认为我们需要适当的 MCVE 才能理解这一点。我的猜测是您的源代码树的文件组织错误......

It is also possible that the problem is that the visibility of the class you are trying to import is wrong (package private), but that wouldn't cause the compiler to say that the packagedoesn't exist, like you said it does.

问题也可能是您尝试导入的类的可见性错误(包私有),但这不会导致编译器说不存在,就像您说的那样。

回答by duffymo

In java parent package class accessible from child packge class? please explain me any one?

在可从子包类访问的 java 父包类中?请解释我任何人?

Not the way you're thinking. The directories are hierarchical, but the packages are just distinguishing names.

不是你想的那样。目录是分层的,但包只是区分名称。

If a child needs a parent package, or any other outside its hierarchy, it simply needs to import it.

如果子程序需要父程序包,或其层次结构之外的任何其他程序包,则只需导入它即可。

That's why import foo.*doesn't give you access to all sub-package names - packages aren't hierarchical the way directories are.

这就是为什么import foo.*不能让您访问所有子包名称的原因 - 包不像目录那样分层。

回答by DanielCuadra

All answers seem to miss OP's point on package classas everyone seem to suggest importing the class as a workaround.

所有答案似乎都忽略了 OP 在包类上的观点,因为每个人似乎都建议将类导入为解决方法。

The answer is:package-level classes (i.e., without explicit access level modifier), are visible ONLY for the EXACT same package.

答案是:包级类(即,没有显式访问级别修饰符),仅对完全相同的包可见。

https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

If a class has no modifier (the default, also known as package-private), it is visible only within its own package

如果一个类没有修饰符(默认,也称为包私有),它只在它自己的包内可见

This effectively means that neither parent/child/external packages can view the class.

这实际上意味着父/子/外部包都不能查看类。

回答by ExtremeCoder

Simply import it:

只需导入它:

import A.sup;

回答by diyism

In object of a.b.c.SomeOtherClass:

在 abcSomeOtherClass 的对象中:

List<String> tmp=new ArrayList<String>(Arrays.asList(this.getClass().getPackage().getName().split("\.")));
tmp.remove(tmp.size()-1);
String parent_package_name=tmp.toString().replace(", ", ".").replaceAll("[\[\]]", "");
Class cls=Class.forName(parent_package_name+".SomeClass");

回答by S K

default class can be only used within package exceptsubpackage

默认类只能在包内使用,子包除外