2010-08-13 122 views
0

这里是我的compareTo方法,但即时通讯仍然收到“缺少返回语句”警告。 任何人都可以告诉我我的代码有什么问题吗?如何覆盖compareTo方法

public int compareTo(Flows other) { 
    if(this.srcAddr.equals(other.srcAddr)){ 
     if(this.dstAddr.equals(other.dstAddr)){ 
       if(this.srcPort.equals(other.srcPort)){ 
        if(this.dstPort.equals(other.dstPort)){ 
         if(this.protocol.equals(other.protocol)){ 
          return 0; 
         } 
        } 
       } 
     } 
} 
} 
+1

首先,为什么这个社区wiki?其次,如果'this.srcAddr.equals(other.srcAddr)'为false,请考虑返回的内容;]。 – pablochan 2010-08-13 09:30:06

+0

相信我你不能正确实现这个,直到你真的知道你想如何订购你的“MyKey”对象:)首先决定你的业务逻辑是什么。 – Gopi 2010-08-13 09:35:34

+0

@Gopi,你的意思是,Flows对象? – aioobe 2010-08-13 09:39:51

回答

0

只是在函数的末尾添加“return 1”(或任何东西),它应该可以解决问题。

+0

其实我做了,但它没有奏效。 – 2010-08-13 09:34:12

+0

public int compareTo(Flows other)if(this.srcAddr.equals(other.srcAddr)){if(this.dstAddr.equals(other.dstAddr)){ if(this.srcPort.equals(other。如果(this.dstPort.equals(other.dstPort)){ if(this.protocol.equals(other.protocol)){ return 0; } } } } } else return 1; } – 2010-08-13 09:36:16

+2

你说“'返回1”(或任何东西)“ - 这将解决编译错误问题,但不是'compareTo'合同违反问题。 – polygenelubricants 2010-08-13 09:55:58

1

这是因为您的代码有可能为compareTo返回任何内容!想想如果所有这些if语句都失败了,那么它会触发方法的结尾,而不会返回任何东西。你需要一个回报进一步下跌:

public int compareTo(Flows other) { 
    if(this.srcAddr.equals(other.srcAddr)){ 
     if(this.dstAddr.equals(other.dstAddr)){ 
      if(this.srcPort.equals(other.srcPort)){ 
       if(this.dstPort.equals(other.dstPort)){ 
        if(this.protocol.equals(other.protocol)){ 
         return 0; 
        } 
       } 
      } 
     } 
    } 
    return 1; 

}

而且你是不是做一个完整的比较。如果它们相等,则需要返回0;如果差值小于0,则小于0;如果大于0,则大于0。这看起来你会更好,最重要的是平等!

也许是这样的:

public boolean equals(Flows other) { 
    return (this.srcAddr.equals(other.srcAddr) && this.dstAddr.equals(other.dstAddr) && this.srcPort.equals(other.srcPort) && this.dstPort.equals(other.dstPort) && this.protocol.equals(other.protocol)); 
+0

即时比较两个对象的5个字段只是为了查看是否已经存在该对象。平等是我对这种情况的兴趣。 – 2010-08-13 09:49:58

+0

然后重写我有的等于方法 – BeRecursive 2010-08-13 09:52:30

+0

。但即时通讯使用sortedMap数据结构。我需要实现compareTo,equal和hashCode。我做了其他两个,但不compareTo。 compareTo的目的是检查不相等的顺序。 – 2010-08-13 10:14:11

0

这将编译和运行,但对于合同的休息吗?小于和大于?

public int compareTo(Flows other) { 

    int value = 0; 

    if(this.srcAddr.equals(other.srcAddr)){ 
     if(this.dstAddr.equals(other.dstAddr)){ 
       if(this.srcPort.equals(other.srcPort)){ 
        if(this.dstPort.equals(other.dstPort)){ 
         if(this.protocol.equals(other.protocol)){ 
          value = 0; 
         } 
        } 
       } 
     } 

    return value; 
} 
+3

这个代码在任何情况下都返回0。 – 2010-08-13 09:45:16

+0

是的,我明白了,但是打破了compareTo合同。 – duffymo 2010-08-13 13:01:59

2

这看起来像一个equals方法。如果目的仅仅是为了比较,如果两者是一样的,我会做类似

return srcAddr.equals(other.srcAddr) && 
     dstAddr.equals(other.dstAddr) && 
     srcPort.equals(other.srcPort) && 
     dstPort.equals(other.dstPort) && 
     protocol.equals(other.protocol); 

如果它意向,你可能打破的compareTo的合同,因为你的方法没有按” t似乎坚持传递性要求。从Comparable文档:

实现类还必须确保关系是可传递

+0

我使用sortedMap和我需要实现compareTo方法有我的领域,我想要的顺序。这就是为什么我要这样。 – 2010-08-13 09:41:52

+0

好吧,请记住,您的compareTo方法必须在* transitive *的Flow对象上实现关系。 – aioobe 2010-08-13 10:02:32

5

两件事情:

  • 你得到“失踪return语句”,因为有路没有返回值的执行。例如,当第一个if语句计算为false时。

  • 您打破了compareTo()合同。对于以下调用:a.compareTo(b),结果应为为:如果a等于b,则为0,<如果a小于b,则为0;如果a大于b,则为> 0。看来你正在使用compareTo()来检查是否相等,在这种情况下,正确的方法是重写equals()方法。