java 在 UML 中表示重载方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/329686/
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-10-29 11:59:25  来源:igfitidea点击:

Representing overloaded methods in UML

javaumloverloading

提问by Mat Kelly

I am attempting to create a UML diagram representative of some Java code.

我正在尝试创建代表某些 Java 代码的 UML 图。

In a class I have a method that is overloaded.

在一个类中,我有一个重载的方法。

As far as I know, parameters for methods aren't shown in UML diagrams.

据我所知,方法的参数没有显示在 UML 图中。

How do I represent method overloading in UML?

如何在 UML 中表示方法重载?

Thanks.

谢谢。

回答by Vincent Ramdhanie

In the sub class you specify the method with the same signature as the method you wish to override and add a note {redefines} to the method. For example:

在子类中,您指定与您希望覆盖的方法具有相同签名的方法,并向该方法添加注释 {redefines}。例如:

+doSomething(p:AThing):int{redefines}

This implies that doSomething() method overrides the method in a super class. And yes, parameters for methods are shown on diagrams. As in the example p is a paremeter of type AThing.

这意味着 doSomething() 方法会覆盖超类中的方法。是的,方法的参数显示在图表上。在示例中,p 是 AThing 类型的参数。

回答by Gabriel ??erbák

When talking about overloading - e.g. in your class you have more methods with same name but different signature(parameters, maybe return value depending on target language...), you should provide the signature. UML doesn't specify that you cannot have method parameters.

在谈论重载时 - 例如,在您的类中,您有更多名称相同但签名不同的方法(参数,可能返回值取决于目标语言......),您应该提供签名。UML 没有规定不能有方法参数。

回答by alepuzio

You don't say your tool and UML diagram (I think class-diagram), but you have 2 ways:

你不说你的工具和 UML 图(我认为类图),但你有两种方法:

  1. you can write a note about this method;
  2. you can use keyword stereotype writing <<overloaded>> in this method;
  1. 你可以写一篇关于这个方法的笔记;
  2. 您可以在此方法中使用关键字构造型写作<<重载>>;

回答by Brian Matthews

Check the display options for the entire diagram or the individual class/interface. Most UML tools have options to display show the parameter list of methods.

检查整个图表或单个类/接口的显示选项。大多数 UML 工具都有显示方法参数列表的选项。

回答by Ted Johnson

Most of the answers above are correct given a certain question. Alepuzio, Vincent and bmatthews68 all have answers that make sense in context.

鉴于某个问题,上述大多数答案都是正确的。Alepuzio、Vincent 和 bmatthews68 都有在上下文中有意义的答案。

** If the question is around Overriding of a super classes method with the same signature than redefining is the correct definition. If it is overloading in that you create the same method which takes different arguments then I do not believe this is possible to model structurally, you can show this with a sequence diagram for example which is behavioral, but still not really.

** 如果问题是关于重写具有相同签名的超类方法而不是重新定义是正确的定义。如果它是重载,因为您创建了采用不同参数的相同方法,那么我认为这不可能在结构上建模,您可以使用序列图来展示这一点,例如这是行为性的,但仍然不是真的。

So +doSomething(p:AThing):int{redefines} is correct which is what Vincent put.

所以 +doSomething(p:AThing):int{redefines} 是正确的,这就是文森特所说的。

** If your problem/question is just around parameters not showing up visually in a diagram that is usually a setting in most UML tools.

** 如果您的问题只是围绕参数未在图表中直观地显示出来,而这通常是大多数 UML 工具中的设置。

** If you want to make it even more clear what you are doing then use a keyword <>, also note a keyword is not a stereotype as it is not part of the meta-model.

** 如果您想更清楚地说明您在做什么,请使用关键字 <>,还要注意关键字不是构造型,因为它不是元模型的一部分。