1

我想知道我想要实现的目标是否真的可行,我不知道我在做什么的确切名称,因此为什么我不能真正正确地用谷歌搜索结果,为什么这个主题标题也有点模糊。

我的课程:

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;
    }
}

范围.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));

如果您想获得 id 为 3 的帐户。

我想要的是:

AccountConstraint ac = new AccountConstraint(3, null, null, null);

asnew Range<Integer>(3, 3)理论上等于3

有没有办法做到这一点,可能通过向Range类添加代码,我觉得这应该是可能的,但我不知道如何和/或从哪里开始。

4

6 回答 6

5

您不需要对您的类做任何事情,Range只需提供另一个AccountConstraint构造函数即可:Rangeint

public AccountConstraint(final int accountId, final String username, final String password, final String email) {
    this(new Range<Integer>(accountId, accountId), username, password, email);
}
于 2013-06-28T11:58:40.413 回答
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;
}
于 2013-06-28T11:59:35.797 回答
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);
}
于 2013-06-28T12:00:44.123 回答
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));
}
于 2013-06-28T12:01:27.983 回答
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;
        }
于 2013-06-28T12:01:30.830 回答
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 
}
于 2013-06-28T12:03:50.980 回答