java中访问修饰符

什么是访问修饰符? (What are Access Modifiers?)

Have you ever wanted to define how people would access some of your properties? You would not want anyone using your underwear. However, your close friends and relatives can use your sweater and maybe your car.

您是否曾经想过定义人们将如何访问您的某些物业? 您不希望任何人使用您的内衣。 但是,您的亲朋好友可以使用毛衣或汽车。

Similarly to how you set a level of access to your posessions, Java controls access, too. You want to define the access level for variables, methods and classes depending on which other classes you want accessing them.

与设置访问级别的方式类似,Java也控制访问。 您要定义变量,方法和类的访问级别,具体取决于您要访问它们的其他类。

Java provides 4 levels of access modifiers. This means that you can modify access to a variable, method or a class in 4 ways. These 4 ways are private, public, protected and default.

Java提供了4个级别的访问修饰符。 这意味着您可以通过4种方式修改对变量,方法或类的访问。 这4种方式分别是私有,公共,受保护和默认。

These access modifiers can be applied to fields, methods and classes (Classes are a special case, we will look at them at the end of this artice). Here is a quick overview1 of what the Access Levels are for each Access Modifier:

这些访问修饰符可以应用于字段,方法和类(类是一种特殊情况,我们将在本文末尾对其进行介绍)。 这是每个Access ModifierAccess Levels的简要概述1

访问修饰符表参考: (Access Modifiers Table Reference:)

专用访问修饰符 (Private Access Modifier)

Allows a variable or method to only be accessed in the class in which it was created. No other class beyond the class that created the variable or method can access it. This is closely similar to your internal organs. They are only accessible to the owner. To make a variable or method private, you simply append the private keyword before the variable or method type. Let us use private in a coding example. If a bank wants to provide an interest rate of 10% on it’s loans, it would make sure that the interest rate variable(let us suppose int int_rate;) would stay private so as no other class would try to access it and change it. For example;

允许仅在创建变量或方法的类中访问它。 除了创建变量或方法的类之外,没有其他类可以访问它。 这与您的内部器官非常相似。 只有所有者才能访问它们。 要使变量或方法私有,您只需在变量或方法类型之前附加private关键字。 让我们在编码示例中使用private。 如果银行想为其贷款提供10%的利率,它将确保利率变量(让我们假设int int_rate; )保持私密,这样其他任何类别的人都不会尝试访问和更改它。 例如;

private String name;The above example creates a variable called name and ensures that it is only accessible within the class from which it was created.

private String name; 上面的示例创建了一个名为name的变量,并确保只能在创建它的类中访问它。

Another example for a method is

方法的另一个示例是

private void setAge(){
System.out.println("Set Age");
}

The above example ensures that the method setAge is accessible only within the class from which it was created and nowhere else.

上面的示例确保方法setAge仅在创建它的类中可访问,而在其他地方则不可访问。

公共访问修饰符 (Public Access Modifier)

The public access modifier is the direct opposite of the private access modifier. A class, method or variable can be declared as public and it means that it is accessible from any class. Public access modifier can be likened to a public school where anyone can seek admission and be admitted.

公共访问修饰符与私有访问修饰符直接相反。 可以将类,方法或变量声明为public,这意味着可以从任何类访问它。 可以将公共访问修饰语比作公立学校,在那里任何人都可以寻求录取并被录取。

A public class, method, or variable can be accessed from any other class at any time.

可以随时从任何其他类访问公共类,方法或变量。

For example, to declare a class as public, all you need is:

例如,要将一个类声明为公共类,您需要做的是:

public class Animal{}

As such, the Animal class can be accessed by any other class.

这样,动物类可以被任何其他类访问。

public int age;
public int getAge(){
}

Above are ways of specifying a variable and a method as public.

上面是将变量和方法指定为public的方法。

默认访问修饰符 (The Default Access Modifier)

The default access modifier is different from all the other access modifiers in that it has no keyword. To use the default access modifier, you simply use none of the other access modifiers and that simply means you are using a default access modifier.

默认访问修饰符与所有其他访问修饰符不同,因为它没有关键字。 要使用默认访问修饰符,您只需使用其他访问修饰符即可,仅表示您正在使用默认访问修饰符。

For example, to use the default access modifier for a class, you use

例如,要为类使用默认的访问修饰符,请使用

class Bird{
}

