Access permission refers to whether to run the role class and its members. It is implemented through an access controller, including the class access controller and the member access controller.
1. Class Access Controller
Class access controllers include public and default.
Public Access Controller:
Public class person {
...
}
Default access controller:
Class person (){
...
}
The class modified by public can be accessed anywhere, and the default access control operator is used only in the same package. See the following code:
---------------- Source file: A. Java -------------------------
Package CH11;
Public Class {
...
}
---------------- Source file: B. Java -------------------------
Package CH11;
Class B {
...
}
---------------- Source file: Test. Java -------------------------
Package CH11;
Public class test {
Public static void main (string [] ARGs ){
A A = new A (); // correct, public run access
B = new B (); // correct. access from the same package is allowed.
}
}
---------------- Source file: Test. Java under the ch12 package -------------------------
Package ch12;
Import ch11.a;
Import ch11. B; // error. Different packages cannot be accessed.
Public class test {
Public static void main (string [] ARGs ){
A A = new A (); // correct
B = new B (); // error. Different packages cannot be accessed.
}
}
Generally, access control operators of a class are written as public. If you want to restrict access to them, you can use the default access control operators.
2. Access Type Division
When we discuss access permissions, we will say that external classes are allowed to access, subclass access is allowed, and class access in the same package is allowed. What do these accesses mean? This is both an access type. Only by understanding these access types can access permissions be understood.
First, we need to know where access occurs. Access is actually an access to a member of an object (sometimes a class). The accessed code must be in a method. The access type is actually the relationship between the Code to be accessed and the location of the accessed code. These access types are analyzed.
The following example shows the access type in Step 4:
Package CH11;
Public Class {
Public String ID;
Public String GETID (){
Return ID; // access the member variable ID within the class, which is actually the method of the class.
}
}
Package CH11;
Public Class B {
Public String getinf (){
Return A. ID; // a member of Class A in Class B of the same package
}
}
Package CH11;
Public Class C extends {
Public String GETID (){
Return ID; // subclass directly calls the method or attribute of the parent class
}
}
Package ch12;
Public Class D {
Public String getinf (){
Return A. ID; // a member of method category Class A in Class D of different packages
}
}
3. access controllers of members
There are four access control operators for Members: public default protected and private.
If the access control character is public, you can perform the following four operations.
If the access control operator is the default, the first case is acceptable.
If the access control operator is protected, the first case is acceptable, and the subclass case is also acceptable.
If the access controller is private, only the first case is allowed.
The following table shows whether access is allowed:
| |
Class itself |
Subclass |
Same package |
Non-subclass of different packages |
| Public |
OK |
OK |
OK |
OK |
| Default |
OK |
Same package OK |
OK |
|
| Protected |
OK |
OK |
OK |
|
| Private |
OK |
|
|
|
The following example shows various situations:
Package CH11; public class A {public
IntI _public; protected
IntI _protected;
IntI _default; private
IntI _private; // method of the class, which can access all types of member variables public void F () {I _public = 10; I _protected = 20; I _default = 30; I _private = 40 ;}} package CH11; public class B _same_package {// Private Public void f (a A) {. I _public = 10;. I _default = 20;. I _protected = 30 ;}} package CH11; public class child_same_package extends a {// cannot access the private member public void F () {I _public = 10; I _protected = 20; I _default = 30 ;}} package ch12; import ch11.a; public class B _different_package {// different packages can only access public void f (a A) {. I _public = 10 ;}} package ch12; import ch11.a; public class child_different_package extends a {// different steamed stuffed bun classes can access public and protected members public void F () {I _public = 10; I _protected = 20 ;}}
Next we will introduce the inheritance of member variables.
Last time: 34th lecture package next time: 36th lecture member variable inheritance and coverage Li xucheng csdn blog: Why? U= 124362 & C = 7be8ba2b6f3b6cc5