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.pswp.cn/news/390384.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390384.shtml
英文地址,請注明出處:http://en.pswp.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…