.Java (access specifiers)
Java public、protect、friendly、private , 。
1. friendly
1) class , friendly。 package classes friendly , package classes private。
2) 、 package classes,Java classes default package, class friendly 。 class , package, default package。
class Sundae{
// friendly
Sundae(){}
Void f() {System.out.println(“Sundae.f()”);
}
public class IceCream{
public static void main(String[] args){
Sundae x = new Sundae();
x.f();
}
}
2. public: class
3. private:private class , :
class Sundae{
private Sundae(){}// Sundae class
Sundae(int i) {}
static Sundae makASundae() {
return new Sundae();
}
}
public class IceCream{
public static void main(String[] args){
// Sundae class Sundae() private,
//
//Sundae x = new Sundae();
Sundae y = new Sundae(1);//Sundae(int) friendly,
Sundae z = Sundae.makASundae();
}
}
4. protected: friendly , subclass( , ) 。 , package classes , protected class subclass 。
.Class
1.Class public、protect、friendly、private :
1)public:
2)protect、private: , class , class
protected private(inner class )
3) friendly: package classes
2. private class
1) static
2) Singteton
class Soup{
private Soup(){}
//(1)
public static Soup makeSout(){
return new Soup();
}
//(2)The "Singleton" pattern:
private static Soup ps1 = new Soup();
public static Soup access(){
return ps1;
}
public void f(String msg){
System.out.println("f(" + msg + ")");
}
}
public class Lunch{
public static void main(String[] args){
//Soup priv1 = new Soup();
Soup priv2 = Soup.makeSout();
Soup priv3 = Soup.access();
priv2.f("priv2");
priv3.f("priv3");
}
:http://zhidao.baidu.com/question/126069870.html