通用对类

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

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

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

public class Pair{ 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; } } 

几乎。 我这样写的:

 public class Pair { 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; } } 

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

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

通常我将它设为不可变的POJO,具有创建实例的便利function。 例如:

 public class Pair { public final T first; public final U second; public static  Pair of(T first, U second); } 

这样最终用户就可以写:

 return Pair.of (a, b); 

 Pair p = someThing (); doSomething (p.first); doSomethingElse (p.second); 

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

您可以查看标准Java类AbstractMap.SimpleEntry和AbstractMap.SimpleImmutableEntry的实现 。 谷歌来源很容易:

这是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 { 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  Pair  create(A a, B b) { return new Pair(a, b); } } 

我想没有。引用:

“这个类应该参数化两种类型…”

我认为他们期望:

 public class Pair 

通常,通用Pair类型有两个generics类型参数,而不是一个 – 所以你可以拥有(比方说) Pair 。 这通常更有用,IMO。

我还建议您考虑类型参数比“事物”更常规的名称。 例如,您可以使用PairPair

吸气剂坏了

 public thing getFirst() { return thing.first; } public thing getSecond() { return thing.second; } 

应该用this替换

编辑后,它看起来不错。

但是,您确实应该实现hashCodeequals方法,以便包含相同对象的两对将彼此相等,并且可以在HashMap中用作键。 如果你感觉很慷慨,那就去串行了。 这些方法不需要满足您给出的要求,但它们是优秀程序员可以添加的内容。

该类应该针对第一个成员的两个类型进行参数化,并且对于该对的第二个成员进行参数化。

您只有一个参数。

你需要像Pair thing ,并且在你首先使用的thing和使用第二个thing S的地方使用F.

不,您是否尝试过编码以查看它是否有效?

您似乎错过了这部分要求:

该类应该针对第一个成员的两个类型进行参数化,并且对于该对的第二个成员进行参数化。

这意味着该类应该被定义为更像:

public class Pair

其他方法也相应更新。 (顺便说一句,我已经使用T1和T2来引用类型,按照惯例,使用short – 1或2 char – 标识符)。

也,

return thing.first;

return thing.second;

不会起作用,因为在你的例子中, thing是一种类型,而不是一个对象。 想想你想要回到这里。 你甚至需要打电话给方法吗?

完成更改后,对其进行编码,然后编写unit testing或简单的测试工具来检查它是否有效。

thing是一个非常规符号的Type变量 – 我们通常使用一个大写的后者(如T )。 然后:一个类型变量没有任何方法,所以你的getter不会编译。

快速改进:用T替换所有thing

快速修复getter:

 public T getFirst() { return first; } public T getSecond() { return second; } 

一个要求是允许两对不同类型的成员。 所以类签名应如下所示:

 public Pair { private S first; private T second; //... } 

我的双版本。 这也处理比较。 PS:大部分代码都来自AOSP。

 package util; import java.util.Objects; public class Pair, S extends Comparable> implements Comparable> { 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.equals(p.first, first) && Objects.equals(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 , B extends Comparable> Pair create(A a, B b) { return new Pair<>(a, b); } @Override public int compareTo(Pair that) { int cmp = this.first.compareTo(that.first); if (cmp == 0) cmp = this.second.compareTo(that.second); return cmp; } } 

我实现了类似的东西,但使用静态构建器和链式setter

 public class Pair { private R left; private L right; public static  Pair of(K k, V v) { return new Pair(k, v); } public Pair() {} public Pair(R key, L value) { this.left(key); this.right(value); } public R left() { return left; } public Pair left(R key) { this.left = key; return this; } public L right() { return right; } public Pair right(L value) { this.right = value; return this; } }