已知类Student的定义如下:
public class Student {
String name;
Student(String name) {
this.name = name;
}
《插入代码》
}
main方法代码:
Student stu1=new Student(new String(“tom”));
Student stu2=new Student(“tom”);
System.out.print(stu1==stu2);
System.out.println(stu1.equals(stu2));
如果控制台的输出结果是false true,那么《插入代码》处填入的代码正确的是:A.public boolean equals(Object obj) {
if (obj instanceof Student) {
Student stu = (Student) obj;
return stu.name==this.name;
} else {
return false;
}
}B.public boolean equals(Object obj) {
if (obj instanceof Student) {
Student stu = (Student) obj;
return stu.name.equals(this.name);
} else {
return false;
}
}C.public boolean equals(Object obj) {
if (obj instanceof Student) {
return obj.name.equals(this.name);
} else {
return false;
}
}D.public boolean equals(Object obj) {
if (obj instanceof Student) {
return obj.name==this.name;
} else {
return false;
}
}答案:B
现问,为什么选b呢,其它选项为何不可呢,thanks
public class Student {
String name;
Student(String name) {
this.name = name;
}
《插入代码》
}
main方法代码:
Student stu1=new Student(new String(“tom”));
Student stu2=new Student(“tom”);
System.out.print(stu1==stu2);
System.out.println(stu1.equals(stu2));
如果控制台的输出结果是false true,那么《插入代码》处填入的代码正确的是:A.public boolean equals(Object obj) {
if (obj instanceof Student) {
Student stu = (Student) obj;
return stu.name==this.name;
} else {
return false;
}
}B.public boolean equals(Object obj) {
if (obj instanceof Student) {
Student stu = (Student) obj;
return stu.name.equals(this.name);
} else {
return false;
}
}C.public boolean equals(Object obj) {
if (obj instanceof Student) {
return obj.name.equals(this.name);
} else {
return false;
}
}D.public boolean equals(Object obj) {
if (obj instanceof Student) {
return obj.name==this.name;
} else {
return false;
}
}答案:B
现问,为什么选b呢,其它选项为何不可呢,thanks
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货