Eclipse喜欢为没有超类的类生成具有
然而,我喜欢。
更易读,因为它更加可读性强。(这总是让我感到不安。)
问题:
为什么Eclipse生成的代码中,
更新:
Eclipse Kepler似乎生成
类之前:
课后:
我认为我们需要清理一下我们在Kepler之前生成的
null
检查的equals
方法,就像这样:if ( null == obj )
return false;
然而,我喜欢。
if ( obj == null )
return false;
更易读,因为它更加可读性强。(这总是让我感到不安。)
问题:
为什么Eclipse生成的代码中,
null
出现在obj
之前?因为if ( null == obj )
是一种过时的做法,源自C/C++,如此描述:(obj == null) vs (null == obj)?
这两种方式之间是否有(运行时)差异?我只能猜测...更新:
Eclipse Kepler似乎生成
if ( obj == null )
,因此这仅适用于早期版本的Eclipse。类之前:
public class Burp
{
private Long id ;
public Burp()
{
// test
}
}
课后:
public class Burp
{
private Long id ;
public Burp()
{
// test
}
// hashCode() omitted
@Override
public boolean equals( Object obj )
{
if ( this == obj )
return true;
if ( obj == null )
return false;
if ( getClass() != obj.getClass() )
return false;
Burp other = ( Burp ) obj;
if ( this.id == null )
{
if ( other.id != null )
return false;
}
else if ( !this.id.equals( other.id ) )
return false;
return true;
}
}
我认为我们需要清理一下我们在Kepler之前生成的
equals
方法。
if ( obj = null )
会产生一个错误。 - Kawu