第五章: Hiding the Implementation

来源:互联网 发布:js 合计不能超过数字 编辑:程序博客网 时间:2024/05/11 12:16

第五章: Hiding the Implementation

1package: the library unit

package mypackage;

Note that the convention for Java package names is to use all lowercase letters, even for intermediate words.

Creating unique package names: 

By convention, the first part of the package name is the reversed Internet domain name of the creator of the class. Since Internet domain names are guaranteed to be unique, if you follow this convention, your package name will be unique and you’ll never have a name clash.

When you see the package name com.bruceeckel.simple, but what about the first portion of the path? That’s taken care of in the CLASSPATH environment variable, as: CLASSPATH=.;D:/JAVA/LIB;C:/DOC/JavaT or CLASSPATH=.;D:/JAVA/LIB;C:/flavors/grape.jar (when using JAR files)

2Java access specifiers(java的访问控制符)

Package access:-- It means that all the other classes in the current package have access to that member, but to all the classes outside of this package, the member appears to be private. a package-access member is inaccessible to the client programmer using the class.

public: interface access --it means that the member declaration that immediately follows public is available to everyone, in particular to the client programmer who uses the library.

private: you can’t touch that!—it means that no one can access that member except the class that contains that member, inside methods of that class.示例如下:

class Sundae {

  private Sundae() {}

  static Sundae makeASundae() {

    return new Sundae();

  }

}

public class IceCream {

  public static void main(String[] args) {

    //! Sundae x = new Sundae();

    Sundae x = Sundae.makeASundae();

  }

} ///:~

protected: inheritance access--protected also gives package access—that is, other classes in the same package may access protected elements

3Interface and implementation (接口与实现)

Access control puts boundaries within a data type for two important reasons--The first is to establish what the client programmers can and can’t use.second, you are free to change anything that’s not public (e.g., package access, protected, or private) without breaking client code

For clarity, you might prefer a style of creating classes that puts the public members at the beginning, followed by the protected, package access, and private members. The advantage is that the user of the class can then read down from the top and see first what’s important to them

4Class access(类访问权限):

if you don’t put an access specifier for class access, it defaults to package access. This means that an object of that class can be created by any other class in the package, but not outside the package. However, if a static member of that class is public, the client programmer can still access that static member even though they cannot create an object of that class

only two choices for class access: package access or public.类不能做成private的,如果你不希望别人访问这个类,你可以将它的构造函数做成private的,而你可以使用static的方法创建对象.

class Soup {

  private Soup() {}

  // (1) Allow creation via static method:

  public static Soup makeSoup() {

    return new Soup();

  }

  // (2) Create a static object and return a reference

  // upon request.(The "Singleton" pattern):只允许你创建一个这个类型的对象

  private static Soup ps1 = new Soup();

  public static Soup access() {

    return ps1;

  }

  public void f() {}

}

// Only one public class allowed per file:

public class Lunch {

  void test() {

    // Can't do this! Private constructor:

    //! Soup priv1 = new Soup();

    Soup priv2 = Soup.makeSoup();

    Soup.access().f();

  }

} ///:~