在 UML 图中,什么时候类应该是抽象的?
在 UML 图中,什么时候类应该是抽象的?就在我们想要阻止实例化的时候?
编辑:抽象类可以完全实现方法(当方法仅依赖于该抽象类的属性时)吗?
In an UML diagram, when should a class be abstract? Just when we want to prevent instantiation?
Edit: Can an abstract class fully implement a method (when the method only depends on the attributes of that abstract class)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
防止实例化是一个实现问题,因此如果您从 Java、C# 等角度思考,这是一个很好的理由。
但是,在建模层面上,如果有多个子类,您会希望将一个类抽象化,并且如果一个对象属于超类型但不属于任何子类型,那么这是没有意义的。
因此,基本上您不会阻止类的实例化,因为子类的任何实例也是抽象超类的实例。在这种情况下,您所做的就是防止超类单独实例化,即强制使用子类型之一。
回答编辑:是的!
Preventing instantiation is a matter of implementation, so it is a good reason if you are thinking in terms of Java, C# etc.
However, on the level of modelling, you'd want to make a class abstract if there are several subclasses, and it makes no sense that an object is of the supertype but of neither of the subtypes.
So, basically you're not preventing instantiation of the class, since any instance of the subclass is also an instance of the abstract superclass. What you are doing in that case is preventing instantiation of the superclass alone, i.e. enforce using one of the subtypes.
Answer to EDIT: Yes!
如果一个类具有一个或多个抽象操作,则该类是抽象的。
在UML中用斜体或{abstract}表示抽象。
下图(位于 Martin Fowler 所著的《UML Distilled Third Edition》一书的第 70 页)显示了如何以及为何表示摘要:
A class is abstract if it has one or more operations that are abstract.
In UML indicate abstract with italics or {abstract}.
The following diagram (which is on page 70 of my UML Distilled Third Edition book by Martin Fowler) shows how and why to denote abstracts:
抽象类 a 是基本的 OOP 概念,所以我认为您的问题不是特定于 UML 的。
这是一个关于何时使用抽象类的问题:
Exact use of Abstract class
基本上,当多个类共享一组功能但该功能本身没有意义或不完整时,您应该使用抽象类。只有抽象类才能包含抽象方法,抽象方法是供后代实现的。
例如,您可能希望将基本的
FileLogger
和EmailLogger
功能提取到Logger
类中,但由于它本身没有意义,你只需将其抽象化即可。Abstract class a is basic OOP concept so I don't think your question is specific to UML.
Here is a question on when to use abstract classes:
Exact use of Abstract class
Basically, you should use abstract class when several classes share a set of functionality but that functionality doesn't make sense or is incomplete on its own. Only abstract classes can contain abstract methods, which are for descendants to implement.
For example, you might want to extract basic
FileLogger
andEmailLogger
functionality intoLogger
class, however since it doesn't make sense on its own, you just leave it abstract.当您有多个具有相同功能的类,但每个类的某些方法与超类(抽象类)不同时,类应该是抽象的。
eG 动物。每个动物类都有 isAlive() 方法。此方法对所有动物都相同。
每个动物都有 move(target) 方法。但每种动物的移动方式都不同。你不能笼统地谈论动物的运动。有些动物会飞,有些动物会游泳,有些动物会跑。
A class should be abstract when you have multiple classes with same functionality, but some methods of each of this class is different from the super class (the abstract one).
e.G. An animal. Each animal-class has the method isAlive(). This method is equal for all animal.
And each animal has the method move(target). But each animal moves in a different way. You can't talk of the movement of an animal in a general way. Some animals fly, some swim and some run.
当您尝试表达子类化时,例如当您想要显式显示
Cat
时,这是Animal
的一种特殊情况。例如,
Animal
可以对某些行为进行一些基本实现,例如Eat
,这些行为可以在子类上重写。When you're trying to express subclassing, like when you want to make explicit a
Cat
is one special case ofAnimal
.For instance,
Animal
could have some basic implementation on some behaviors, likeEat
, which could be override on subclasses.抽象类与常规类类似,只是它允许您定义方法头而无需方法体。
这允许派生类有自己的实现,而基类没有自己的实现。
An abstract class is like a regular class except that it allows you to define method headers without a method body.
This allows derived classes to have their own implementation without the base class having it's own implementation.
抽象类意味着您可以维护一个方法或变量列表(默认情况下在抽象类内部为 public static final ),这强制扩展此抽象类的类实现那些未在声明的抽象类中实现的方法,(抽象类有一个与接口相似度很高,但唯一的区别是接口是100%抽象类)。
well an abstract class means that you can maintain a list of methods or variables(public static final by default inside abstract class) this enforces the class extending this abstract class to implement those methods not implemented in the declared abstract class, (abstract class has a high similarity to interfaces , but the only difference is that interfaces are 100% abstract classes ).