一个查看源代码的问题
2015-08-31 09:58
435 查看
代码如下:
package java1;
import java.util.Set;
import java.util.HashSet;
class Person
{
private String name;
private int age;
public Person(String name ,int age)
{
this.name=name;
this.age=age;
};
public String toString()
{
return "姓名:"+this.name+";年龄"+this.name;
}
public boolean equals(Object obj)
{
if(this==obj)
{
return true;
}
if(!(obj instanceof Person))
{
return false;
}
Person p=(Person) obj;
if(this.name.equals(p.name)&&this.age==p.age)
{
return true;
}
else
{
return false;
}
}
public int hashCode()
{
return this.name.hashCode()*this.age;
}
}
public class nicai{
public static void main(String args[])
{
Set<Person> allset=new HashSet<Person>();
allset.add(new Person("张三",30));
allset.add(new Person("李四",31));
allset.add(new Person("王五",32));
allset.add(new Person("王五",32));
allset.add(new Person("王五",32));
allset.add(new Person("赵六",33));
allset.add(new Person("孙七",33));
System.out.println(allset);
}
}
代码的本身没问题,我思考的是代码中是怎么调用equal()方法以及hashCode方法的,于是就去查看add的源代码,我先点击allset.add(new Person)的add源代码发现只是跳转到了 boolean add(E e);并没有它的源码,刚开始我怀疑是我的编辑器打不开源码,后来发现,allset是Set类,HashSet是Set的子类,Set<Person> allset=new HashSet<Person>()这条语句把allset实例化为Set的一个子类也就是HashSet的一个对象,那就是说allset.add调用的方法当然是在HashSet()类中覆写过得方法,所以应该去寻找HashSet类中的add方法,果然找到了。
package java1;
import java.util.Set;
import java.util.HashSet;
class Person
{
private String name;
private int age;
public Person(String name ,int age)
{
this.name=name;
this.age=age;
};
public String toString()
{
return "姓名:"+this.name+";年龄"+this.name;
}
public boolean equals(Object obj)
{
if(this==obj)
{
return true;
}
if(!(obj instanceof Person))
{
return false;
}
Person p=(Person) obj;
if(this.name.equals(p.name)&&this.age==p.age)
{
return true;
}
else
{
return false;
}
}
public int hashCode()
{
return this.name.hashCode()*this.age;
}
}
public class nicai{
public static void main(String args[])
{
Set<Person> allset=new HashSet<Person>();
allset.add(new Person("张三",30));
allset.add(new Person("李四",31));
allset.add(new Person("王五",32));
allset.add(new Person("王五",32));
allset.add(new Person("王五",32));
allset.add(new Person("赵六",33));
allset.add(new Person("孙七",33));
System.out.println(allset);
}
}
代码的本身没问题,我思考的是代码中是怎么调用equal()方法以及hashCode方法的,于是就去查看add的源代码,我先点击allset.add(new Person)的add源代码发现只是跳转到了 boolean add(E e);并没有它的源码,刚开始我怀疑是我的编辑器打不开源码,后来发现,allset是Set类,HashSet是Set的子类,Set<Person> allset=new HashSet<Person>()这条语句把allset实例化为Set的一个子类也就是HashSet的一个对象,那就是说allset.add调用的方法当然是在HashSet()类中覆写过得方法,所以应该去寻找HashSet类中的add方法,果然找到了。
相关文章推荐
- Python-切片
- DecimalFormat用法
- 【Java】Java实现多线程下载
- EA代码嵌入VS项目
- CoreAnimation-06-CAKeyframeAnimation 相关代码
- 使用eclipse调试SylixOS应用
- 【Java】Java单例模式的七种写法 (全)
- 【java】贪吃蛇小游戏
- C++11模版元编程的应用
- C++四种强制类型转换运算符
- Python, pitfall of creating 2d list, [foo*3]*3
- eclipse中如何导入jar包源码
- c#导入导出excel(无格式控制)
- c#基础笔记(1) 枚举
- Json for Java
- 使用JavaIO提供的API下载指定文件(image)
- 报表导出到word或者excel的功能实现
- 浅析Java内存模型( 深入理解Java虚拟机))
- 【struts2】struts2配置中Action的name和package的name和namespace有什么作用 ?
- windows和ubuntu下使用python写备份脚本