---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------
首先List都有序(存入和取出顺序一致)、元素都有索引,元素可以重复
Set 无序,元素不可以重复
实例:
第一种情况:定义普通类Point,没有重写equal()和HashCode()方法
Point类
public class Point {
private int x;
public int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
ReflectDemo 类
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
public class ReflectDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// Collection coll=new ArrayList();输出结果:4
Collection coll = new HashSet(); // 输出结果:3
Point p1 = new Point(3, 3);
Point p2 = new Point(3, 5);
Point p3 = new Point(3, 3);
coll.add(p1);
coll.add(p2);
coll.add(p3);
coll.add(p1);
System.out.println(coll.size());
}
}
现在我重写Point类中的equals()方法和HashCode()方法
Point类
public class Point {
private int x;
public int y;
public Point(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;
Point other = (Point) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}
ReflectDemo 类
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
public class ReflectDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Collection coll = new ArrayList();// 输出结果:4
// Collection coll=new HashSet();//输出结果:2
Point p1 = new Point(3, 3);
Point p2 = new Point(3, 5);
Point p3 = new Point(3, 3);
coll.add(p1);
coll.add(p2);
coll.add(p3);
coll.add(p1);
System.out.println(coll.size());
}
}
由此可总结出HashCode()方法的作用:(如何保证HashSet集合元素的唯一性呢? 通过hashcode与equals方法来完成对象的唯一性的)
如果对象的HashCode值不同,则视为不同元素,直接存储到哈希表中,如果对象的HashCode值相同,则再判断equals,若值为true,视为相同,不存,否则,视为不同元素,进行存储
反射的作用:实现框架功能
现在修改上面代码:
使得在ReflectDemo中具体创建哪个类有配置文件决定,我们只需要修改配置文件,就可以动态修改所要创建的类
配置文件:config.properties
className=java.util.HashSet
ReflectDemo类
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Properties;
public class ReflectDemo {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Properties prop = new Properties();
InputStream is = new FileInputStream(new File("config.properties"));
prop.load(is);
is.close();
String className = prop.getProperty("className");
Collection coll = (Collection) Class.forName(className).newInstance();
// Collection coll=new HashSet();
Point p1 = new Point(3, 3);
Point p2 = new Point(3, 5);
Point p3 = new Point(3, 3);
coll.add(p1);
coll.add(p2);
coll.add(p3);
coll.add(p1);
System.out.println(coll.size());
}
}
用类加载器的方法管理资源和配置文件
注意这条语句:
InputStream is=new FileInputStream(new File("config.properties"));
实际编程中没有这么写的,实际中没有用到相对路径的
修改代码:
(其实在我们编好程序给用户使用时,发给用户的只是编译完成的class包,并没有源码)
所以我们把config.properties文件放在这里肯定是不合适的 ,如果放在这里,那么我们打开源码就会看到,config,properties是放在包的根目录下的,而我们并不会将整个包提供给用户,我们提供的只是放在bin目录下的class文件
如图:
Bin目录下并没有config.properties
所以我们需要修改config.properties的存放位置
此时我们再看bin目录
可以看到config.properties已经在bin目录下了
(java在编译时如果是java程序,就编译成class程序然后放到bin目录下,如果非java程序,直接就放到bin目录下)
现在继续修改源程序:
ReflectDemo类:
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Properties;
public class ReflectDemo {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Properties prop = new Properties();
// InputStream is=new FileInputStream(new File("config.properties"));
InputStream is = ReflectDemo.class.getClassLoader()
.getResourceAsStream("config.properties");
prop.load(is);
is.close();
String className = prop.getProperty("className");
Collection coll = (Collection) Class.forName(className).newInstance();
// Collection coll=new HashSet();
Point p1 = new Point(3, 3);
Point p2 = new Point(3, 5);
Point p3 = new Point(3, 3);
coll.add(p1);
coll.add(p2);
coll.add(p3);
coll.add(p1);
System.out.println(coll.size());
}
}
---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ---------------------