博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java访问修饰符_Java访问修饰符
阅读量:2532 次
发布时间:2019-05-11

本文共 6789 字,大约阅读时间需要 22 分钟。

java访问修饰符

Java access modifiers are used to provide access control in java. Java provides access control through three keywords – private, protected and public. We are not required to use these access modifiers always, so we have another one namely “default access“, “package-private” or “no modifier“.

Java访问修饰符用于在Java中提供访问控制。 Java通过三个关键字( 私有受保护公共)提供访问控制。 我们不需要总是使用这些访问修饰符,因此我们还有另一个名称,即“ 默认访问 ”,“ package-private ”或“ 无修饰符 ”。

Java访问修饰符 (Java Access Modifiers)

We can use java access modifiers with Classes as well as Class variables and methods.

我们可以将Java访问修饰符与类以及类变量和方法一起使用。

We are allowed to use only “public” or “default” access modifiers with java classes.

我们只能在Java类中使用“公共”或“默认”访问修饰符。

  1. If a class is “public” then we can access it from anywhere, i.e from any other class located in any other packages etc.

    如果一个类是“公共”的,那么我们可以从任何地方访问它,即可以从位于任何其他包等中的任何其他类访问它。
  2. We can have only one “public” class in a source file and file name should be same as the public class name.

    源文件中只能有一个“公共”类,并且文件名应与公共类名相同。
  3. If the class has “default access” then it can be accessed only from other classes in the same package.

    如果该类具有“默认访问权限”,则只能从同一程序包中的其他类进行访问。

具有类成员的Java Access修饰符 (Java Access Modifiers with Class Member)

We can have all the four access modifiers for class member variables and methods. However, member access modifier rules get applied after the class level access rules. For example, if a class is having default access then it will not be visible in other packages and hence methods and variables of the class will also be not visible.

我们可以为类成员变量和方法提供所有四个访问修饰符。 但是,成员访问修饰符规则在类级别访问规则之后应用。 例如,如果一个类具有默认访问权限,则它将在其他包中不可见,因此该类的方法和变量也将不可见。

We will look into each of them separately and then we will show the java access modifiers usage with a simple program.

我们将分别研究它们中的每一个,然后通过一个简单的程序显示java访问修饰符的用法。

Java访问修饰符–公共关键字 (Java Access Modifiers – public keyword)

If a class member is “public” then it can be accessed from anywhere. The member variable or method is accessed globally. This is the simplest way to provide access to class members. However, we should take care of using this keyword with class variables otherwise anybody can change the values. Usually, class variables are kept as private and getter-setter methods are provided to work with them.

如果班级成员是“公开”的,则可以从任何地方访问它。 成员变量或方法是全局访问的。 这是提供对类成员的访问的最简单方法。 但是,我们应该注意将此关键字与类变量一起使用,否则任何人都可以更改值。 通常,类变量被保留为私有变量,并且提供了使用getter-setter方法来使用它们。

Java Access修饰符–专用关键字 (Java Access Modifiers – private keyword)

If a class member is “private” then it will be accessible only inside the same class. This is the most restricted access and the class member will not be visible to the outer world. Usually, we keep class variables as private and methods that are intended to be used only inside the class as private.

如果班级成员是“私人”,则只能在同一班级内访问。 这是最受限制的访问,并且该类成员对外部世界是不可见的。 通常,我们将类变量保留为私有,而将仅在类内部使用的方法保留为私有。

Java访问修饰符–受保护的关键字 (Java Access Modifiers – protected keyword)

If class member is “protected” then it will be accessible only to the classes in the same package and to the subclasses. This modifier is less restricted from private but more restricted from public access. Usually, we use this keyword to make sure the class variables are accessible only to the subclasses.

如果类成员是“受保护的”,则只有同一包中的子类才能访问它。 此修饰符受私人访问的限制较小,但受公共访问的限制更大。 通常,我们使用此关键字来确保类变量只能由子类访问。

Java访问修饰符–默认访问 (Java Access Modifiers – default access)

If a class member doesn’t have any access modifier specified, then it’s treated with default access. The access rules are similar to classes and the class member with default access will be accessible to the classes in the same package only. This access is more restricted than public and protected but less restricted than private.

如果类成员未指定任何访问修饰符,则将使用默认访问权限进行处理。 访问规则与类相似,具有默认访问权限的类成员仅可用于同一包中的类。 这种访问比公共和受保护的限制更严格,但比私有的限制更宽松。