This basically means you are using the default access modifier. The default access modifier allows a variable, method, or class to be accessible by other classes within the same package. A package is a collection of related classes in a file directory. For more information about packages, check out the section on packages.

这基本上意味着您正在使用默认的访问修饰符。 默认访问修饰符允许变量,方法或类可由同一包中的其他类访问。 包是文件目录中相关类的集合。 有关软件包的更多信息,请查看有关软件包的部分。

Any variable, method, or class declared to use the default access modifier cannot be accessed by any other class outside of the package from which it was declared.

声明为使用默认访问修饰符的任何变量,方法或类都不能被声明其的包外部的任何其他类访问。

int age;
void setNewAge(){
}

Above are some ways of using the default access modifier for a variable or method. Don’t forget, the default access modifier does not have a key word. The absence of the 3 other access modifiers means you are using the default access modifier.

以上是对变量或方法使用默认访问修饰符的一些方法。 别忘了,默认访问修饰符没有关键字。 如果没有其他3个访问修饰符,则表示您使用的是默认访问修饰符。

受保护的访问修饰符 (Protected Access Modifier)

The protected access modifier is closely related to the default access modifier. The protected access modifier has the properties of the default access modifier but with a little improvement.

受保护的访问修饰符与默认访问修饰符紧密相关。 受保护的访问修饰符具有默认访问修饰符的属性,但有一些改进。

A variable and method are the only ones to use the protected access modifier. The little improvement is that a class outside the class package from which the variable or method was declared can access the said variable or method. This is possible ONLY if it inherits from the Class, however.

变量和方法是唯一使用受保护的访问修饰符的方法。 小改进是,在类包之外声明了变量或方法的类可以访问所述变量或方法。 但是,只有从Class继承时才有可能。

The class from another package which can see protected variables or methods must have extended the Class that created the variables or methods.

可以查看受保护的变量或方法的另一个程序包中的类必须扩展了创建变量或方法的类。

Note without the advantage of Inheritance, a default access modifier has exactly the same access as a protected access modifier.

请注意,没有继承的优势,默认访问修饰符具有与受保护访问修饰符完全相同的访问权限。

Examples of using the protected access modifier is shown below:

下面显示了使用受保护的访问修饰符的示例:

protected int age;
protected String getName(){return "My Name is You";
}

类上的访问修饰符 (Access Modifiers on Classes)

By default, classes can only have 2 modifiers:

默认情况下,类只能具有2个修饰符:

  • public
    上市
  • no modifier (default modifier)
    无修饰符(默认修饰符)

So this means classes can never be set to private or protected?

因此,这意味着永远不能将类设置为privateprotected吗?

This is logical, why would you want to make a private class? No other class would be able to use it. But sometimes, you can embed a class into another class. These special classes, inner classes, can be set to private or protected so that only its surrounding class can access it:

