[英]C#: Help understanding <<property>> in UML Class Diagrams
<<property>>
是一種刻板印象(就像<< >>
所包含的UML中的大多數東西一樣)。 在這種情況下,它指示您應為相應命名的類的私有屬性實現getter和setter。 例如,對於Status
您將實現getStatus
和setStatus
(或為此目的在目標語言中使用的任何東西)。 由於Name
還存在約束{ readonly }
,因此您只需實現getName
。 您可能不得不猜測該屬性的名稱是_bookName
。
由於您將其標記為[C#]
,因此您應該知道屬性是C#語言的重要組成部分。 類可以具有任何類型的屬性。 獲取器和設置器可以具有不同的訪問級別(例如,獲取器是公共的,而設置器是私有的)。 只讀屬性(無設置器)和僅寫屬性(無獲取器)可用。 如果該屬性的定義很簡單(getter和setter只需訪問一個私有的后備字段),則可以使用具有簡單,易於表達和理解的語法的自動屬性。
class MyClass {
//this is a simple property with a backing field
private int _someInt = 0;
public int SomeInt {
get { return _someInt; }
set { _someInt = value; } //"value" is a keyword meaning the rhs of a property set expression
}
//this is a similar property as an "auto property", the initializer is optional
public int OtherInt { get; set; } = 0;
//this is an auto-property with a public getter, but a protected setter
public string SomeString { get; protected set; }
}
如果省略了setter(或getter),則該屬性將變為只讀(或只寫)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.