java類只讀怎么辦
The question is that "can we make a read-only class in Java?"
問題是“我們可以用Java制作一個只讀類嗎?”
The answer is: "Yes, we can make a read-only in java."
答案是: “是的,我們可以在Java中將其設為只讀。”
在Java中定義只讀類 (Defining read-only class in Java)
Now, we will see in few steps, how to make Read-only class and the various steps in given below:
現在,我們將在幾個步驟中看到如何制作只讀類以及下面給出的各個步驟:
We can make a class read-only by making all of the data members private.
我們可以通過將所有數據成員設為私有來將類設為只讀。
Please note:
請注意:
If we make a class read-only, then we can’t modify the properties or data members value of the class.
如果我們將類設為只讀,則無法修改該類的屬性或數據成員值。
If we make a class read-only, then we can only read the properties or data members value of the class.
如果我們將類設為只讀,則只能讀取該類的屬性或數據成員值。
The read-only class will contain only getter methods which return the value of the private properties to the main() function.
只讀類將僅包含將私有屬性的值返回給main()函數的getter方法。
The read-only class can contain setter methods if we want to modify the value of the private properties after reading because there is our choice to keep setter method in the class but as per based on the concepts we should not contain.
如果我們想在讀取后修改私有屬性的值,則只讀類可以包含setter方法,因為可以選擇將setter方法保留在類中,但是根據我們不應該包含的概念。
Now, we will see the objective of the getter method, why it is required?
現在,我們將看到getter方法的目標,為什么需要它?
Few points need to remember about getter methods are given below:
以下是關于getter方法需要記住的幾點:
As we know that "private" data member of the class is accessible in the same class only.
眾所周知,該類的“私有”數據成員只能在同一類中訪問。
Let suppose we want to access "private" data member of the class in outside class. So, in that case, we need to declare public "getter" methods.
假設我們要在外部類中訪問該類的“私有”數據成員。 因此,在這種情況下,我們需要聲明公共的“ getter”方法。
The objective of the getter method is used to view the private variable values.
getter方法的目標用于查看私有變量值。
Syntax:
句法:
public returntype getDataMember_Name();
In the Getter method, it is not mandatory the same data member name after get, but it is convenient for our understanding that we should consider the same name as the data member after get.
在Getter方法中,獲取后并不一定要使用相同的數據成員名稱,但是對于我們理解而言,方便的是,我們應該考慮與獲取后的數據成員使用相同的名稱。
There are few advantages of getter methods are given below:
下面給出了getter方法的一些優點:
Hiding the internal representation of the private data member.
隱藏私有數據成員的內部表示。
Getter methods provide access level hierarchy.
Getter方法提供訪問級別層次結構。
This method adds additional functionality easily later.
此方法以后可以輕松添加其他功能。
This class allows getter methods to be passed around as lambda expressions rather than values.
此類允許getter方法作為lambda表達式而不是值傳遞。
The private data member is accessible from outside the class with the help of getter methods.
可以使用getter方法從類外部訪問私有數據成員。
Example:
例:
// Java program to demonstrate the example of
// making Read-only class in Java
public class Weeks {
// Private Data Member Declaration
private String days = "7 days";
// Defining Getter method to return the value of
// private properties.
public String getDays() {
return days;
}
public static void main(String[] args) {
// Weeks object instanstiation
Weeks w = new Weeks();
// Get the value of the private member
String result = w.getDays();
// Display the value of the private properties
System.out.println("Days in a Week:" + " " + result);
}
}
Output
輸出量
Days in a Week: 7 days
翻譯自: https://www.includehelp.com/java/how-to-make-a-read-only-class-in-java.aspx
java類只讀怎么辦