简体   繁体   English

接口中的 toString()、equals() 和 hashCode()

[英]toString(), equals(), and hashCode() in an interface

So, I have an interface with a bunch of methods that need to be implemented, the method names are irrelevant.所以,我有一个接口,里面有一堆需要实现的方法,方法名称无关紧要。

The objects that implement this interface are often put into collections, and also have a special toString() format that I want them to use.实现此接口的对象通常放入集合中,并且还有我希望它们使用的特殊 toString() 格式。

So, I thought it would be convenient to put hashCode(), equals(), and toString() into the interface, to make sure that I remember to override the default method for these.所以,我认为将 hashCode()、equals() 和 toString() 放入接口会很方便,以确保我记得覆盖这些的默认方法。 But when I added these methods to the interface, the IDE/Compiler doesn't complain if I don't have those three methods implemented, even though I explicitly put them in the interface.但是当我将这些方法添加到接口时,IDE/编译器不会抱怨如果我没有实现这三个方法,即使我明确地将它们放在接口中。

Why won't this get enforced for me?为什么这不会对我强制执行? It complains if I don't implement any of the other methods, but it doesn't enforce those three.如果我不实现任何其他方法,它会抱怨,但它不会强制执行这三个方法。 What gives?是什么赋予了? Any clues?有什么线索吗?

It sounds like you want to force your classes to override the default implementations of those methods.听起来您想强制您的类覆盖这些方法的默认实现。 If so, the way to do this is to declare an abstract superclass that has the methods declared as abstract.如果是这样,这样做的方法是声明一个抽象超类,该超类将方法声明为抽象。 For example:例如:

public abstract class MyBaseClass implements ... /* existing interface(s) */ {

    public abstract boolean equals(Object other);

    public abstract int hashCode();

    public abstract String toString();
}

Then change your current classes to extend this class.然后更改您当前的类以extend该类。

This approach kind of works, but it is not an ideal solution.这种方法有效,但不是理想的解决方案。

  • It can be problematic for an existing class hierarchy.对于现有的类层次结构,这可能是有问题的。

  • It is a bad idea to force classes that implement your existing interface to extend a specific abstract class.强制实现现有接口的类扩展特定的抽象类是一个坏主意。 For example, you can change parameters in method signatures to use the abstract class rather than the existing interface(s).例如,您可以更改方法签名中的参数以使用抽象类而不是现有接口。 But the end result is less flexible code.但最终的结果是代码不太灵活。 (And people can find ways to subvert this anyway; eg by adding their own abstract subclass that "implements" the methods with a super.<method>(...) call!) (而且人们无论如何都可以找到颠覆这一点的方法;例如,通过添加他们自己的抽象子类,使用super.<method>(...)调用“实现”这些方法!)

  • Imposing a particular class hierarchy / implementation pattern is short sighted.强加特定的类层次结构/实现模式是短视的。 You cannot predict whether some future requirement change will mean that your restrictions cause difficulties.您无法预测未来的某些需求更改是否意味着您的限制会造成困难。 (This is why people recommend programming against interfaces rather than specific classes.) (这就是为什么人们推荐针对接口而不是特定类进行编程的原因。)


Back to your actual question about why your interface doesn't force a class to redeclare those methods:回到关于为什么您的接口不强制类重新声明这些方法的实际问题:

Why won't this get enforced for me?为什么这不会对我强制执行? It complains if I don't implement any of the other methods, but it doesn't enforce those three.如果我不实现任何其他方法,它会抱怨,但它不会强制执行这三个方法。 What gives?是什么赋予了? Any clues?有什么线索吗?

An interface imposes the constraint that a concrete class implementing it has an implementation for each of the methods.接口强加了一个约束,即实现它的具体类对每个方法都有一个实现。 However, it doesn't require that the class itself provides those methods.但是,它并不要求类本身提供这些方法。 The method implementations can be inherited from a superclass.方法实现可以从超类继承。 And in this case, that is what is happening.在这种情况下,这就是正在发生的事情。 The methods inherited from java.lang.Object saftisfy the constraint.java.lang.Object继承的方法满足约束。

JLS 8.1.5 states the following: JLS 8.1.5声明如下:

"Unless the class being declared is abstract, all the abstract member methods of each direct superinterface must be implemented (§8.4.8.1) either by a declaration in this class or by an existing method declaration inherited from the direct superclass or a direct superinterface , because a class that is not abstract is not permitted to have abstract methods (§8.1.1.1)." “除非声明的类是抽象类,否则每个直接超接口的所有抽象成员方法都必须通过此类中的声明或从直接超类或直接超接口继承的现有方法声明来实现(第 8.4.8.1 节),因为非抽象类不允许具有抽象方法(第 8.1.1.1 节)。”

All objects in Java inherit from java.lang.Object and Object provides default implementations of those methods. Java 中的所有对象都继承自java.lang.Object并且Object提供了这些方法的默认实现。

If your interface contains other methods, Java will complain if you don't implement the interface fully by providing an implementation of those methods.如果您的接口包含其他方法,如果您没有通过提供这些方法的实现来完全实现该接口,Java 会抱怨。 But in the case of equals() , hashCode() and toString() (as well as a few others that you didn't mention) the implementation already exists.但是在equals()hashCode()toString() (以及您没有提到的其他一些)的情况下,实现已经存在。

One way you might be able to accomplish what you want is by providing a different method in the interface, say, toPrettyString() or something like that.您可能能够完成所需的一种方法是在接口中提供不同的方法,例如toPrettyString()或类似的方法。 Then you can call that method instead of the default toString() method.然后您可以调用该方法而不是默认的toString()方法。

