我有两个带有构造函数的不同类,但具有相似的pojo,并且我想实现Java继承,因为在运行声纳代码分析时会给我代码重复
我创建了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;
}
}
请告知。
答案 0 :(得分:0)
您可以为ServiceException和DAOExecption创建一个公共父类。您可以创建带有所有详细信息的类GenericException。 然后使ServiceException和DAOExecption扩展此GenericException。从孩子那里调用适当的父类的构造函数