Chapter 6 Access Rights Control

6.1 Packages: Library Units

import keyword, if you want to import multiple classes from a package, use *

6.1.1 Code Organization

If the name of the file is MyClass.java, this means that there is only one public class in the file, and the name of the class must be MyClass

6.1.2 Creating Unique Package Names

Generally, the domain name is written in reverse as the package name

6.2 Java Access Qualifiers

6.2.1 Package access rights

The permission to write nothing is the package access permission. By default, only the classes under one package can access each other, and other packages cannot access.

6.2.2 public: interface access rights

Can be accessed by importing

6.2.3 private: you cannot access

This member cannot be accessed by any class other than the class that contains the member.

The constructor uses private to prevent others from creating objects through new, and the class cannot be inherited

6.2.4 protected: inherited access rights

protected contains package access rights,

6.3 Interface and Implementation

Access control is often referred to as implementation-hiding. The wrapping of data and methods into classes, and the hiding of concrete implementations, is often referred to as encapsulation. For clarity and ease of reading, it is generally in the order of public, protected, package access, and private of a class.

6.4 Class Access

  1. There can only be one public class per compilation unit.
  2. The name of the public class must exactly match the name of the file containing the compilation unit, including case.
  3. Although not very commonly used, it is possible to have no public classes in a compilation unit at all.

A class cannot be private or protected (unless it is an inner class). There are only two options for class access: package access or public

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325951139&siteId=291194637