some fragment of thinking in java part3

1.Package: the library unit

a package contains a group of classes. organized togeother under a single namespace. if you're planning to create libraries or programs that are friendly to other Java programs on the same machine, must think about preventing class name clashes.

if you use a package statement, it must appear as the first non-comment in the file.

it's worth keeping in mind that the package and import keywords allow you to do, as a library designer, is to divide up the single global namespace so you won't have clashing names.

2. Java accesss specifiers

if you don't provide an access spcifiers,it means "package access"(sometimes "friendly").

public: interface access               

private: you can't touch that, the private keyword means that no one can access that nunber except the class that contains that number

    any method that you are certain is only a "helper" method for that class can be made private.

    a reference to an object is private inside a class does not mean that some other objcet can't have a public reference to the same object.

protected: inheritance access, protected keyword deals with a concept called inheritance.

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

3. Class access

there can be only one public class per compilation unit(file), the name of the public class must be completely match the name of the file containing the compilation unit, it is possible, though not typical, to have a compilation unit with not public class at all, In this case,you can name the file what ever you like

not that a class cannot be private(that would make it  inaccessible to anyone but the class) or protected, So you have only two choices for class access:package access or public. if you don't want anyone else to have access to that class, you can make all constructors private, thereby preventing anyone but you, inside a static member of the class, from creating an object of that class

class Soup1 {

  private Soup1() {}

  public static Soup1 makeSoup() {

    return new Soup1();  

  }

}

class Soup2 {

  private Soup2() {}

  private static Soup2 ps1 = new Soup2(); // the singleton pattern

  public static Soup2 access() {

    return ps1;

  }

  public void f() {}

}

// only one public class allowed per file

public class Lunch {

  void testPrivate() {

    // can't do this! private constructor;

    //! Soup1 soup = new Soup1();

  }

  void testStatic() {

    Soup1 soup = Soup1.makeSoup;

  }

  void testSingleton() {

    Soup2.access().f();

  }

}

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.

4.Summary

the first is to keep the users' hands off portions that they shouldn't touch. These pieces are neccessary for the internal operation of the class, but not part of interface that the client programmer needs  

if you don't get the interface right the first time, you can add more methods, as long as  you don't  remove any that client programmers have  already used in their code.

some situations have a different kind of communication, and rigid adherence to access rules may not be optimal.  

posted on 2015-10-22 21:22  terminator-LLH  阅读(324)  评论(0编辑  收藏  举报

导航