class Golfball
{
private String brand;
private String make;
private int compression;
public boolean equals(Object obj)
{ /* if (this == obj)
return true;*/为什么这儿要有这一段代码,没有也能编译通过啊. if (obj != null && getClass() == obj.getClass())
{
Golfball gb = (Golfball)obj;
if (brand.equals(gb.brand()) &&
make.equals(gb.make()) &&
compression == gb.compression())
{
return true;
}
}
return false;
} public Golfball (String str, String mk,int comp)
{
brand = str;
make = mk;
compression = comp;
} public String brand()
{
return brand;
} public String make()
{
return make;
} public int compression()
{
return compression;
}
}public class Warehouse
{
public static void main(String args[])
{
Golfball gb1 = new Golfball("BrandX", "Professional",100);
Golfball gb2 = new Golfball("BrandX", "Professional",100);
//...
if (gb1.equals(gb2))
System.out.println("Ball 1 equals Ball 2");
else
System.out.println("Ball 1 does not equal Ball 2");
}
}
{
private String brand;
private String make;
private int compression;
public boolean equals(Object obj)
{ /* if (this == obj)
return true;*/为什么这儿要有这一段代码,没有也能编译通过啊. if (obj != null && getClass() == obj.getClass())
{
Golfball gb = (Golfball)obj;
if (brand.equals(gb.brand()) &&
make.equals(gb.make()) &&
compression == gb.compression())
{
return true;
}
}
return false;
} public Golfball (String str, String mk,int comp)
{
brand = str;
make = mk;
compression = comp;
} public String brand()
{
return brand;
} public String make()
{
return make;
} public int compression()
{
return compression;
}
}public class Warehouse
{
public static void main(String args[])
{
Golfball gb1 = new Golfball("BrandX", "Professional",100);
Golfball gb2 = new Golfball("BrandX", "Professional",100);
//...
if (gb1.equals(gb2))
System.out.println("Ball 1 equals Ball 2");
else
System.out.println("Ball 1 does not equal Ball 2");
}
}
return true;*/为什么这儿要有这一段代码,没有也能编译通过啊.就是判断自己与自己相不相等没有肯定可以