当前位置: 代码迷 >> J2SE >> 关于方法覆盖的有关问题
  详细解决方案

关于方法覆盖的有关问题

热度:79   发布时间:2016-04-24 13:35:01.0
关于方法覆盖的问题
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;elsereturn false;
  相关解决方案