这是合乎逻辑的,为什么您要进行私人授课? 没有其他班级可以使用它。 但有时,您可以将一个类嵌入另一个类。 可以将这些特殊类( inner classes设置为私有或受保护,以便只有其周围的类才能访问它:

public class Car {private String brand;private Engine engine;// ...    private class Engine {// ...}
}

In the above example, only the Car class can use the Engineclass. This can be useful in some cases.

在上面的示例中,只有Car类可以使用Engine类。 在某些情况下这可能很有用。

Other classes can never be set to protected or private, because it makes no sense. The protectedaccess modifier is used to make things package-private but with the option to be accessible to subclasses. There is no concept such as ‘subpackages’ or ‘package-inheritance’ in java.

永远都不能将其他类设置为protectedprivate ,因为这没有任何意义。 protected访问修饰符用于使事物成为package-private但具有子类可访问的选项。 Java中没有诸如“子包”或“包继承”之类的概念。

翻译自: https://www.freecodecamp.org/news/java-access-modifiers-explained/

java中访问修饰符

java中访问修饰符_Java中的访问修饰符介绍相关推荐

  1. java中访问修饰符_Java中的非访问修饰符是什么?

    java中访问修饰符 Java非访问修饰符 (Java non access modifiers) We have 7 non-access modifiers in Java. The name o ...

  2. java的四个访问修饰符_Java中的四种访问修饰符

    Java中修饰符分为两种:访问修饰符和非访问修饰符.修饰符中,有一些修饰符可以既可以修饰类,也可以修饰方法,但是有一些修饰符只能修饰符方法. 今天这篇文章先介绍一下四种访问修饰符. 1.private ...

  3. java 权限修饰符_java中4种访问权限修饰符

    在Java编程语言中有四种权限访问控制符,这四种访问权限的控制符能够控制类中成员的可见性. 一.public (1) 定义:public是公共的,被public所修饰的成员可以在任何类中都能被访问到. ...

  4. java中有哪些访问修饰符_java中四种访问修饰符

    Java中的四种访问修饰符:public.protected.default(无修饰符,默认).private. 四种修饰符可修饰的成分(类.方法.成员变量) public protected def ...

  5. java 属性访问修饰符_Java中的访问修饰符(二十七)

    访问(控制)修饰符:可以修饰属性和方法的访问范围 (在实际生活中,如果要获取某件物品,与其直接穿过堡垒的墙壁,从而导致墙壁毁灭和破坏,不如通过门口的警卫请求进入堡垒的许可.一般而言,这对对象同样适用: ...

  6. java中类的修饰符_Java中的类修饰符

    之前每次写小测试程序的时候,总是把一个类放在一个Java文件中,按理说这样比较规范,可主要原因是我是在不知道怎么在一个文件里放多个类,可见java基础有多差了...只要把类的属性改成默认的就可以了,也 ...

  7. java对于数组的定义_Java中方法的定义与使用,以及对数组的初步了解。

    方法 方法的含义 定义: 方法就是用来完成解决某件事情或实现某个功能的办法. 方法实现的过程中,会包含很多条语句用于完成某些有意义的功能--通常是处理文本,控制输入或计算数值. 我们可以通过在程序代码 ...

  8. Java NIO_I/O基本概念_Java中的缓冲区(Buffer)_通道(Channel)_网络I/O

    I/O基本概念 缓冲区基础 缓冲区是I/O的基础, 进程使用read(), write()将数据读出/写入从缓冲区中; 当缓冲区写满, 内核向磁盘发出指令, 将缓冲区中数据写入磁盘中(这一步不需要CP ...

  9. java中类定义修饰符_Java 中类和方法修饰符

    Java 中类和方法修饰符 类的修饰 访问修饰符 修饰符 class 类名 extends 父类名称 implements 接口名称(notes: 访问修饰符符与修饰符的位置可以互换) 访问修饰符 名 ...

最新文章

  1. 选本还是从缓存设计理念选择更好
  2. 你必须知道如何回答的五大计算机安全问题!
  3. lua 调用文件中的函数调用_深入Lua:调用相关的指令
  4. 理想制动力分配曲线matlab源代码_宝马进入“血拼”状态,动力倍儿棒
  5. face3000 c++ 代码运行
  6. linux查看服务器mib,Linux MIB目录的打开和查看
  7. UnityShader19:渲染纹理(上)之截屏功能实现
  8. node.js连接数据库(mysql)
  9. 用 JAVA 编写一个 M3U8 视频下载器
  10. linux 多线程服务端编程 pdf,Linux 多线程服务端编程.pdf
  11. 图解Python List数据结构
  12. 常用的三款专业的OCR文字识别软件
  13. Unity 之 ShaderGraph 实现火焰效果入门级教程
  14. Blender插件BoxCutter 7.1.7v15 硬表面建模2.91+教程Box Cutter
  15. java 内存中创建文件_java - 如何在Java中创建一个zip文件 - 堆栈内存溢出
  16. c/c++中指针学习的两个绝好例子
  17. 太过伤心,小王被这 10 道 Java 面试题虐哭了
  18. iOS 视频播放(AVPlayer)
  19. 4 看电影--贪心算法
  20. 开关电源产生浪涌电流的原因

热门文章

  1. web开发要学多久,HTML表格标签,薪资翻倍
  2. 将visio的图片插入latex(png格式转换成eps格式图片)
  3. windows下GitHub的SSH Key 配置
  4. 【安富莱二代示波器教程】第8章 示波器设计—测量功能
  5. 代码扫描工具测试覆盖率工具
  6. JavaWeb项目前端规范(采用命名空间使js深度解耦合)
  7. Linux下实现视频读取(二)---camera參数设定
  8. Leetcode: Kth Largest Element in an Array
  9. 删除.svn文件夹方法(转)
  10. Oracle优化检查表