package
com.zj.main;
public
class
Out {
public
PrivateIn getPrivateIn(){
return
new
PrivateIn();
}
public
DefaultIn getDefaultIn(){
return
new
DefaultIn();
}
public
ProtectedIn getProtectedIn(){
return
new
ProtectedIn();
}
public
PublicIn getPublicIn(){
return
new
PublicIn();
}
private
class
PrivateIn
implements
InMethod{
private
int
private_arg
;
int
default_arg
;
protected
int
protected_arg
;
public
int
public_arg
;
private
void
private_method(){};
void
default_method(){};
protected
void
protected_method(){};
public
void
public_method(){};
}
class
DefaultIn
implements
InMethod{
private
int
private_arg
;
int
default_arg
;
protected
int
protected_arg
;
public
int
public_arg
;
private
void
private_method(){};
void
default_method(){};
protected
void
protected_method(){};
public
void
public_method(){};
}
protected
class
ProtectedIn
implements
InMethod{
private
int
private_arg
;
int
default_arg
;
protected
int
protected_arg
;
public
int
public_arg
;
private
void
private_method(){};
void
default_method(){};
protected
void
protected_method(){};
public
void
public_method(){};
}
public
class
PublicIn
implements
InMethod{
private
int
private_arg
;
int
default_arg
;
protected
int
protected_arg
;
public
int
public_arg
;
private
void
private_method(){};
void
default_method(){};
protected
void
protected_method(){};
public
void
public_method(){};
}
public
static
void
main(String[] args){
//create an outer object
Out out=
new
Out();
//create a private inner object by 'new'
Out.PrivateIn privateIn=out.
new
PrivateIn();
privateIn.
private_arg
=0;
privateIn.private_method();
// create a private inner object by 'out's method'
Out.PrivateIn privateIn2 = out.getPrivateIn();
privateIn2.
private_arg
= 0;
privateIn2.private_method();
}
}
|
package
com.zj.main;
public
class
SamePackage {
public
static
void
main(String[] args) {
// create an outer object
Out out =
new
Out();
//create a private inner object by 'new'
//Out.PrivateIn privateIn=out.new PrivateIn();
//->error: Out.PrivateIn is not visible.
// create a default inner object by 'new'
Out.DefaultIn defaultIn = out.
new
DefaultIn();
//defaultIn.private_arg=0;->error:not visible
defaultIn.
default_arg
= 0;
//defaultIn.private_method();->error:not visible
defaultIn.default_method();
// create a private inner object by 'out's method'
//Out.PrivateIn privateIn2 = out.getPrivateIn();
//->error:Out.PrivateIn is not visible through out.getPrivateIn() is visible.
// create a private inner object by 'out's method',
// but use Interface reference to handle it
InMethod privateIn=out.getPrivateIn();
privateIn.public_method();
}
}
|
public
interface
InMethod {
void
public_method();
}
|
package
com.zj.other;
import
com.zj.main.InMethod;
import
com.zj.main.Out;
public
class
DifferPackage {
public
static
void
main(String[] args){
//create an outer object
Out out=
new
Out();
//create a public inner object by 'new'
Out.PublicIn publicIn=out.
new
PublicIn();
publicIn.
public_arg
=0;
publicIn.public_method();
// create a public inner object by 'out's method'
Out.PublicIn publicIn2 = out.getPublicIn();
publicIn2.
public_arg
=0;
publicIn2.public_method();
//use Interface reference
InMethod method;
method=out.getPrivateIn();
method.public_method();
method=out.getDefaultIn();
method.public_method();
method=out.getProtectedIn();
method.public_method();
method=out.getPublicIn();
method.public_method();
}
}
|
package
com.zj.other;
import
com.zj.main.Out;
public
class
DifferPackageAndExtend
extends
Out{
public
static
void
main(String[] args){
//create an DifferPackageAndExtend's object,which extends Out
Out extend=
new
DifferPackageAndExtend();
//create a protected inner object by 'new'
//Out.ProtectedIn protectedIn=extend.new ProtectedIn();
//->error:The constructor Out.ProtectedIn() is not visible
// create a protected inner object by 'out's method'
Out.ProtectedIn protectedIn=extend.getProtectedIn();
protectedIn.
public_arg
=0;
protectedIn.public_method();
}
}
|