java中访问控制符
Java访问说明符 (Java access specifiers)
We know that there are few access specifiers in java. We will explore access specifiers one by one. We will study first what is access specifiers in java? and then after we will study what are the uses of these access specifiers.
我们知道java中几乎没有访问说明符 。 我们将一一探讨访问说明符。 我们将首先研究Java中的访问说明符是什么? 然后我们将研究这些访问说明符的用途。
Access specifiers
访问说明符
Access specifiers are the keywords like "public", "protected", "default" and "private" which has its special meaning in java.
访问说明符是诸如“ public” , “ protected” , “ default”和“ private”之类的关键字,在Java中具有特殊含义。
It defines the access scope of the variable, methods, and classes and here the access scope means the area or space where a variable or classes or methods are accessible.
它定义了变量,方法和类的访问范围,此处访问范围是指可以访问变量或类或方法的区域或空间。
访问说明符的类型 (Types of access specifiers)
In java, there are four types of access specifiers and the name of these access specifiers are given below:
在Java中,访问说明符有四种类型,这些访问说明符的名称如下:
public access specifiers
公共访问说明符
protected access specifiers
受保护的访问说明符
default access specifiers
默认访问说明符
private access specifiers
专用访问说明符
Now, with the help of example, we will describe each access specifiers one by one in java.
现在,借助示例,我们将在Java中逐个描述每个访问说明符。
1)公共访问说明符 (1) public access specifiers)
"public" is the keyword which is introduced in java.
“ public”是在Java中引入的关键字。
The access scope of the "public" is everywhere like in all classes and methods as well.
像所有类和方法一样, “公共”的访问范围无处不在。
If we prefixed "public" keyword with any class, variable or method then it can be accessed by any class or methods.
如果我们为“ public”关键字加上任何类,变量或方法的前缀,那么任何类或方法都可以访问它。
Example:
例:
// ClassA save in package1
package package1;
public class ClassA {
public void display() {
System.out.println("We are in Java World");
}
}
package package2;
// importing package1 because we are using ClassA of package1
import package1.ClassA;
// class ClassB save in package2
class ClassB {
public static void main(String args[]) {
ClassA ca = new ClassA();
ca.display();
}
}
Output
输出量
We are in Java World
2)受保护的访问说明符 (2) protected access specifiers)
"protected" is the keyword which is introduced in java.
“ protected”是在Java中引入的关键字。
The access scope of the "protected" is not everywhere and it is accessible in the same class or its child class or in all those classes which are defined in the same package.
“受保护”的访问范围不是无处不在,并且可以在同一类或其子类或在同一程序包中定义的所有那些类中访问。
If we prefixed "protected" keyword with any class, variable or method then it can be accessed by the same class or its child classes or all the classes which are defined in the same package.
如果我们为“ protected”关键字加上任何类,变量或方法,则可以由同一类或其子类或同一包中定义的所有类访问它。
Example:
例:
// ClassA save in package1
package package1;
public class ClassA {
protected void display() {
System.out.println("We are in Java World");
}
}
package package2;
// importing package1 because
// we are using ClassA of package1
import package1.ClassA;
// class ClassB save in package2
class ClassB extends ClassA {
public static void main(String args[]) {
ClassA ca = new ClassA();
ca.display();
}
}
Output
输出量
We are in Java World
3)默认访问说明符 (3) default access specifiers)
"default" is the keyword which is introduced in java.
“ default”是在Java中引入的关键字。
The access scope of the "default" is not everywhere.
“默认”的访问范围并非无处不在。
It is not mandated to prefixed "default" keyword with any class, variable or method because by default class, variable or method is default public in java and it can be accessed by all those classes which are defined in same package only.
它不是必须在任何类,变量或方法的前面加上“ default”关键字的前缀,因为默认情况下,类,变量或方法在java中是默认的公共属性,并且只能由在同一程序包中定义的所有那些类访问。
Example:
例:
// ClassA save in package1
package package1;
class ClassA {
void display() {
System.out.println("We are in Java World");
}
}
package package2;
// importing package1 because we are using
// ClassA of package1
import package1.ClassA;
// ClassB save in package2
class ClassB {
public static void main(String args[]) {
/* Here we will get compiletime error because
ClassA is not public so we can't access
this class outside the package
*/
ClassA ca = new ClassA();
ca.display();
}
}
Output
输出量
ClassB.java:3: error: ClassA is not public in package1;
cannot be accessed from outside package
import package1.ClassA;
^
ClassB .java:10: error: cannot find symbol
ClassA ca= new ClassA();
^
symbol: class ClassA
location: class ClassB
ClassB.java:10: error: cannot find symbol
ClassA ca= new ClassA();
^
symbol: class ClassA
location: class ClassB
3 errors
4)私有访问说明符 (4) private access specifiers)
"private" is the keyword which is introduced in java.
“ private”是在Java中引入的关键字。
The access scope of the "private" is not everywhere.
“私有”的访问范围并非无处不在。
If we prefixed "private" keyword with any variable or method then it can be accessed only in the same class.
如果我们在“ private”关键字前面加上任何变量或方法,则只能在同一类中对其进行访问。
Example:
例:
// ClassA save in package1
package package1;
class ClassA {
private void display() {
System.out.println("We are in Java World");
}
}
package package2;
// importing package1 because we are using
// ClassA of package1
import package1.ClassA;
// ClassB save in package2
public class ClassB {
public static void main(String args[]) {
/* Here we will get compiletime error because
ClassA method is private so we can't access
this method outside the class and other package too
*/
ClassA ca = new ClassA();
ca.display();
}
}
Output
输出量
ClassB.java:3: error: ClassA is not public in package1;
cannot be accessed from outside package
import package1.ClassA;
^
ClassB .java:10: error: cannot find symbol
ClassA ca= new ClassA();
^
symbol: class ClassA
location: class ClassB
ClassB.java:10: error: cannot find symbol
ClassA ca= new ClassA();
^
symbol: class ClassA
location: class ClassB
3 errors
翻译自: https://www.includehelp.com/java/access-specifiers-in-java.aspx
java中访问控制符