Java 包级别访问 [英] Java Package level access
问题描述
我知道可以在包级别访问具有默认访问控制的类成员,但我对包级别访问的实际含义感到困惑.如果可以在包级别访问默认成员,那么我不应该在以下示例中的 Test2 类中可见吗?1级-
I know that class members with default access control can be accessible at package level but i'm confused about what does package level access actually mean. If default members can be accessed at package level then shouldn't i be visible in class Test2 in following example? class 1-
package pkg1;
public class Test {
int i=0;
}
类 2-
import pkg1.Test;
public class Test2 {
void get(){
Test t = new Test();
t.i=0;
}
}
请帮我理解这个概念.提前致谢.
Please help me getting this concept. Thanks in advance.
推荐答案
包级访问意味着只有在同一包中定义的类才能访问包级变量.如果您必须导入 Test
,那么我假设 Test
位于不同的包中,因此它无法访问 i
.
Package level access means that only classes that are defined in the same package can access the package level variable. If you have to import Test
, then I'm assuming that Test
is in a different package and therefore it can't access i
.
对于Test2
访问i
,定义在与Test1
相同的包中.
For Test2
to access i
, define it in the same package as Test1
.
这篇关于Java 包级别访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!