简体   繁体   English

如何实现POJO类的继承

[英]How to implement inheritance for POJO class

I have two different class with constructor but with similar pojo and i wanted to implement java inheritance as while running sonar code analysis gives me code duplicate 我有两个不同的类与构造函数,但有类似的pojo,我想实现java继承,因为运行声纳代码分析给我代码重复

I have created the classes DaoException and ServiceException which has same pojo and wanted to have the pojo common to avoid code duplicate 我已经创建了DaoException和ServiceException类,它们具有相同的pojo,并且希望将pojo通用以避免代码重复

public class DaoException extends Exception {

  private static final long serialVersionUID = 1L;

  private final int code;
  private String message;


  public DaoException(int code, String message) {

      this.code = code;
      this.message = message;
  }

  public DaoException(int code, Throwable throwable) {

      this.code = code;
      this.message = throwable.getMessage();
  }

  public int getCode() {

      return code;
  }

  public void setCode(int code) {

      this.code = code;
  }

  public String getMessage() {

      return message;
  }

  public void setMessage(String message) {

      this.message = message;
  }
}


public class ServiceException extends Exception {

private static final long serialVersionUID = 1L;

private final int code;
private String message;

public ServiceException(int code, String message) {

    this.code = code;
    this.message = message;
}

public ServiceException(int code, Throwable throwable) {

    this.code = code;
    this.message = throwable.getMessage();
}

public int getCode() {

    return code;
}

public void setCode(int code) {

    this.code = code;
}

public String getMessage() {

    return message;
}

public void setMessage(String message) {

    this.message = message;
}

} }

Please advise. 请指教。

You can create a common parent class for ServiceException and DAOExecption . 您可以为ServiceException和DAOExecption创建公共父类。 You can create your class GenericException , with all the details . 您可以使用所有详细信息创建类GenericException。 Then make ServiceException and DAOExecption extend this GenericException. 然后使ServiceException和DAOExecption扩展此GenericException。 Call the appropriate constructor of parent class from the child 从子进程调用父类的相应构造函数

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

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