DAY03-----Access Specifiers in the Java language
First, an overview of the access specifier:
The access specifier is actually set up an access permission, only to set up this access right later to better encapsulate some of our variables or methods. So learning this specifier is very helpful for us to follow up on Java's encapsulation function.
Second, the access specifier:
1. What are the access specifiers?
In the Java language, an access specifier can implement a wrapper on a class member, with 4 specifiers, public, private, default, protected. Here's a one by one introduction.
1.1, Public: Open. can be accessed by any class, or by a class within a different package.
1.2, Private: Confidential. This member can only be accessed by the current class, and other classes are not allowed. Different packages can not be accessed
1.3, Protected: protected. The classes in the same package can be accessed, and only subclasses can access them in different packages, and inheritance can be accessed.
1.4. Default: Defaults. The classes inside the same package can be accessed.
2. The large order of access permissions:
Public--"protected"--"default" private
Public Private can modify a class, and the other 2 cannot be decorated.
3, an example to illustrate the 4 kinds of descriptors.
Create a Student class
Package www.com; public class Student {publicly string number;//: string username;//default: PRI vate int age;//Confidential: protected char gender; Protected: public void Display () {//can also be modified directly using the specifier. System.out.println ("Study No.:" +number); System.out.println ("Name:" +username); System.out.println ("Age:" +age); System.out.println ("Gender:" +gender); } }
Create a Userstudent class
Package www.dzx.one; public class Userstudent {public static void main (string[] args) {//Create instance: Student stu = new Student ();//Assignment Stu.number = "003"; Stu.username = "ZS"; Stu.age = 24;//This is not set because we previously set the private permission Stu.gender = ' M ';//Call Stu.display (); }}
Run the Userstudent class, you can see the assignment, and here you can obviously feel the access rights
Third, concluding remarks:
Come here. Our access modifiers are finished, so go ahead and use some of these specifiers! It's very important.
This article from "Lonely One Night" blog, reproduced please contact the author!
---Access specifiers in the Java language