What’s wrong with defining a Java class with an array of elements of that class as one of its fields?

My application model is composed of disorders, each of which has innate attributes (identity, name, incidence rate, etc.) and its relationship with other diseases (such as risk factors, disease causing, and discovery). I have considered the following two object models:

First: nested class:

class DiagnosticElement{
      Disorder disorder;
      List<Disorder> relatedDisorders;

      static class Disorder {
          int id;
          String name;
          double incidence;
          Disorder(id){
              this.id=id;
          }

    }
}

Second:

class Disorder {
        int id;
        String name;
        double incidence;
        List<Disorder> relatedDisorders;

        Disorder(id){
            this.id=id;
            this.relatedDisorders=new ArrayList<Disorder>()
        }

    }

The second method seems simpler Of course, I must instantiate the order object before I can reference them in the ArrayList Is one method better than the other?

Solution

The second method has no problem. In fact, it is often used Imagine trees or linkedlists, such as links that nodes save to other nodes If you create objects of the same type in the constructor, you must be careful, because this will cause the code to run forever

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>