当前位置: 技术问答>java相关
请教关于modifier的问题。
来源: 互联网 发布时间:2015-11-10
本文导语: 请先看看程序: //file:/def/Q.java package def; public class Q { private int privateVar; int packageVar; protected int protectedVar; public int publicVar; } //file:Sub.java public class Sub extends Tester { } //file:Tester.java import def.Q; public class...
请先看看程序:
//file:/def/Q.java
package def;
public class Q
{
private int privateVar;
int packageVar;
protected int protectedVar;
public int publicVar;
}
//file:Sub.java
public class Sub extends Tester
{ }
//file:Tester.java
import def.Q;
public class Tester extends Q
{
void someMethod()
{
Q q = new Q();
Sub sub = new Sub();
// First, try to refer to q's memebers.
q.privateVar = 1; // compiler error
q.packageVar = 2; // compiler error
//(1):为什么q.protectedVar会编译错误??
q.protectedVar = 3; // compiler error
q.publicVar = 4; // fine
// Next, try to refer to this object's members
// supplied by class Q.
privateVar = 5; // compiler error
packageVar = 6; // compiler error
protectedVar = 7; // fine (2)
publicVar = 8; // fine
// Next, let's try to access the members of
// another instance of Tester.
Tester t = new Tester();
t.privateVar = 9; // compiler error
t.packageVar = 10; // compiler error
t.protectedVar = 11; // fine (3)
t.publicVar = 12; // fine
// Finally, try to refer to the members in a
// subclass of Tester.
sub.privateVar = 13; // compiler error
sub.packageVar = 14; // compiler error
sub.protectedVar = 15; // fine (4)
sub.publicVar = 16; // fine
}
}
我的问题是:
在(1)处为什么q.protectedVar会编译错误??
(1)和(2),(3),(4)有什么不同?
//file:/def/Q.java
package def;
public class Q
{
private int privateVar;
int packageVar;
protected int protectedVar;
public int publicVar;
}
//file:Sub.java
public class Sub extends Tester
{ }
//file:Tester.java
import def.Q;
public class Tester extends Q
{
void someMethod()
{
Q q = new Q();
Sub sub = new Sub();
// First, try to refer to q's memebers.
q.privateVar = 1; // compiler error
q.packageVar = 2; // compiler error
//(1):为什么q.protectedVar会编译错误??
q.protectedVar = 3; // compiler error
q.publicVar = 4; // fine
// Next, try to refer to this object's members
// supplied by class Q.
privateVar = 5; // compiler error
packageVar = 6; // compiler error
protectedVar = 7; // fine (2)
publicVar = 8; // fine
// Next, let's try to access the members of
// another instance of Tester.
Tester t = new Tester();
t.privateVar = 9; // compiler error
t.packageVar = 10; // compiler error
t.protectedVar = 11; // fine (3)
t.publicVar = 12; // fine
// Finally, try to refer to the members in a
// subclass of Tester.
sub.privateVar = 13; // compiler error
sub.packageVar = 14; // compiler error
sub.protectedVar = 15; // fine (4)
sub.publicVar = 16; // fine
}
}
我的问题是:
在(1)处为什么q.protectedVar会编译错误??
(1)和(2),(3),(4)有什么不同?
|
q.protectedVar 是Q上变量。Tester与Q不在同一包内,故对q.protectedVar没有存取权限,而Sub与Tester默认在同一包内,故可以访问他的变量如:
sub.protectedVar ,t 就更不用说了。
sub.protectedVar ,t 就更不用说了。
|
必须在同一包下
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。