Java access control

Java provides four access rights:

Public > protected > package access rights (no keywords) > private

Packages: library units

For package access permission, all within a package can be accessed.

When creating a class in eclipse, if you do not create a package, the class will be created in the default package. I have always been used to this, but this is a very bad practice (= =!) Unrelated classes should be placed in different packages. Because if there are two classes with the same name in a package, there will be a conflict.

Put different classes into different packages. If you want to use classes from other packages, you need to add the package name before the name. For example, the ArrayList class is in Java In the util directory, it will be written as Java util. ArrayList。 The name is verbose, so Java provides the import keyword to import the package.

import java. util. ArrayList; Then you can use ArrayList directly in the file. If you import Java util.*; Import into Java All classes under util.

If there is an import package, there are packaging and export packages.

Packaging is implemented through the keyword package, which must be the first sentence of the file except comments. The format is package packagename; Package names should be unique, so they are generally in the reverse order of domain names.

The Java interpreter needs to find out the environment variable classpath. We can write our own class library and reference it. However, I didn't do it for two hours. I gave up this temporarily. I can directly use eclipse to package it into jars (right-click in the engineering office and select export), and then I can reference my own class library after importing (right-click properties -- > java build path -- > add external jars).

Via import XXX MyClass (XXX is the path) can import classes. For static methods in classes, you can import static xxx.myclass.staticmethod; you can import static methods, and you can directly use staticmethod () in files without class name. And import static XXX myclass.*; All static methods can be imported. Note: can only be used for static methods.

Access rights

For members in a class:

Public access is available to everyone.

When protected inherits, subclasses can access and have package access rights.

Package access permission, a package can be accessed.

Private is accessible only to members of the class.

For a class:

We all know that a file can only have one public class (or none). The class name should be the same as the file name, so other classes have package access rights. Classes do not have private and protected (there are special cases in internal classes). Classes with package access rights are invisible to other packages.

Summary

Providing access has two purposes:

1. For class users, it can prevent them from touching things that should not be touched, and simplify the understanding of the class.

2. Isolate and protect the interface and implementation, and change the internal design of the class without significant impact on the user of the class.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>