简体   繁体   English

父 package class 可从 java 中的子包 class 访问?

[英]parent package class accessible from child packge class in java?

In java parent package class accessible from child packge class?在 java 父 package class 可从子包 class 访问? please explain me any one?请给我解释一下吗?

example package A.A1.A2 contains class sub package A contains class sup例如 package A.A1.A2 包含 class sub package A 包含 class 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 包含 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; output:编译时错误:package 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.对于从不同目录导入 class,我们使用导入语句,但在这里我们需要从子包访问父级 package。我尝试导入它不起作用,请举例说明。

Java does not recognize the notion of a subpackage 1 . Java无法识别子包1的概念。 As far as Java is concerned packages a and ab and abc are unrelated. 就Java而言,包aababc是无关的。 They are just names. 它们只是名字。

So, if you want to access abSomeClass from abcSomeOtherClass , you must either use a fully qualified class name, or add an import to SomeOtherClass 因此,如果要从abcSomeOtherClass访问abSomeClass ,则必须使用完全限定的类名,或者向SomeOtherClass添加import

1 - From Java 9 onwards you can use modules to implement abstraction boundaries that are larger than a single package. 1 - 从Java 9开始,您可以使用模块来实现比单个包更大的抽象边界。 This doesn't address this question which is about package-private access, but it could be viewed as an alternative to package-private . 这并没有解决这个关于包私有访问的问题,但它可以被视为package-private的替代方案


As for your example that doesn't compile, I think we need a proper MCVE to understand that. 至于你没有编译的例子,我认为我们需要一个合适的MCVE来理解它。 My guess is that you have gotten the file organization for your source tree wrong ... 我的猜测是你的源代码树的文件组织错了......

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 package doesn't exist, like you said it does. 问题还在于您尝试导入的类的可见性是错误的(包私有),但这不会导致编译器说不存在,就像您说的那样。

In java parent package class accessible from child packge class? 在java子包类中可以从子包类访问? please explain me any one? 请解释我任何一个?

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.*不允许您访问所有子包名称 - 包不像目录那样是分层的。

All answers seem to miss OP's point on package class as everyone seem to suggest importing the class as a workaround. 所有答案似乎都错过了OP在包类上的观点,因为每个人似乎都建议将该类作为解决方法导入。

The answer is: package-level classes (ie, without explicit access level modifier), are visible ONLY for the EXACT same package. 答案是:包级别类(即没有显式访问级别修饰符)仅对EXACT相同的包可见。

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. 这实际上意味着父/子/外部包都不能查看该类。

只需导入它:

import A.sup;

Yes I have encountered the same error whenever I tried to access a class in the same package of the source file or the parent package of the source file it is generating a compiled time error, so by trial and error method I came to the conclusion that the packages are not built in a way to support main methods in the classes and they are not built in a way to support importing their parent packages or child packages是的,每当我尝试访问源文件的同一个 package 或源文件的父 package 中的 class 时,我都会遇到相同的错误,它会生成编译时错误,因此通过反复试验,我得出的结论是这些包不是以支持类中主要方法的方式构建的,也不是以支持导入其父包或子包的方式构建的

In object of abcSomeOtherClass: 在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");

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

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

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