All 3 of those methods are defined by java.lang.Object which is (implicitly) extended by all other classes;所有这 3 个方法都由java.lang.Object定义,它由所有其他类(隐式)扩展; therefore default implementations for those methods exist and compiler has nothing to complain about.因此这些方法的默认实现存在,编译器没有什么可抱怨的。

Any class that implements your interface also extends Object.任何实现您的接口的类也扩展了 Object。 Object defines hashCode, equals, and toString and has default implementations of all three. Object 定义了 hashCode、equals 和 toString,并且具有所有这三个的默认实现。

What you are trying to achieve is good, but not practicable.你想要达到的目标是好的,但不切实际。

这些方法的实现一直来自Object

如果要强制覆盖 equals() 和 hashCode(),请从抽象超类扩展,该超类将这些方法定义为抽象方法。

您的对象已经包含这三个方法的实现,因为每个对象都从 Object 继承这些方法,除非它们被覆盖。

Java only cares that the methods are defined somewhere. Java 只关心方法是否在某处定义。 The interface doesn't force you to redefine the methods in new classes that inherit from the interface for the first time if they're already defined.接口不会强制您在第一次从接口继承的新类中重新定义方法(如果它们已经定义)。 Since java.lang.Object already implements these methods, your new objects conform to the interface even if they don't override these three methods on their own.因为java.lang.Object已经实现了这些方法,所以你的新对象即使不重写这三个方法也符合接口。

Other people have answered your actual question sufficiently.其他人已经充分回答了您的实际问题。 As far as a solution for your particular problem, you might consider creating your own methods (maybe getStringRepresentation, getCustomHashcode, and equalsObject), and have your objects extend a base class whose equals, toString, and hashCode methods call these methods.至于您的特定问题的解决方案,您可能会考虑创建自己的方法(可能是 getStringRepresentation、getCustomHashcode 和 equalsObject),并让您的对象扩展一个基类,该基类的 equals、toString 和 hashCode 方法调用这些方法。

This might defeat the purpose of using an interface in the first place, though.不过,这可能会破坏使用接口的初衷。 This is one of the reasons that some people have suggested that equals, toString, and hashCode should never have been included on the Object class in the first place.这就是一些人建议从一开始就不应将 equals、toString 和 hashCode 包含在 Object 类中的原因之一。

Adam gives you the reason why you cannot get away with trying to force equals, hashCode and toString. Adam 为您提供了为什么您无法摆脱尝试强制使用 equals、hashCode 和 toString 的原因。 I would go for the following implementation which touches the solution provided by Adam and Stephan:我将采用以下实现,其中涉及 Adam 和 Stephan 提供的解决方案:

public interface Distinct {
    boolean checkEquals(Object other);
    int hash();
}

public interface Stringable {
    String asString();
}

public abstract class DistinctStringableObject {

    @Override
    public final boolean equals(Object other) {
        return checkEquals();
    }

    @Override
    public final int hashCode() {
        return hash();
    }

    @Override
    public final String toString() {
        return asString();
    }
}

Now, any class which requires itself to definitely distinct and represented as a String can extend the DistinctStringableObject which will force implementation of checkEquals, hashCode and toString.现在,任何要求自己明确区分并表示为字符串的类都可以扩展 DistinctStringableObject,这将强制实现 checkEquals、hashCode 和 toString。

Sample concrete class:示例具体类:

public abstract class MyDistinctStringableObject extends DistinctStringableObject {

    @Override
    public final boolean checkEquals(Object other) {
        ...
    }

    @Override
    public final int hash() {
        ...
    }

    @Override
    public final String asString() {
        ...
    }
}

Abstract classes won't work if you have a grandchild since its father already overrided both equals and hashCode methods and then you have your problem all over again.如果你有一个孙子,抽象类将不起作用,因为它的父亲已经覆盖了 equals 和 hashCode 方法,然后你的问题又来了。

Try using annotatins and APT ( http://docs.oracle.com/javase/1.5.0/docs/guide/apt/GettingStarted.html ) to get it done.尝试使用注释和 APT ( http://docs.oracle.com/javase/1.5.0/docs/guide/apt/GettingStarted.html ) 来完成它。

well if u have declared an interface in which by default all the methods are abstract and u do need to provide the functionality but when u implement it in the subclass, then u provide the implementation right.好吧,如果您声明了一个接口,其中默认情况下所有方法都是抽象的,并且您确实需要提供功能,但是当您在子类中实现它时,那么您提供了实现权。 as you can see that every class is the subclass of one superclass(put simply Object is superclass of all the classes) so if u have a class implementing an interface, u need to provide implementation for the methods.正如你所看到的,每个类都是一个超类的子类(简单地说,Object 是所有类的超类),所以如果你有一个实现接口的类,你需要提供方法的实现。 but a thing needs to be remembered here that.但这里需要记住一件事。

irrespective of, if u didn't declare these methods in the interface, you still had this behavior for the subclass that implemented the interface in the first place.不管怎样,如果你没有在接口中声明这些方法,你仍然对首先实现接口的子类有这种行为。

so if don't declare it, it still will be present and another thing is that since these methods and other methods of Object class are present for all the objects of classes, so there is no need for implementation.所以如果不声明,它还是会存在的,另外,由于Object类的这些方法和其他方法对于类的所有对象都存在,所以不需要实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM