//此处Public类的名称必须为Hello,用户不可修改。
public class Hello {
private int size;
private String name;
Hello(String name,int size)
{
this.name=name;
this.size=size;
}
public static void main(String[] args) {
//System.out.println("Hello Fenby!");
Hello helloKitty = new Hello("Kitty",3);
Hello helloWideWorld = new Hello("World",1000);
Hello helloAroundWorld = new Hello("World",1000);
if(helloKitty.equals(helloWideWorld))
{
System.out.println("helloKitty and helloWideWorld have the same meaning");
}
else System.out.println("helloKitty and helloWideWorld don't have the same meaning");
if(helloAroundWorld.equals(helloWideWorld))
{
System.out.println("helloAroundWorld and helloWideWorld have the same meaning");
}
else System.out.println("helloAroundWorld and helloWideWorld don't have the same meaning");
}
}
Java 对象的行为和方法一章的最后一节变量的比较,讲到equals方法:使用equals()来判断两个对象是否在意义上相等。课后练习中用equals对两个没有内容的对象进行判断,返回值是false。我就借鉴视频中的例子自己写了一个,可是helloAroundWorld和helloWideWorld用equals比较还是返回false。这里很疑惑。
大家看一看。为什么会这样呢?
|