总结一下这两天复习的东东-《训练营2010年java高新技术》 ,对我最大的感受就是张老师讲的知识点有一定的深度,比如spring的aop,beanfactory,以前只知道使用,不知道其中的原理(spring应用了大量的反射技术),好了,下面大概做个知识点的总结
hashCode的作用:判断两个对象的哈希值是否相等,HashMap 和 ArrayList 的添加原则,HashMap 和 ArrayList 的添加原则,注解annotation,menu枚举的使用,泛型,反射,spring框架的aop,动态代理原理看例子吧,
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collection;
import java.util.Properties;
public class HashCodeTest {
public static void main(String[] args) throws Exception{
// Collection c = new ArrayList();
InputStream in = new FileInputStream("config.properties");
//加载配置文件 用到 Properties 对象哈
Properties props = new Properties();
props.load(in);
in.close();
// java.util.HashSet
String className = (String)props.get("className");
Collection c = (Collection)Class.forName(className).newInstance();
//Collection c = new HashSet();
ReflectPoint p1 = new ReflectPoint(3, 3);
ReflectPoint p2 = new ReflectPoint(4, 4);
ReflectPoint p4 = new ReflectPoint(3, 3);
/*
* hashcode
* Set 不容许相同的元素加入 每一个对象要存入set集合, 都要计算hashcode值,根据内存地址来
* 计算的(这个是你没有复写hashCode()方法),你知道吗, 所以这个肯定不相等,
*/
c.add(p1);
c.add(p2);
c.add(p4);
// 没有复写ReflectPoint equals 方法时
// 当HashSet时添加元素时,必须检查有没有这个元素
System.out.println(c.size());
p1.x = 8;
//对象被加入到Set集合中去,就不要去修改那些影响
//hashcode值的元素了,不然就无法删除rm,照成内存溢出
c.remove(p1);
// 内存地址
System.out.println(p1 == p4);
// 复写equals方法,一般是比较它们的内容,所以相等
System.out.println(p1.equals(p4));
System.out.println(c.size());
}
}
public class ReflectPoint {
public int x;
private int y;
public ReflectPoint(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ReflectPoint other = (ReflectPoint) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}
config.properties属性文件的内容
className=java.util.HashSet