父包类可以从java中的子包类访问? [英] parent package class accessible from child packge class in java?

查看:1122
本文介绍了父包类可以从java中的子包类访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

无论如何从sub访问sup?

Is there anyway to access sup from sub?

请解释。

我尝试导入它将无法工作
示例:
之前程序目录结构是
package1包含package1.java - > package2 - > package3包含PCheck.java

i tried import it won't work Example: before the program Directory Structure is package1 contains 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");
    }
}

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

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

从不同目录导入类我们使用import语句但是这里我们需要从subpackage.i访问父包尝试导入它不会work pls用一个例子解释。

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.

推荐答案

Java无法识别子包的概念。至于Java涉及的包 a ab abc 是无关的。它们只是名字。

Java does not recognize the notion of a subpackage. As far as Java is concerned packages a and a.b and a.b.c are unrelated. They are just names.

因此,如果你想从 abc访问 abSomeClass SomeOtherClass ,您必须使用完全限定的类名,或者将 import 添加到 SomeeOtherClass

So, if you want to access a.b.SomeClass from a.b.c.SomeOtherClass, you must either use a fully qualified class name, or add an import to SomeeOtherClass

这篇关于父包类可以从java中的子包类访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