UML Representing an anonymous class in class diagr

2019-02-13 14:54发布

How would one represent a Java anonymous class in a UML class diagram?

标签: java oop uml
3条回答
做个烂人
2楼-- · 2019-02-13 15:01

There are two really anonymous classes in Java First is the non-named inner class. Such as:

class BGThread<T>{...}
...
class TitleEditDlg{
    new BGThread<Props>(cont, true) {
        @Override
        public Props run() {
            ...
        }
    }
 }

A citation from UML standard 2.5 (p.149):

The standard notation for an anonymous InstanceSpecification of an unnamed Classifier is an underlined colon (‘:’).

So, as for anonymous java class, you should create a class block with only : as name and connect the container class to it twice - by container relationship and by one-direction arrow without a dot. From the other side, the : block should connect to the parent class.


According to the same source, an Anonymous Bound Class, that is the second anonymous class we meet in Java, but often do not notice it, when you use a template/generic class, as in

class BGThread<T>{...}
...
class TitleEditDlg{
   BGThread<String> newThread= new BGThread<String>();
}    

can be shown by two ways:

  • As bind dependency, with substitution on it.
  • As an intermediate class, with the name of the parent class and substitution in angle brackets. Notice, here the class is anonymous, but the attribute has a name. So, this way you are showing more information.

enter image description here

查看更多
欢心
3楼-- · 2019-02-13 15:12

I was looking for a way to represent a JavaScript object literal in a class diagram (I know it doesn't make much sense...)and I found this post.

I'd contribute with this link and image. Ciao.

http://www.uml-diagrams.org/class-diagrams.html

enter image description here

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-02-13 15:22

Inner (nested) classes are represented in UML with an association adorned with a crossed circle.

Illustration:

UML inner class

Source:

查看更多
登录 后发表回答