通过反射机制加载类驱动和创建类实现:
配置文件内容<文件名:properties.properties>:
person=entity.PersonInstanceTest
接口:
package iInterface;
public interface IPerson {
public void setAge(int age);
public void setName(String name);
public void setAddress(String address);
public int getAge();
public String getName();
public String getAddress();
public String sayHello(String name);
public String sayGoodBye(String name);
}
接口实现类:
package entity;
import iInterface.IPerson;
public class PersonInstanceTest implements IPerson{
private int age;
private String name;
private String address;
public PersonInstanceTest() {
System.out.println("--对象被实例化啦!--");
}
public PersonInstanceTest(int age,String name,String address){
this.age = age;
this.name = name;
this.address = address;
}
@Override
public int getAge() {
return 0;
}
@Override
public String getName() {
return null;
}
@Override
public String getAddress() {
return null;
}
@Override
public String sayHello(String name) {
// TODO Auto-generated method stub
return null;
}
@Override
public String sayGoodBye(String name) {
return null;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "age: " + age + " name: " + name + " address: " + address;
}
}
创建类实现工具类:
package reflect;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;
/***
* 面向接口编程:
* newInstance() 和 new 有什么区别?
* 1、区别在于创建对象的方式不一样,前者是使用类加载机制
* 2、从jvm的角度看,我们使用new的时候,这个要new的类可以没有加载;
* 3、使用newInstance时候,就必须保证:
* 1、这个类已经加载;
* 2、这个类已经连接了。
* 而完成上面两个步骤的正是class的静态方法forName()方法,这个静态方法调用了启动类加载器(就是加载javaAPI的那个加载器)。
* 即:newInstance实际上是把new这个方式分解为两步,即,首先调用class的加载方法加载某个类,然后实例化
*
* [补充:]
* newInstance: 弱类型。低效率。只能调用无参构造。
* new: 强类型。相对高效。能调用任何public构造。
* newInstance()是实现IOC、反射、面对接口编程 和 依赖倒置 等技术方法的必然选择,new 只能实现具体类的实例化,不适合于接口编程。
* @author huawei
*
*/
public class ClassReflect {
private static Properties props;
//采用静态语句块:类加载便会执行
static{
props = new Properties();
try {
String path = "src" + File.separator + "file" + File.separator + "properties.properties";
props.load(new FileInputStream(path));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//调用类无参构造器
public static Object createInstance(Object className)
throws ClassNotFoundException, InstantiationException,
IllegalAccessException, IllegalArgumentException,
SecurityException, InvocationTargetException,
NoSuchMethodException{
String object = (String) props.get(className);
Class> cls = Class.forName(object);
return cls.getConstructor().newInstance();
}
//调用有参构造器
public static Object createInstance(Object className,Class>[] parameterTypes,Object[] initargs) throws Exception{
String object = (String) props.get(className);
Class> cls = Class.forName(object);
return cls.getConstructor(parameterTypes).newInstance(initargs);
}
}
调用不同参数构造器测试类:
package reflect;
import org.junit.Test;
import iInterface.IPerson;
/***
* 创建类的实例测试类:--实现面向接口编程思想
* 1、调用无参构造器
* 2、调用有参构造器
* @author huawei
*
*/
public class ClassInstanceTest {
//调用无参构造器
//@Test
public void test1(){
try {
IPerson person = (IPerson) ClassReflect.createInstance("person");
person.setName("beyond0851");
person.setAge(100);
person.setAddress("hello");
System.out.println(person.toString());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
//根据参数类型不同,调用有参构造器
@Test
public void test2(){
try {
IPerson person = (IPerson) ClassReflect.createInstance("person",
new Class[]{int.class,String.class,String.class},
new Object[]{90,"beyond0851","love记记"});
System.out.println(person.toString());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}