黑马程序员_HashCode_框架_配置文件

 ------- android培训java培训、期待与您交流! ----------

ArrayList_HashSet的比较:

对于ArrayList是一种有顺序的集合,就相当于一种数组,当你要放一个对象进来时,它首先找到第一个空位置将对象的引用放进去,当你要放第二个对象的时候,ArrayList集合按照顺序将对象放进去。
对于HashSet是一种有顺序的集合,就相当于一种数组,当你要放第二个对象的时候,HashSet集合首先比较集合中是否有相同的对象,有就不放,否则放进去
HashCode的分析:

当一个对象被存放进HashSet集合中以后,就不能修改这个对象中的那些参与计算哈希值的字段了,否则,对象修改后的哈希值与最初存进HashSet集合中的哈希值就不一样了,这样就造成了内存泄漏,无法在找到这个修改后的值。

代码:

package cn.itcast;

public class ReflectPoint {

	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + x;
		result = prime * result + y;
		return result;
	}

	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;
	}

	private int x;
	public int y;

	public ReflectPoint(int x, int y) {
		super();
		this.x = x;
		this.y = y;
	}
}

package cn.itcast;

import java.util.*;

public class ReflectTest2 {

	public static void main(String[] args) throws Exception {
		
//		Collection collections = new ArrayList();
		Collection collections = new HashSet();
		ReflectPoint pt1 = new ReflectPoint(3, 3);
		ReflectPoint pt2 = new ReflectPoint(5, 5);
		ReflectPoint pt3 = new ReflectPoint(3, 3);
		collections.add(pt1);
		collections.add(pt2);
		collections.add(pt3);
		collections.add(pt1);

		pt1.y = 7;
		collections.remove(pt1);
		System.out.println(collections.size());
	}

}



反射的作用:实现框架功能。

1. 框架与框架要解决的核心问题
        例如:我做房子卖给用户住,由用户自己安装门窗和空调,我做的房子就是框架,用户需要使用我的框架,把门窗插入我提供的框架中。框架与工具类有区别,工具类被用户的类调用,而框架则是调用用户提供的类。
 2. 框架要解决的核心问题
        我在写框架(房子)时,你这个用户可能还在上小学,还不会写程序呢?我写的框架程序怎样能调用你以后写的类(门窗)呢?
        因为在写程序时无法知道要被调用的类名,所以,在程序中无法直接new某个类的实例对象了,而要用反射的方式来做。

先创建一个配置文件:

config.properties中存放以下内容:

className = java.util.ArrayList
#className = java.util.HashSet

代码:

package cn.itcast;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collection;
import java.util.Properties;

public class ReflectTest2 {

	public static void main(String[] args) throws Exception {
		
		InputStream is = new FileInputStream("config.properties");
		Properties props = new Properties();
		props.load(is);
		is.close();
		String className = props.getProperty("className");
		Collection collections = (Collection) Class.forName(className).newInstance();

//		Collection collections = new ArrayList();
		Collection collections = new HashSet();
		ReflectPoint pt1 = new ReflectPoint(3, 3);
		ReflectPoint pt2 = new ReflectPoint(5, 5);
		ReflectPoint pt3 = new ReflectPoint(3, 3);
		collections.add(pt1);
		collections.add(pt2);
		collections.add(pt3);
		collections.add(pt1);

		pt1.y = 7;
		collections.remove(pt1);
		System.out.println(collections.size());
	}

}

在程序运行时,没有JAVAC的时候可以创建一个TXT文档来进行程序修改,所以我们需要创建一个配置文件

我们可以通过以下方法来找到配置文件:

1.通过GetRealPath();方法得到文件的绝对路径

  2.通过相对路径来找到配置文件

3.通过类加载器加载文件

//		通过类找到类加载器,用类加载器加载文件
		InputStream is = ReflectTest2.class.getClassLoader().getResourceAsStream("cn/itcast/config.properties");
		InputStream is = ReflectTest2.class.getResourceAsStream("config.properties");

一定要记住用完整的路径,但完整的路径不是硬编码得到,而是运算出来的



 ------- android培训java培训、期待与您交流! ----------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值