如果有两个类的字段具有相似的含义,则可以考虑声明一个
interface。
Class1 implements MyInterface { int firstProperty; String secondProperty; ... int getOne() { return firstProperty; } String getTwo() { return secondProperty; }}Class2 implements MyInterface { int propertyOne; String propertyTwo; ... int getOne() { return propertyOne; } String getTwo() { return propertyTwo; ...}并且
interface具有默认实现
isEqualTo:
MyInterface { int getOne(); String getTwo(); ... boolean isEqualTo(MyInterface that) { return that != null && this.getOne() == that.getOne() && this.getTwo().equals(that.getTwo()) && //add null checks! ...; }}有
isEqualTo被覆盖的风险-确保它永远不会发生。



