通用对类

Joh*_*tsy 12 java generics generic-programming

只是尝试这个问题,我在过去的考试试卷中发现,这样我就可以为即将进行的Java考试做准备.

提供用于表示事物对的通用类Pair.该类应该提供一个构造函数,一个获取该对的第一个成员的方法,一个获取该对的第二个成员的方法,一个用于设置该对的第一个成员的方法,一个用于设置该对的第二个成员的方法.该类应该针对第一个成员的两个类型进行参数化,并且对于该对的第二个成员进行参数化.

这是这个问题的正确实现吗?

public class Pair<firstThing, secondThing>{
   private firstThing first;//first member of pair
   private secondThing second;//second member of pair

   public Pair(firstThing first, secondThing second){
     this.first = first;
     this.second = second;
   }

   public void setFirst(firstThing first){
    this.first = first;
   }

   public void setSecond(secondThing second) {
     this.second = second;
   }

   public thing getFirst() {
     return this.first;
   }

   public thing getSecond() {
     return this.second;
   }
}
Run Code Online (Sandbox Code Playgroud)

Cla*_*ren 17

几乎.我写的是这样的:

public class Pair<F, S> {
    private F first; //first member of pair
    private S second; //second member of pair

    public Pair(F first, S second) {
        this.first = first;
        this.second = second;
    }

    public void setFirst(F first) {
        this.first = first;
    }

    public void setSecond(S second) {
        this.second = second;
    }

    public F getFirst() {
        return first;
    }

    public S getSecond() {
        return second;
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:我同意@ karmakaze的评论.代码应跳过setter并进行第一和第二次final以使其保持不变.

  • 如果没有'set'方法,我通常会使它成为不可变的. (13认同)

sim*_*tts 10

对类的需求通常在较大的项目中出现 - 我即将(重新)为当前项目实现一个(因为以前的实现是不可访问的).

通常我将它变成一个不可变的POJO,具有创建实例的便利功能.例如:

public class Pair<T,U>
{
    public final T first;
    public final U second;
    public static <T,U> Pair<T,U> of(T first, U second);
}
Run Code Online (Sandbox Code Playgroud)

这样最终用户就可以写:

return Pair.of (a, b);
Run Code Online (Sandbox Code Playgroud)

Pair<A,B> p = someThing ();
doSomething (p.first);
doSomethingElse (p.second);
Run Code Online (Sandbox Code Playgroud)

如上所述,Pair类还应该实现hashCode(),equals(),optional-but-useful toString(),可能是clone()和compareTo(),用于T和U支持的地方 - 尽管额外的工作需要描述Pair类如何支持这些契约.


pom*_*tee 5

这是Android SDK的一个实现

/**
 * Container to ease passing around a tuple of two objects. This object provides a sensible
 * implementation of equals(), returning true if equals() is true on each of the contained
 * objects.
 */
public class Pair<F, S> {
    public final F first;
    public final S second;

    /**
     * Constructor for a Pair.
     *
     * @param first the first object in the Pair
     * @param second the second object in the pair
     */
    public Pair(F first, S second) {
        this.first = first;
        this.second = second;
    }

    /**
     * Checks the two objects for equality by delegating to their respective
     * {@link Object#equals(Object)} methods.
     *
     * @param o the {@link Pair} to which this one is to be checked for equality
     * @return true if the underlying objects of the Pair are both considered
     *         equal
     */
    @Override
    public boolean equals(Object o) {
        if (!(o instanceof Pair)) {
            return false;
        }
        Pair<?, ?> p = (Pair<?, ?>) o;
        return Objects.equal(p.first, first) && Objects.equal(p.second, second);
    }

    /**
     * Compute a hash code using the hash codes of the underlying objects
     *
     * @return a hashcode of the Pair
     */
    @Override
    public int hashCode() {
        return (first == null ? 0 : first.hashCode()) ^ (second == null ? 0 : second.hashCode());
    }

    /**
     * Convenience method for creating an appropriately typed pair.
     * @param a the first object in the Pair
     * @param b the second object in the pair
     * @return a Pair that is templatized with the types of a and b
     */
    public static <A, B> Pair <A, B> create(A a, B b) {
        return new Pair<A, B>(a, b);
    }
}
Run Code Online (Sandbox Code Playgroud)