如何在 UML 中表示 C# 属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/470097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 04:40:13  来源:igfitidea点击:

How to represent a C# property in UML?

c#.netpropertiesuml

提问by annakata

Not quite an Attribute, not quite a Method. Stereotypes? <<get>><<set>>?

不完全是一个属性,也不完全是一个方法。刻板印象?<<get>><<set>>?



I'm retro-modelling an existing system, so I need to clearly reflect that this is not the same as a readonly field or a methods pair (regardless of what the IL says), so I think I'll go with the stereotype, but I'll accept the language independant get_ set_ as a general solution. Thanks all for the sanity test.

我正在对现有系统进行复古建模,所以我需要清楚地反映这与只读字段或方法对不同(无论 IL 怎么说),所以我想我会采用刻板印象,但我会接受语言独立的 get_set_ 作为通用解决方案。感谢大家的理智测试。

采纳答案by Ray Hayes

Properties are just a convenient way of writing get_MyValue()and set_MyValue(value)allowing assignment rather than the normal method calling (using parenthesis).

属性只是一种方便的编写get_MyValue()set_MyValue(value)允许赋值的方式,而不是普通的方法调用(使用括号)。

What you are accessing is actually a .NET property, C# has its own syntax for accessing these. Since under the skin the real get_and set_methods are created, so you could simply show those methods (to make your UML language independent - e.g. make your UML equally applicable to a VB.NET developer)

您正在访问的实际上是一个 .NET 属性,C# 有自己的语法来访问这些属性。由于在皮肤下创建了真实get_set_方法,因此您可以简单地显示这些方法(使您的 UML 语言独立 - 例如,使您的 UML 同样适用于 VB.NET 开发人员)

... or as you have suggested, introduce your own stereotype!

... 或者按照您的建议,介绍您自己的刻板印象!

回答by Rob

Eh, I just throw it in as a method in my pseudo-UML diagrams. :-)

呃,我只是把它作为我的伪 UML 图中的一种方法。:-)

回答by workmad3

properties are Get/Set methods wrapped up in some nicer syntax. Just put them in as methods, or create some new UML syntax for them :)

属性是包含在一些更好的语法中的 Get/Set 方法。只需将它们作为方法放入,或者为它们创建一些新的 UML 语法:)

回答by Hemanshu Bhojak

You can represent properties the same way as fields. To specify additional info like readonly or writeonly you can use

您可以以与字段相同的方式表示属性。要指定其他信息,如只读或只写,您可以使用

+Name:string {READONLY}

+Name:string {READONLY}

回答by Mike Hofer

I usually prepare my UML diagrams in Visio (I know, I know; but what're ya gonna do?).

我通常在 Visio 中准备我的 UML 图(我知道,我知道;但是你要做什么?)。

When diagramming properties, they end up as so:

在绘制属性图时,它们最终是这样的:

+------------------------+
| MyClass                |
|------------------------|
| - _foo : int           |
|------------------------|
| ?property? + Foo : int |
+------------------------+

?property? being a custom stereotype derived from ?operator?.

?财产?是从 ?operator? 派生的自定义构造型。

Ugly, I know. But it works, and it's clear. I do constructors the same way.

丑,我知道。但它有效,而且很清楚。我以同样的方式做构造函数。

回答by VoidPointer

I'd put them as public fields in UML, because that's what they are conceptually. UML is not a syntax for your programming language (although some tool vendors claim that it is).

我会将它们作为 UML 中的公共字段,因为这就是它们的概念。UML 不是您的编程语言的语法(尽管一些工具供应商声称它是)。

The details on how your implementation language handles properties don't need to show in the UML. This would entirely defeat the point of using UML as a tool that abstracts away the implementation details and lets you focus on design.

您的实现语言如何处理属性的细节不需要在 UML 中显示。这将完全违背使用 UML 作为工具来抽象实现细节并让您专注于设计的意义。

If the property is special in some way, such that it is derived or read-only, you can mark that with a stereotype annotation.

如果属性在某些方面是特殊的,例如它是派生的或只读的,您可以使用构造型注释来标记它。

回答by Kunal S

I use like this

我这样用

-memberThePropertyWillExpose
+memberThePropertyIsExposing

Well, comments on this are welcome if this is a right way !!

好吧,如果这是正确的方法,欢迎对此发表评论!!

回答by anon

The problem with depicting a property as a single field is that for C# using the 2.0 framework and beyond the get and set can have different access modifiers.

将属性描述为单个字段的问题在于,对于使用 2.0 框架的 C# 而言,get 和 set 可以有不同的访问修饰符。

回答by Assassin

I agree with workmad3. Properties are just a trick make a get/set methods a little bi nicer. For that reason I think it should be saved as a two different methods. What is more in this case you can set a different access permissions for them

我同意 workmad3。属性只是一个让 get/set 方法更好一点的技巧。出于这个原因,我认为它应该保存为两种不同的方法。更重要的是,在这种情况下,您可以为他们设置不同的访问权限

回答by Alexander

In Visio you can create a <<readonly>> stereotype for Attribute and just use this stereotype for each read-only attributes. Same for write-only. It'll show you a good notation:

在 Visio 中,您可以为 Attribute 创建一个 <<readonly>> 构造型,并且只需将此构造型用于每个只读属性。只写相同。它会告诉你一个很好的符号:

<<readonly>> +PropertyName : int

There is nothing ugly about it. The standard Visio's Changeable option is much more uglier in that it has no any visual representation and you actually need to open properties for each attribute in order to see it, no chances to see it on the printed diagram.

没有什么难看的。标准 Visio 的 Changeable 选项更加丑陋,因为它没有任何视觉表示,您实际上需要打开每个属性的属性才能看到它,没有机会在打印的图表上看到它。