UML 枚举和 class 之间的泛化
Generalization between UML enumeration and class
根据UML 2.5版的UML规范对UML枚举和UML之间的泛化关系建模是否正确class?在第 166 页,规范声明 "As a specification of Classifier, Enumerations can participate in generalization relationships"。然而,在 class 和枚举之间放置泛化关系是否正确?如果是,那么应该如何解释这样的枚举?
是的,你可以这样做。如 UML 2.5 元模型所示,这两个元素都是 Classifier
。
因此,您可以在两者之间使用继承。
现在回答你的问题,这意味着什么。好吧,老实说我不知道。枚举基本上只是枚举。在过去,它们只是 EnumerationLiterals 的列表。最近的语言也允许 operations/properties。但是,当你用 Class 拉皮条时,那又有什么意义呢?泛化是一个有两条边的blade。将 amphi-car 定义为继承自 car 和 ship 感觉很奇怪。两者都不是,而是新事物。所以在枚举的情况下它是相似的。
我仍然没有找到正式的约束,但您可以使用泛化定义来寻找 Class 和枚举之间的泛化是否有意义的证据(或找到有解释的例子):
A Generalization is a taxonomic relationship between a more general
Classifier and a more specific Classifier. Each instance of the
specific Classifier is also an instance of the general Classifier. (UML 2.5.1, p.138)
根据您的构造,枚举的一个实例也将是 Class 的一个实例(反之亦然)。现在让我们看看这意味着什么。
When a Classifier is generalized, certain members of its
generalizations are inherited, that is they behave as though they were
defined in the inheriting Classifier itself. For example, an inherited
member that is an attribute may have a value or collection of values
in any instance of the inheriting Classifier... (UML 2.5.1, p.100)
事实上,枚举可以像任何 Class 标识符一样具有属性,那么为什么它不应该从 Class 继承这些属性呢?进一步研究枚举的实例或 class 的实例意味着什么,我们发现:
DataTypes model Types whose instances are distinguished only by their
value. ... Each value of an Enumeration corresponds to one of its
user-defined EnumerationLiterals. ... EnumerationLiterals may not
change during their existence, so any attributes on an Enumeration
shall be read-only. (UML 2.5.1, p. 209 f.)
啊哈,也许我们可以枚举对象及其属性值(Class 的实例)作为枚举的值?枚举的实例是否也是 Class 的实例?这会是一个合适的解释吗?
我只是在大声思考,不确定这是否有意义。我只是不会因为我从未遇到或无法想象而排除存在一种解释。事实上,看看 Java 中的枚举——它们隐式地扩展了 Class java.lang.Enum
并且它们不能进一步扩展用户定义的 classes 或其他枚举的唯一原因是Java 不支持状态的多重继承 (Oracle Java tutorials)。
根据UML 2.5版的UML规范对UML枚举和UML之间的泛化关系建模是否正确class?在第 166 页,规范声明 "As a specification of Classifier, Enumerations can participate in generalization relationships"。然而,在 class 和枚举之间放置泛化关系是否正确?如果是,那么应该如何解释这样的枚举?
是的,你可以这样做。如 UML 2.5 元模型所示,这两个元素都是 Classifier
。
因此,您可以在两者之间使用继承。
现在回答你的问题,这意味着什么。好吧,老实说我不知道。枚举基本上只是枚举。在过去,它们只是 EnumerationLiterals 的列表。最近的语言也允许 operations/properties。但是,当你用 Class 拉皮条时,那又有什么意义呢?泛化是一个有两条边的blade。将 amphi-car 定义为继承自 car 和 ship 感觉很奇怪。两者都不是,而是新事物。所以在枚举的情况下它是相似的。
我仍然没有找到正式的约束,但您可以使用泛化定义来寻找 Class 和枚举之间的泛化是否有意义的证据(或找到有解释的例子):
A Generalization is a taxonomic relationship between a more general Classifier and a more specific Classifier. Each instance of the specific Classifier is also an instance of the general Classifier. (UML 2.5.1, p.138)
根据您的构造,枚举的一个实例也将是 Class 的一个实例(反之亦然)。现在让我们看看这意味着什么。
When a Classifier is generalized, certain members of its generalizations are inherited, that is they behave as though they were defined in the inheriting Classifier itself. For example, an inherited member that is an attribute may have a value or collection of values in any instance of the inheriting Classifier... (UML 2.5.1, p.100)
事实上,枚举可以像任何 Class 标识符一样具有属性,那么为什么它不应该从 Class 继承这些属性呢?进一步研究枚举的实例或 class 的实例意味着什么,我们发现:
DataTypes model Types whose instances are distinguished only by their value. ... Each value of an Enumeration corresponds to one of its user-defined EnumerationLiterals. ... EnumerationLiterals may not change during their existence, so any attributes on an Enumeration shall be read-only. (UML 2.5.1, p. 209 f.)
啊哈,也许我们可以枚举对象及其属性值(Class 的实例)作为枚举的值?枚举的实例是否也是 Class 的实例?这会是一个合适的解释吗?
我只是在大声思考,不确定这是否有意义。我只是不会因为我从未遇到或无法想象而排除存在一种解释。事实上,看看 Java 中的枚举——它们隐式地扩展了 Class java.lang.Enum
并且它们不能进一步扩展用户定义的 classes 或其他枚举的唯一原因是Java 不支持状态的多重继承 (Oracle Java tutorials)。