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

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

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中没有诸如“子包”或“包继承”之类的概念。

翻译自:

java中访问修饰符

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

你可能感兴趣的文章
静态链接与动态链接的区别
查看>>
Android 关于悬浮窗权限的问题
查看>>
如何使用mysql
查看>>
linux下wc命令详解
查看>>
敏捷开发中软件测试团队的职责和产出是什么?
查看>>
在mvc3中使用ffmpeg对上传视频进行截图和转换格式
查看>>
python的字符串内建函数
查看>>
Spring - DI
查看>>
微软自己的官网介绍 SSL 参数相关
查看>>
Composite UI Application Block (CAB) 概念和术语
查看>>
64位MATLAB和C混合编程以及联合调试
查看>>
原生js大总结二
查看>>
PHP基础
查看>>
UVa 11488 超级前缀集合(Trie的应用)
查看>>
Django 翻译与 LANGUAGE_CODE
查看>>
[转]iOS教程:SQLite的创建数据库,表,插入查看数据
查看>>
【转载】OmniGraffle (一)从工具栏开始
查看>>
初识ionic
查看>>
java 中打印调用栈
查看>>
开发 笔记
查看>>