简体   繁体   English

如何在 JAVA 中实现这个 Class 图?

[英]How to implement this Class Diagram in JAVA?

I'm following a Class Diagram of a project.我正在关注一个项目的 Class 图。 and I got confused with this part:我对这部分感到困惑: 在此处输入图像描述

How am I supposed to create a variable containing a Class?我应该如何创建一个包含 Class 的变量? (I don't know if it's clear, but none of these classes are the main one) (我不知道是否清楚,但这些类都不是主要的)

Please, bare in mind that I'm a student, and I've never had to follow any Class Diagram before.请记住,我是一名学生,我以前从未遵循过任何 Class 图表。

Class diagram shows a composition between Historico and Usuario. Class 图显示了 Historico 和 Usuario 之间的组合。 It's a has relationship between (Historico has a Usuario).这是一个有关系(Historico 有一个 Usuario)。

You can define zero parameterized constructor like this:您可以像这样定义零参数化构造函数:

 class Usuario {
    public Usuario() {
   }
  }

more ever compiler will add zero parameter constructor if you don't define while byte code generation if no constructor defined.如果没有定义构造函数,则编译器将添加零参数构造函数,而在字节码生成时未定义。

You can define compostion like this:您可以像这样定义成分:

public class Historico {
  private Usuario responsavel;
}

Complete code:完整代码:

public class Usuario {
    String login;
    String senha;

    /**
     * @return the login
     */
    public String getLogin() {
        return login;
    }

    /**
     * @param login the login to set
     */
    public void setLogin(String login) {
        this.login = login;
    }

    /**
     * @return the senha
     */
    public String getSenha() {
        return senha;
    }

    /**
     * @param senha the senha to set
     */
    public void setSenha(String senha) {
        this.senha = senha;
    }

}

class Historico: class 历史:

 public class Historico {
    private Usuario responsavel;
    private String dataatualizacao;
    private String descricao;

    /**
     * @return the dataatualizacao
     */
    public String getDataatualizacao() {
        return dataatualizacao;
    }

    /**
     * @param dataatualizacao the dataatualizacao to set
     */
    public void setDataatualizacao(String dataatualizacao) {
        this.dataatualizacao = dataatualizacao;
    }

    /**
     * @return the descricao
     */
    public String getDescricao() {
        return descricao;
    }

    /**
     * @param descricao the descricao to set
     */
    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }

    public void setResponsavel(Usuario responsavel) {
        this.responsavel = responsavel;
    }
}

It would be something like this:它会是这样的:

class Usuario {

  public Usuario() {
    // TODO Auto-generated constructor stub
  }
}

class Historico {
  private Usuario responsavel;

  public Historico() {
    // TODO Auto-generated constructor stub
  }

  public Usuario getResponsavel() {
    return responsavel;
  }

  public void setResponsavel(Usuario responsavel) {
    this.responsavel = responsavel;
  }
}

public class Demo {
  public static void main(String[] args) {
    Usuario u = new Usuario();
    Historico h = new Historico();
    h.setResponsavel(u);
  }
}

You can create a variable for a class just like you create any other variable:您可以像创建任何其他变量一样为 class 创建变量:

// what you're probably used to:
int myInt;

Say you have a class Usuario as in the diagram, you can make a variable for it like this:假设您有一个Usuario ,如图所示,您可以为它创建一个变量,如下所示:

Usuario usuario;

Not sure exactly what you're asking, but you can even have instances of a class, point to other instances of the same class, for example with a node:不确定您到底在问什么,但您甚至可以拥有 class 的实例,指向相同 class 的其他实例,例如使用节点:

public class Node {
    public Node prev;
    public Node next;

    public Node(Node prev, Node next) {
        this.prev = prev;
        this.next = next;
    }
}

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

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