java中访问修饰符_Java中的访问修饰符介绍

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中访问修饰符

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390384.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

VIM 编辑器

2019独角兽企业重金招聘Python工程师标准>>> VIM 相对于VI 的提升 VIM 支持多级撤销VIM 可以跨平台运行VIM 支持语法高亮VIM 支持图形界面VIM 编辑器的操作模式 Command Mode -命令模式Insert Mode -输入模式Last Lin Mode -底行模式#使用yum 命令安装vim 软件&…

/etc/profile、/etc/bashrc、~/.bash_profile、~/.bashrc 文件的作用

转载自:http://blog.csdn.net/u013968345/article/details/21262033 /etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行. 并从/etc/profile.d目录的配置文件中搜集shell的设置. /etc/bashrc:为每一个运行bash shell的用户执行此文件…

python初学者_终极Python初学者手册

python初学者Python has become one of the fastest-growing programming languages over the past few years. 在过去的几年中,Python已成为增长最快的编程语言之一。 Not only it is widely used, it is also an awesome language to tackle if you want to get …

z-index

z-index 这个东西非常简单,它有四大特性,每个特性你记住了,页面布局就不会出现找不到盒子的情况。 z-index 值表示谁压着谁,数值大的压盖住数值小的,只有定位了的元素,才能有z-index,也就是说,不…

大型运输行业实战_day12_1_权限管理实现

1.业务分析 权限说的是不同的用户对同一个系统有不同访问权限,其设计的本质是:给先给用户分配好URL,然后在访问的时候判断该用户是否有当前访问的URL. 2.实现 2.1数据库设计标准5表权限结构 2.2.sql语句实现,根据用户id查询该用户所有的资源 sql语句: SELECT ur.user_id, r.u…

aws python库_如何使用Python,AWS和IEX Cloud创建自动更新股市数据的Excel电子表格

aws python库Many Python developers in the financial world are tasked with creating Excel documents for analysis by non-technical users.金融界的许多Python开发人员的任务是创建Excel文档,以供非技术用户进行分析。 This is actually a lot harder than i…

37)智能指针(就是自动delete空间)

1)问题引入: 在java或者在C中,一旦你new一个东西,那么必然有一个delete与之对应,比如: 1 int main()2 {3 int* p new int();4 5 *…

linux 安装maven

2019独角兽企业重金招聘Python工程师标准>>> 目录:/usr/local/maven 1.下载 wget http://mirrors.hust.edu.cn/apache/maven/maven-3/3.5.3/binaries/apache-maven-3.5.3-bin.tar.gz 2.解压 tar -zxvf apache-maven-3.5.3-bin.tar.gz 3.配置 vi /etc/profile #讲下面…

自由开发者怎么生存_如何作为自由开发者生存

自由开发者怎么生存It’s been 8 weeks since we started experiencing the dramatic impact of the COVID-19 pandemic. In that time, we’ve all borne witness to how this virus can impact our families, our communities, and our livelihood. 自我们开始体验COVID-19大…

UUID生成字符串

在向数据库插入新数据时,可能需要插入字符串形式的ID,这时使用UUID可以生成随机字符串: String str UUID.randomUUID().toString(); 转载于:https://www.cnblogs.com/suhfj-825/p/8260861.html

如何在React Native中使用react-navigation 5处理导航

React-navigation is the navigation library that comes to my mind when we talk about navigation in React Native. 当我们谈论React Native中的导航时,React-navigation是我想到的导航库。 Im a big fan of this library and its always the first solution I…

flask内置session原理

内置session原理 请求到来 当请求进来之后,先执行Flask对象的 __call__ 方法 def wsgi_app(self, environ, start_response):# 获取请求相关数据,并进行封装和加工ctx self.request_context(environ)# 将请求消息推送到堆栈中,并执行 open_s…

指针3

#include <stdio.h>/* 2018-05-28 如何通过被调函数修改主调函数普通变量的值1&#xff0c;实参必须为该普通变量的地址2,形参必须为指针变量3&#xff0c;在背调函数中通过*形参名 。。。。。的方式就可以修改主调函数相关变量的值*/f(int *i,int *j) {*i 4;*j 5;ret…

面试系统设计_系统设计面试问题–您应该知道的概念

面试系统设计You may have heard the terms "Architecture" or "System Design." These come up a lot during developer job interviews – especially at big tech companies.您可能已经听说过“架构”或“系统设计”这两个术语。 在开发人员工作面试中&…

8597 石子划分问题 dpdp,只考虑第一次即可

8597 石子划分问题 时间限制:500MS 内存限制:1000K提交次数:155 通过次数:53 题型: 编程题 语言: G;GCC;VC Description 给定n个石子&#xff0c;其重量分别为a1,a2,a3,...,an。 要求将其划分为m份&#xff0c;每一份的划分费用定义为这份石子中最大重量与最小重量差的平方。…

文章中嵌入代码块_如何在您的文章中嵌入多项选择测验问题

文章中嵌入代码块In my experience, supplementing study with practical exercises greatly improves my understanding of a topic. This is especially true when I can test my knowledge as I go and receive instant feedback for each question.以我的经验&#xff0c;通…

mysql免安装版配置

1.官网下载https://dev.mysql.com/downloads/mysql/ 2.将下载好的压缩包mysql-5.7.20-winx64.zip解压。 3.mysql解压后&#xff0c;设置.ini文件&#xff0c;在加压后的路径中加一个my.ini文件 配置如下内容&#xff1a; # 设置mysql客户端默认字符集 default-character-setutf…

各种IE(IE6-IE10)兼容问题一行代码搞定

x-ua-compatible 用来指定IE浏览器解析编译页面的model x-ua-compatible 头标签大小写不敏感&#xff0c;必须用在 head 中&#xff0c;必须在除 title 外的其他 meta 之前使用。 1、使用一行代码来指定浏览器使用特定的文档模式。 <meta http-equiv"x-ua-compatible&q…

802. 找到最终的安全状态

在有向图中&#xff0c;以某个节点为起始节点&#xff0c;从该点出发&#xff0c;每一步沿着图中的一条有向边行走。如果到达的节点是终点&#xff08;即它没有连出的有向边&#xff09;&#xff0c;则停止。 对于一个起始节点&#xff0c;如果从该节点出发&#xff0c;无论每…

元类型与类型的区别

元类型是指所有类型的类型。 元类型只能类型出现在类型标示位&#xff1b; 类型即能作为类型存在&#xff0c;出现在类型标示位&#xff1b; 也能作为变量存在&#xff0c;出现在元类型的变量位。 http://www.swift51.com/swift2.0/chapter3/03_Types.html#type_inheritance_cl…