我想知道我想要实现的目标实际上是否可行,我不知道我正在做什么的确切名称因此为什么我不能正确地谷歌搜索结果以及为什么这个主题标题也有点含糊不清
我的课程:
AccountConstraint.java:
package dao.constraint;
public class AccountConstraint {
private Range<Integer> accountId;
private String username;
private String password;
private String email;
public AccountConstraint(final Range<Integer> accountId, final String username, final String password, final String email) {
this.accountId = accountId;
this.username = username;
this.password = password;
this.email = email;
}
public Range<Integer> getAccountId() {
return accountId;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getEmail() {
return email;
}
}
Range.java:
package dao.constraint;
public class Range<T> {
private T min;
private T max;
public Range(T min, T max) {
this.min = min;
this.max = max;
}
public T getMin() {
return min;
}
public T getMax() {
return max;
}
}
一个完全有效的代码示例是:
AccountConstraint ac = new AccountConstraint(new Range<Integer>(5, 10), null, null, null));
如果您希望获得ID为5到10的所有帐户。仍然有效,但更奇怪的代码将是:
AccountConstraint ac = new AccountConstraint(new Range<Integer>(3, 3), null, null, null));
如果您想获取身份证号为3的帐户。
我想要的是:
AccountConstraint ac = new AccountConstraint(3, null, null, null);
因为new Range<Integer>(3, 3)
理论上等于3
。
有没有办法做到这一点,可能是通过向Range
类添加代码,我觉得这应该是可能的,但我不知道如何和/或从哪里开始。
答案 0 :(得分:5)
您无需对Range
课程执行任何操作,只需提供另一个AccountConstraint
构造函数,即Range
int
个public AccountConstraint(final int accountId, final String username, final String password, final String email) {
this(new Range<Integer>(accountId, accountId), username, password, email);
}
:
{{1}}
答案 1 :(得分:1)
创建更多构造函数:
public AccountConstraint(Integer accountId, final String username, final String password, final String email) {
this.accountId = new Range<Integer>(accountId);
this.username = username;
this.password = password;
this.email = email;
}
public Range(T num) {
this.min = num;
this.max = num;
}
答案 2 :(得分:0)
添加一个以Integer作为第一个参数的构造函数,并用它创建一个Range:
public AccountConstraint(Integer accountId, final String username, final String password, final String email) {
this(new Range<Integer>(accountId, accountId), username, password, email);
}
答案 3 :(得分:0)
你可以尝试这个(创建一个新的重载构造函数):
public AccountConstraint(final Range<Integer> accountId, final String username, final String password, final String email) {
this.accountId = accountId;
this.username = username;
this.password = password;
this.email = email;
}
//new overloaded constructor
public AccountConstraint(Integer value, final String username, final String password, final String email) {
Range<Integer> accountId = new Range(value,value);
super(accountId , username, password, email));
}
答案 4 :(得分:0)
像这样创建一个构造函数。将值传递给构造函数
public AccountConstraint(Integer i, final String username, final String password, final String email) {
this.accountId = new Range<Integer>(i,i)
this.username = username;
this.password = password;
this.email = email;
}
答案 5 :(得分:0)
您可以更改Range类以添加单个参数构造函数:
public Range(T t) {
min = t;
max = t;
}
并在单独的AccountConstraint构造函数中调用该构造函数。
顺便提一下,您的Range类可以从某些类型边界中受益:
public class Range<T extends Comparable<T>> {
public boolean contains(T value) {
return value.compareTo(min) >= 0
&& value.compareTo(max) <= 0;
}
// rest of class the same
}