package access.dessert;
public class Cookie {
public Cookie(){
System.out.println("Cookie constructor");
}
void bite(){System.out.println("bite");}
}
//:access/Dinner.java
//Uses the library.
import access.dessert.*;
public class Dinner {
public static void main(String[] args) {
Cookie x = new Cookie();
//! x.bite();//Can't access
}
}
输出结果:
Cookie constructor
(注意看以上代码内注释掉的部分)
The default package
You might surprised to discover that the following code compiles,even though it would appear that it breaks the rules:
//:access/Cake.java
//Access a class in a separate compilation unit.
class Cake{
public static void main(String[] args){
Pie x = new Pie();
x.f();
}
}
输出结果:
Pie.f()
In a second file in the same directory:
//:access/Pie.java
//The other class.
class Pie{
void f(){System.out.println("Pie.f()");}
}
private:you can't touch that!
The private keyword means that no one can access that member except the class that contains that member,inside methods of that class.
private allows you to freely change that member without concern that it will affect another class in the same package.
举例:
//:access/IceCream.java
//Demonstrates "private" keyword.
class Sundae{
private Sundae(){}
static Sundae makeASundae(){
return new Sudnae();
}
}
public class IceCream{
public static void main(String[] args){
//!Sundae x = new Sundae();
Sundae x = Sundae.makeASundae();
}
}
This shows an example in which private comes in dandy:You might want to control how an object is created and prevent someone from directly accessing a particular constructor(or all of them).In the preceding example,you cannot create a Sundae object via its constructor;instead,you must call the makeASundae() method to do it for you.
Unless you must expose the underlying implementation(which is less likely than you might think),you should make all fields private.
protected:inheritance access
The protected keyword deals with a concept called inheritance.which takes an existing class-which we refer to as the base class-and adds new members to that class without touching the existing class.You can also change the behavior of existing members of the class.To inherit from a class,you say that your new class extends an existing class,like this:
class Foo extends Bar{
The rest of the class definition looks the same.
If you create a new package and inherit from a class in another package,the only members you have access to are the public members of the original package.(Of course,if you perform the inheritance in the same package,you can manipulate all the members that have package access.)Sometimes the creator of the base class would like to take a particular member and grant access to derived classes but not the world in general.That's what protected does.protected also gives package access-that is,other classes in the same package may access protected elements.
If you refer back to the file Cookie.java,the following class cannot call the package-access member bite();
//:access/ChocolateChip.java
//Can't use package-access member from another package.
import access.dessert.*;
public class ChocolateChip extends Cookie{
public ChocolateChip(){
System.out.println("ChocolateChip constructor");
}
public void chomp(){
//!bite();//Can't access bite
}
public static void main(String[] args){
ChololateChip x = new ChocolateChip();
x.chomp();
}
}
输出结果:
Cookie constructor
ChocolateChip constructor
One of the interesting things about inheritance is that if a method bite() exists in class Cookie,then it also exists in any class inherited from Cookie.But since bite() has package access and is in a foreign package,it's unavailable to us in this one .Of course,you could make it public,but then everyone would have access,and maybe that's not what you want.If you change the class Cookie as follows:
//:access/cookie2/Cookie.java
package access.cookie2;
public class Cookie{
public Cookie(){
System.out.println("Cookie constructor");
}
protected void bite(){
System.out.println("bite");
}
}
now bite() becomes accessible to anyone inheriting from Cookie:
//:access/ChocolateChip2.java
import access.cookie2.*;
public class ChocolateChip2 extends Cookie{
public ChocolateChip2(){
System.out.println("ChocolateChip2 constructor");
}
public void chomp(){bite();}//Protected method
public static void main(String[] args){
ChocolateChip2 x = new ChocolateChip2();
x.chomp();
}
}
输出结果:
Cookie constructor
ChocolateChip2 constructor
bite
(待续)