(Least Accessible) private < default < protected < public (Most Accessible)

(最少访问权限) 私人 < 默认 < 受保护 < 公共 (最多访问)

The below table summarise above access modifiers with respect to different classes in the same package or other packages and subclasses.

下表总结了关于同一包或其他包和子类中的不同类的上述访问修饰符。

Let’s write some simple classes where we will see the java access modifiers in action.

让我们写一些简单的类,在那里我们将看到java访问修饰符的作用。

TestA.java

TestA.java

package com.journaldev.access;class TestA {	public void methodPublic(){		methodPrivate();	}		protected void methodProtected(){		methodPrivate();	}		void methodDefault(){		methodPrivate();	}		private void methodPrivate(){}}

Note that TestA class has default access and the private class method is accessible to all other parts of the same class.

请注意,TestA类具有默认访问权限,私有类方法可用于同一类的所有其他部分。

TestB.java

TestB.java

package com.journaldev.access;import com.journaldev.access.TestA;public class TestB {	public static void main(String args[]) {		new TestA().methodPublic();		new TestA().methodProtected();		new TestA().methodDefault();	}	public void methodPublic() {	}	protected void methodProtected() {	}	void methodDefault() {	}	private void methodPrivate() {	}}

Note that TestB is in the same package as TestA class and hence it is able to access it’s class members. private members are not accessible but all other members are accessible because of the same package.

请注意,TestB与TestA类位于同一程序包中,因此它可以访问其类成员。 私有成员不可访问,但由于同一软件包,所有其他成员均可访问。

TestC.java

TestC.java

package com.journaldev.access.child;import com.journaldev.access.TestB;public class TestC {	public static void main(String[] args) {		new TestB().methodPublic();	}}

TestB class is accessible because it’s public. Only public members of TestB class is accessible because TestC class is not in the same package nor its subclass of TestB.

可访问TestB类,因为它是公共的。 由于TestC类不在同一个包中,也不在其TestB的子类中,因此只能访问TestB类的公共成员。

TestE.java

TestE.java

package com.journaldev.util;import com.journaldev.access.TestB;public class TestE extends TestB {	public static void main(String[] args) {		new TestB().methodPublic();		new TestB().methodProtected(); // compile time error		// works, accessing super class protected method using subclass		new TestE().methodProtected();	}}

Since TestE class is a subclass of TestB, we can access TestB protected members through child class TestE. If we try to access the superclass protected method directly, we will get a compile-time error.

由于TestE类是TestB的子类,因此我们可以通过子类TestE访问受TestB保护的成员。 如果尝试直接访问超类受保护的方法,则会收到编译时错误。

That’s all for the java access modifiers, it’s simple to understand. Just don’t confuse with the default and protected access.

这就是java访问修饰符的全部内容,很容易理解。 只是不要混淆默认和受保护的访问。

An easy way to remember is that default access is more restricted than protected and protected members are accessible in subclasses.

记住一个简单的方法是,默认访问比受保护的对象要受限制,并且受保护的成员可以在子类中访问。

Recently I made a video to explain java access modifiers in detail, you can watch it below on YouTube.

最近,我拍了一段视频,详细解释了Java访问修饰符,您可以在下面的YouTube上观看。

翻译自:

java访问修饰符

转载地址:http://avlzd.baihongyu.com/

你可能感兴趣的文章
POJ 1141 Brackets Sequence
查看>>
Ubuntu 18.04 root 使用ssh密钥远程登陆
查看>>
Servlet和JSP的异同。
查看>>
虚拟机centOs Linux与Windows之间的文件传输
查看>>
ethereum(以太坊)(二)--合约中属性和行为的访问权限
查看>>
IOS内存管理
查看>>
middle
查看>>
[Bzoj1009][HNOI2008]GT考试(动态规划)
查看>>
Blob(二进制)、byte[]、long、date之间的类型转换
查看>>
OO第一次总结博客
查看>>
day7
查看>>
iphone移动端踩坑
查看>>
vs无法加载项目
查看>>
Beanutils基本用法
查看>>
玉伯的一道课后题题解(关于 IEEE 754 双精度浮点型精度损失)
查看>>
《BI那点儿事》数据流转换——百分比抽样、行抽样
查看>>
哈希(1) hash的基本知识回顾
查看>>
Leetcode 6——ZigZag Conversion
查看>>
dockerfile_nginx+PHP+mongo数据库_完美搭建
查看>>
Http协议的学习
查看>>