日期:2014-05-20  浏览次数:20698 次

关于方法覆盖的问题
class Student1
{
int age;
String name;
boolean equals(Object obj)
{
if(obj instanceof Student1)
{
return true;
Student1 stu=(Student1)obj;
if(this.age==stu.age&&this.name==stu.name)
return true;
else
return false;
}
else 
return false;
}
public static void main(String [] args)
{
Student1 stu1=new Student1();
Student1 stu2=new Student1();
stu1.age=12;
stu1.name="aaa";
stu2.age=12;
stu2.name="aaa";
if(stu1.equals(stu2))
System.out.println("stu1 is equals stu2!");
else
System.out.println("stu1 is not equals stu2");
}
}
-----------------------------------------------

--------------------配置: <默认>--------------------
F:\4\Student1.java:5: equals(java.lang.Object) in Student1 cannot override equals(java.lang.Object) in java.lang.Object; attempting to assign weaker access privileges; was public
  boolean equals(Object obj)
  ^
1 error


------解决方案--------------------
楼主的错误不少
1)return 执行后(return true;), 
Java code
Student1 stu=(Student1)obj;
if(this.age==stu.age&&this.name==stu.name)
return true;
else
return false;