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 Modifier
的Access 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
?
因此,這意味著永遠不能將類設置為private
或protected
嗎?
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 Engine
class. 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 protected
access 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.
永遠都不能將其他類設置為protected
或private
,因為這沒有任何意義。 protected
訪問修飾符用于使事物成為package-private
但具有子類可訪問的選項。 Java中沒有諸如“子包”或“包繼承”之類的概念。
翻譯自: https://www.freecodecamp.org/news/java-access-modifiers-explained/
java中訪問修飾符