简体   繁体   English

类图-这种自反关系的外观如何?

[英]Class Diagram - How would this reflexive relationship look?

I am doing y firsts class diagrams as part of my career, and when reading the requirements of the assignment I came to one where two of the attributes of an Animal object are the Animal's Mother and Father IF they have pedigree. 作为职业生涯的一部分,我正在做一流的图表,当阅读作业要求时,我发现动物对象的两个属性是动物的母亲和父亲(如果他们有血统书的话)。

I asked the course teacher but the only thing he told me was that I was right in my assumption that it was a reflexive relationship but the following doubt remains. 我问课程老师,但他唯一告诉我的是,我以为是自反性关系是对的,但接下来的疑问仍然存在。

-How is this implemented in code? -如何在代码中实现?

We haven't seen this kind of relationship before and I only discovered it by browsing StackOverflow. 我们以前从未见过这种关系,我只是通过浏览StackOverflow才发现的。

Also we're managing the objects by demand, so instead of arrayLists we have been recently introduced to the use of vectors and tree maps. 此外,我们还按需管理对象,因此最近向我们介绍了矢量和树图的使用,而不是arrayLists。

This is how my class diagram looks right now. 这就是我的类图现在的样子。

动物类在右上角。

Padres = parents, and the justification for the multiplicity is that an Animal may or may not have both parents of just a mother/father registered. 教士=父母,多重性的理由是动物可能会或可能不会只注册有一个母亲/父亲的父母。

Thanks a lot in advance. 非常感谢。

I don't know if i understand the question correctly - is the code below of any help? 我不知道我是否正确理解了问题-下面的代码对您有帮助吗?

public class Animal {
    private Animal padre;
    private Animal madre;

    // ...
    // other fields
    // ...

    //one of the constructors.
    public Animal(final Animal padre, final Animal madre)
    {
        this.padre = padre;
        this.madre = madre;
    }

    // ...
    // rest of your code
    // ...
}

or if it should be an Array then: 或如果它应该是一个数组,则:

//field
private Animal[] padres;
//constructor
public Animal(final Animal[] padres) {
    if (padres != null) {
        if (padres.length > 2) {
            throw new IllegalStateException("Too many parents");
        }
    }
    this.padres = padres;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM