【问题标题】:UML - How to align class diagrams with child classes verticalyUML - 如何将类图与子类垂直对齐
【发布时间】:2020-12-09 06:47:47
【问题描述】:

在随附的 UML 图中有 5 个类,我想了解 A、B 和 C 类的顶部如何垂直对齐,而子类保持对齐。请在下面找到我的 UML 代码和屏幕截图。

非常感谢您的支持! :)

目前的样子:

它应该是什么样子(油漆编辑):

UML - 代码:

@startuml TestClassDiagram
scale 800 width
skinparam SameClassWidth true
skinparam ClassFontSize 15

class classA {
{field}  - attribute1  : int
{field}  - attribute2  : int
{method} + method1(void)
{method} + method2(void)
{method} + method3(void)
{method} + method4(void)
{method} + method5(void)
}

class classB {
{field}  - attribute1 : int
{field}  - attribute2 : int
{method} + method1(void)
{method} + method2(void)
}
class classBchild     {
{method} + method1(void)    
}

class classC {
{field}  - attribute1  : int
{field}  - attribute2  : int
{field}  - attribute3  : int
{field}  - attribute4  : int
{method} + method1(void)
{method} + method2(void)
{method} + method3(void)
{method} + method4(void)
{method} + method5(void)
}
class classCchild {
{method} + method1(void)   
}

classB <|-- classBchild
classC <|-- classCchild

@enduml

【问题讨论】:

  • 从网上查到的其他问题看来是不可能的。
  • 感谢阿尔伯特,您的快速回复!这很难过:-P

标签: uml class-diagram plantuml


【解决方案1】:

PlantUML 中还没有对齐类的功能。

如果我们在所有元素之间添加箭头,就可以清楚地看到 PlantUML 试图做什么:

它只是从中间对齐所有图表。

使用这个,我们可以创建一个 的 hack,通过在类定义中添加额外的换行符直到它们的大小相同,从而达到您想要的结果:

@startuml
skinparam {
    SameClassWidth true
    ClassFontSize 15
}

class A as "classA" {
    {field}  - attribute1  : int
    {field}  - attribute2  : int


    __
    {method} + method1(void)
    {method} + method2(void)
    {method} + method3(void)
    {method} + method4(void)
    {method} + method5(void)
}

class B as "classB" {
    {field}  - attribute1 : int
    {field}  - attribute2 : int


    __
    {method} + method1(void)
    {method} + method2(void)



}

class Bc as "classBchild"     {
    {method} + method1(void)    
}

class C as "classC" {
    {field}  - attribute1  : int
    {field}  - attribute2  : int
    {field}  - attribute3  : int
    {field}  - attribute4  : int

    {method} + method1(void)
    {method} + method2(void)
    {method} + method3(void)
    {method} + method4(void)
    {method} + method5(void)
}

class Cc as "classCchild" {
    {method} + method1(void)   
}

B <|-- Bc
C <|-- Cc
@enduml

【讨论】:

  • 太棒了!这个解决方案对我来说与请求的格式具有相同的价值:) 非常感谢!!!
猜你喜欢
  • 1970-01-01
  • 2015-05-04
  • 1970-01-01
  • 1970-01-01
  • 2021-08-22
  • 1970-01-01
  • 2020-04-29
  • 2012-02-24
相关资源
最近更新 更多