簡體   English   中英

當我們將實例的所有變量或 class 聲明為私有時,是否實現了封裝?

[英]Is encapsulation get achieved when we declare all variables of the instance or class private?

當我們將實例的所有變量或 class 聲明為私有時,是否實現了封裝? 如果我必須將 class 的一些變量公開和一些私有變量怎么辦? 還是封裝嗎? 是否有必要將所有變量保密以進行封裝? Suppose I have a class "Employee", the variables of the class are Employee_name, Employee_salary, Employee_pincode and the methods of the class include "get Employee_salary", "set Employee_salary", "get Employee_pincode", "set Employee_pincode".I want to將 Employee_name 保持為公開,將 Employee_salary、Employee_pincode 保持為私有。 還是封裝嗎? 如果我將 class 的所有變量公開怎么辦? 是封裝嗎? 請清除我的困惑。 謝謝

在OOP(面向對象編程,其中封裝是四大支柱之一)中,變量被稱為字段或屬性。 字段是私有變量,而屬性是公共變量(可在類外訪問)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM