黑马程序员技术交流社区
标题:
instanceof中碰到的问题
[打印本页]
作者:
hzhzhen
时间:
2015-5-18 11:02
标题:
instanceof中碰到的问题
在学习容器时,编写了一个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,程序就报错了,不知道为啥?
作者:
大兵在海边
时间:
2015-5-18 11:19
你类型转换的时候不用加new
作者:
zxj35986847
时间:
2015-5-18 12:38
18行Name name=new (Name)obj; 去掉new,
25行后加个大括号}
另外,为什么只有getName方法,没有setName方法?????
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2