Java programming ideas --- Chapter VI Access Control (lower)

Chapter VI Access Control (lower)

6.2 Java access modifiers

 

  public, protected, private access to these Java modifiers, when used, is placed before the definition of each class of each member, whether it is a city or a method, each access modifier only control it access to the modified specific definition. If you do not provide any access modifier, it means that it is the package access.

 

6.2.1 package access

 

  Default access has no keyword, but it usually refers to the package access, all the other classes have access to the members of the authority which means that the current package, but for all the classes outside of this package, the member is private . Since a compilation unit can only belong to one package, all the packages via access between each other in all classes they are accessible in the same compilation unit.

  Way to access a member of the take are:

    1, so that the members of the public

    2, through the qualifiers without access to other classes and placed in the way members of the same package to package gives access

    3, inheritance

    4, provides access to devices and variation (getter and the setter) to read and change the value.

 

6.2.2 public: Interface Access

 

  as follows:

public class Cookie {
  public Cookie() {
    System.out.println(“Cookie constructor”);
  }

  void bite() {
    System.out.println(“bite”);
  }
}

 

 

  Now if you create a program that uses Cookie:

Cookie x = new Cookie();

//! x.bite();

 

  The first sentence of code can be executed, while the second sentence will be error, because its constructor is public and the class is public, but due to the bite () method provides access only to the class in the package, so the bite () member here it is unable to access.

 

6.2.3 private: You can not access

 

  Keyword private means, in addition to containing the class members, other any class can access this member, as in the other classes is not possible to access private members in the same package, so this is tantamount to their own isolation . The default package access often already provided sufficient measures to hide the client programmer can not access the class members.

  Here is an example:

class Sundae {
    private Sundae() {}

    static Sundae makeASundae() {
        return new Sundae();
    }
}

public class IceCream {
    public static void main(String[] args) { Sundae x = Sundae.makeASundae(); } }

 

  This is a note there are examples of private arena, you can prevent others from directly accessing a particular constructor, can not directly create a Sundae object via the constructor, you must use makeASundae () method to reach this goal.

 

6.2.4 protected: inheritance access

 

  Keyword protected process is the concept of inheritance, inheritance may be utilized by an existing class (a base class), and then add new members to the existing classes without having to touch the existing class. In order to inherit from an existing class, we declare a new class extends (extend) an existing class:

class Foo extends Bar {  }

 

  If you create a new package, and inherited class from another package, the only members can access the package is the source of public members. Sometimes the base class creators will want to have a specific member of the access privileges granted to its derived class instead of all classes, which requires protected to complete this work. protected also provides package access, which means that other classes can access protected elements within the same package.

 

6.3 interface and implementation

 

  Access control is typically referred to collectively achieve Hide, the data and methods in the class into the package, and hide implementation, often referred to collectively packaged, the result is a data type with characteristics and behaviors of colleagues.

  For two very important reasons, access to the border control authority will draw in the internal data type. The first reason is to set boundaries Can the client programmer to use. The second reason is that the interfaces and specific separation, if the structure is used in a set of programs, and the client programmer can do in addition to what can not send messages to the outside of the interface, then you are free to change all not a public thing.

 

Access 6.4 class

 

  In Java, the access modifier can also be used to determine which class library for users of the library are available, if you want a class can be used as a client programmer, you can put key public role domain to achieve the purpose of the entire class.

  In order to control access to a class, the modifier must appear before the keyword class, so you can declare like this:

public class Widget {

 

  Here are some additional restrictions:

    1, each compilation unit can have only one public class, which means that each compilation unit has a single common interface with the public to show the class, the interface can contain a number of required support package access class, if in more than one public class within a compilation unit, the compiler will give an error message.

    2, the name of the class must be public completely containing the coding unit matches the file name, including case. For Widget, the file name must be Widg.java.

    3, although less common, but the complete compilation unit with no public class also possible, free to the file name in this case.

 

  Class is not private, and it is not protected, so for access to the class, only two options: package access or public. If you do not want anyone else to have access to the classes, you can put all the constructors are designated as private to prevent anyone to create objects of that class. But you can create within the static members of the class.

Guess you like

Origin www.cnblogs.com/parable/p/11448647.html