黑马程序员Java 框架的简单例子

来源:互联网 发布:工控软件下载 编辑:程序博客网 时间:2024/05/01 11:29
  1. 框架是什么
    java框架就是一些类和接口的集合,通过这些类和接口协调来完成一系列的程序实现。JAVA框架可以分为三层:表示层,业务层和物理层。框架又叫做开发中的半成品,它不能提供整个WEB应用程序的所有东西,但是有了框架,我们就可以集中精力进行业务逻辑的开发而不用去关心它的技术实现以及一些辅助的业务逻辑。Structs和Spring就是表示层和业务层框架的强力代表。框架与工具有区别,工具类被用户的类调用,而框架则是调用用户提供的类。因为在写程序时无法知道要被调用的类名,所以程序无法直接new某个类的实例对象,而要用反射的方式来做。
  2. 没有用反射的例子
    定义要测试的类 ReflectPoint
public class ReflectPoint {    public int x;    public int y;    public String str1 = "ball";    public String str2 = "basketball";    public String str3 = "ittest";    public ReflectPoint(int x, int y) {        super();        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;    }    public String toString(){        return str1 + ":" + str2 + ":" + str3;    }}

定义主函数

import java.util.ArrayList;import java.util.Collection;import java.util.HashSet;/** * 先直接用new语句创建ArrayList和HashSet的实例对象,演示用eclipse自动生成ReflectPoint类的equals和hashcode * 方法,比较两个集合的运算结果差异。 * 然后改为配置文件加反射的方式创建ArrayList和HashSet的实例对象,比较观察运行结果差异。 * @author admin */public class ReflectTest2 {    public static void main(String[] args) {        // TODO Auto-generated method stub        Collection collections = new ArrayList();//面向接口编程        Collection collections1 = new HashSet();        ReflectPoint pt1 = new ReflectPoint(2, 3);        ReflectPoint pt2 = new ReflectPoint(3, 5);        ReflectPoint pt3 = new ReflectPoint(5, 3);        ReflectPoint pt4 = new ReflectPoint(2, 3);        collections.add(pt1);        collections.add(pt2);        collections.add(pt3);        collections.add(pt4);        collections.add(pt1);        collections1.add(pt1);        collections1.add(pt2);        collections1.add(pt3);        collections1.add(pt4);        collections1.add(pt1);//没有放进去        System.out.println(collections.size());//运行结果:5;在ReflectPoint添加上HashCode方法后,运行结果:5        System.out.println(collections1.size());//运行结果:3;在ReflectPoint添加上HashCode方法后,运行结果:3    }}/**ArrayList是有顺序的集合,相当于一个数组,当有一个对象放进来的时候,先找到第一个位置放进去 * 不是真的放进去,而是把对象的引用放进去;接着放第二个元素,放在第二个位置上;放置第三个元素, * 若是第三个元素与第一个元素一样,则第三个位置上放的与第一个位置上一样,是该元素的引用。有位置顺序 * HashSet:放置每一个对象,都会先在HashSet中寻找,是否有一样的,若是有一样的,则无法放进去 * HashCode作用 * */
  1. 用反射的例子
    定义config文件
className=java.util.ArrayList

主函数

import java.util.Collection;import java.util.HashSet;import java.util.Properties;import java.io.*;/** * 先直接用new语句创建ArrayList和HashSet的实例对象,演示用eclipse自动生成ReflectPoint类的equals和hashcode * 方法,比较两个集合的运算结果差异。 * 然后改为配置文件加反射的方式创建ArrayList和HashSet的实例对象,比较观察运行结果差异。 * 在运行的时候不用改JAVA源程序,只要改properties文件即可 * @author admin */public class ReflectTest2_2 {    public static void main(String[] args) throws Exception{        // TODO Auto-generated method stub        //用反射的方式就是在这里不出现类的名字,而从配置文件读取出来        //Collection collections1 = new HashSet();//        //1.加载properties文件,尽量面向父类或接口编程        InputStream ips = new FileInputStream("config.properties");        Properties props = new Properties();//相当于一个HashSet,里面装的是key-value,在HashSet基础上扩展一项功能,它可以 将文件写入硬盘,他也可以在初始化的时候,把自己的键值对加载进来        props.load(ips);        ips.close();//若不及时关闭,内存泄露,是ips对象关联的系统资源没有被释放.ips由java虚拟机管理,最后由垃圾回收机制处理         String className = props.getProperty("className");//前后保持一致        Collection collections = (Collection)Class.forName(className).newInstance();        Collection collections1 = new ArrayList();        //Collection collections = new HashSet();        ReflectPoint pt1 = new ReflectPoint(2, 3);        ReflectPoint pt2 = new ReflectPoint(3, 5);        ReflectPoint pt3 = new ReflectPoint(5, 3);        ReflectPoint pt4 = new ReflectPoint(2, 3);        collections.add(pt1);        collections.add(pt2);        collections.add(pt3);        collections.add(pt4);        collections.add(pt1);        collections1.add(pt1);        collections1.add(pt2);        collections1.add(pt3);        collections1.add(pt4);        collections1.add(pt1);//没有放进去        System.out.println(collections.size());        System.out.println(collections1.size());    }}/**运行结果 * className=java.util.HashSet  3 5 * className=java.util.ArrayList 5 5 * */
0 0
原创粉丝点击