java類只讀怎么辦_如何在Java中制作一個只讀類?

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類只讀怎么辦

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/378135.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/378135.shtml
英文地址,請注明出處:http://en.pswp.cn/news/378135.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

LeetCode 53:最大子序和解題以及優化思路(第一次獨立刷題記錄)

給定一個整數數組 nums ,找到一個具有最大和的連續子數組(子數組最少包含一個元素),返回其最大和。 示例: 輸入: [-2,1,-3,4,-1,2,1,-5,4] 輸出: 6 解釋: 連續子數組 [4,-1,2,1] 的和最大,為 6。 進階: 如果你已經實現…

NHibernate 的 ID 標識選擇器

在 Hibernate 中,每個對象需要一個標識 ID,通過這個標識 ID 建立對象與數據庫中記錄的對應關系。 Nhibernate 提供了多種方式來建立這個 POID。基于不同的生成策略,可以選擇更佳的方式。 首先是賦值方式:assigned,這種…

三、規則組織的衍生組織——經山形組織數學模型的建立

基礎概念公式推到可參考該專欄下的前幾篇博文。 經山形組織圖: 左半部分:,3上2下1上2下,右斜,飛數為1 右半部分:,3上2下1上2下,左斜,飛數為-1 左右兩部分只有飛數是相…

c語言 函數的參數傳遞示例_scalbln()函數以及C ++中的示例

c語言 函數的參數傳遞示例C scalbln()函數 (C scalbln() function) scalbln() function is a library function of cmath header. It scales the significand using floating-point base exponent (long int) i.e. it is used to calculate the product of the given signific…

上周熱點回顧(7.8-7.14)

熱點隨筆: MingQQ v1.0高仿版開源了,使用WebQQ協議實現了QQ客戶端基本的聊天功能...(ZYM) 我的新書--《從員工到經理人》(Jimmy Zhang) MVC實用架構設計(三&#xff0…

儲存過程生成器

/Files/qanholas/SPGen_ReleaseCandidate1_Binaries.zip ---- Dropping stored procedure sp_費用表_SelectAll : --IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id OBJECT_ID(N[sp_費用表_SelectAll]) AND OBJECTPROPERTY(id, NIsProcedure) 1)DROP PROCEDURE [dbo].[sp…

基于計算機控制的溫度檢測系統,基于專用溫度傳感的溫度檢測系統.doc

基于專用溫度傳感的溫度檢測系統摘 要 在現代工業領域溫度檢測系統是指用某種方式顯示出當前的環境溫度。傳統使用PTC或NTC電阻作為溫度傳感器的方式在使用過程中存在著很多不足之處比如所采集溫度的精度比較低、系統的可靠性差、設計難度較大、整體設計成本較高等缺點已…

LeetCode 121:買賣股票的最佳時機 思考分析

題目描述: 給定一個數組,它的第 i 個元素是一支給定股票第 i 天的價格。 如果你最多只允許完成一筆交易(即買入和賣出一支股票一次),設計一個算法來計算你所能獲取的最大利潤。 注意:你不能在買入股票前賣出…

四、規則組織的衍生組織——經向破斜組織數學模型的建立

基礎概念公式推到可參考該專欄下的前幾篇博文。 經向破斜組織圖: 左半部分:,3上2下1上2下,右斜,飛數為1 右半部分:,2上1下2上3下。左斜,飛數為-1 左右兩部分,經緯紗組織…

EASYUI+MVC4通用權限管理平臺

通用權限案例平臺在經過幾年的實際項目使用,并取得了不錯的用戶好評。在平臺開發完成后,特抽空總結一下平臺知識,請各位在以后的時間里,關注博客的更新。 1.EASYUIMVC4通用權限管理平臺--前言 2.通用權限管理平臺--架構選型 3.通用…

int max+1小于0_INT_MAX常數,C ++中的示例

int max1小于0C INT_MAX宏常量 (C INT_MAX macro constant) INT_MAX constant is a macro constant which is defied in climits header, it is used to get the maximum value of a signed int object, it returns the maximum value that a signed int object can store, wh…

在計算機領域客觀事物的屬性表示為數據,數據與信息試題解析

一圖看懂數據與信息1、在計算機領域,信息是經過轉化而成為計算機能夠處理的__________。A.數據B.符號C.圖形D.數字答案:A。解析:本題考查有關信息基本概念的知識。信息是人們由客觀事物得到的。…

Mysql Data 目錄和 Binlog 目錄 搬遷

Mysql5.1.38 Data 目錄和 Binlog 目錄 搬遷 [mysql-bin.index not found (Errcode: 2)]Leave a comment Go to comments剛開始安裝時使用了默認目錄,使用一段時間,數據慢慢變在,發現當前設置的目錄空間不夠時,就要搬遷數據到另一個…

【數據結構基礎】【散列表】

散列表也叫做哈希表(hash table),這種數據結構提供了鍵(key)和值(value)的映射關系。只要給出一個key,就可以高效查找它匹配的value,時間復雜度接近O(1); 哈希函數 哈希函數通過某種方式,把key和數組下標進行轉換。 在java中,每…

VisualStudio運行C++項目檢測include<stdio.h>報錯解決方案

一、項目—>屬性 二、將SDL檢查更改為否即可

事業單位計算機技術崗工資,事業單位新入職的人員在管理崗位和技術崗位工資待遇是否有區別?...

解答于: 2016-05-24 17:17工傷保險條例對工傷工資待遇有說明: 第三十一條職工因工作遭受事故傷害或者患職業病需要暫停工作接受工傷醫療的,在停工留薪期內,原工資福利待遇不變,由所在單位按月支付。  停工留薪期一般…

信息設計中的“父子關系”

交互設計工作核心在于信息架構和交互細節設計。信息架構包括信息分類以及信息展示邏輯設計;交互細節則多表現為控件的選擇,交互效果的定義等。在信息設計中,遇到最棘手的問題就是信息量太多而顯得設計結果不盡人意,那么在砍不掉需…

python 示例_帶有示例的Python文件關閉屬性

python 示例文件關閉屬性 (File closed Property) closed Property is an inbuilt property of File object (IO object) in Python, it can be used to check whether a file object (i.e. a file) is closed or not, this is a read-only property and returns a Boolean val…

[Object-oriented] : 控制反轉

前言 : 參加點部落的活動,關于IoC(控制反轉)大家有很多的討論。本文排除對象生成的部份,單純解釋IoC為甚么叫做控制反轉。本篇文章以之前寫的 [Object-oriented] : 重用內容來舉例。 未IoC之前的對象圖 : 很明顯的左邊的組件A,相依右邊的組件…

二、規則組織數學模型的建立

一、規則組織數學模型的建立 規則組織滿足兩個不變:1,組織點運動規律不變、2,飛數不變的單系統組織 即:若知道組織點運動規律和飛數即可確定唯一一個組織。 3上2下,組織循環數為325,經紗循環數緯紗循環數…