在学习容器时,编写了一个Name类,如下
- public class Name {
- private String firstName,secondName;
- public Name(String firstName,String secondName){
- this.firstName=firstName;
- this.secondName=secondName;
- }
- public String getfirstName(){
- return firstName;
- }
- public String getsecondName(){
- return secondName;
- }
- public String toString(){
- return (firstName+" "+secondName);
- }
- public boolean equals(Object obj){
- if(obj instanceof Name){
- Name name=new (Name)obj;
- return (firstName.equals (Name.this))&&(secondName.equals(Name.this));
- }
- return super.equals(obj);
- }
- public int HashCode(){
- return firstName.hashCode();
- }
复制代码
重写equals方法时,比较一下obj是否属于Name,程序就报错了,不知道为啥? |
|