您的位置:首页 > 其它

getInterfaces(),getMethod(String name, Class... parameterTypes),getClass

2013-08-04 09:58 337 查看
返回一个
Method
对象,它反映此
Class
对象所表示的类或接口的指定公共成员方法。
name
参数是一个
String
,用于指定所需方法的简称。
parameterTypes
参数是按声明顺序标识该方法形式参数类型的
Class
对象的一个数组。如果
parameterTypes
null
,则按空数组处理。 

例:以项目Spring_Test为例

beans.xml

<beans>
<bean id = "u" class = "com.test.dao.impl.UserDAOImpl" />
<bean id = "userService" class="com.test.service.UserService" >
<property name="userDAO" bean="u"/>
</bean>
</beans>


UserService.java

import com.test.dao.UserDAO;
import com.test.model.User;

public class UserService {
private UserDAO userDAO;
public void add(User user) {
userDAO.save(user);
}
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
}


ClassPathXmlApplicationContext.java

package com.test.spring;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

public class ClassPathXmlApplicationContext implements BeanFactory {

private Map<String , Object> beans = new HashMap<String, Object>();
//IOC Inverse of Control DI Dependency Injection
public ClassPathXmlApplicationContext() throws Exception {
SAXBuilder sb=new SAXBuilder();

Document doc=sb.build(this.getClass().getClassLoader().getResourceAsStream("beans.xml")); //构造文档对象
Element root=doc.getRootElement(); //获取根元素HD
List list=root.getChildren("bean");//取名字为disk的所有元素
for(int i=0;i<list.size();i++){
Element element=(Element)list.get(i);
String id=element.getAttributeValue("id");
String clazz=element.getAttributeValue("class");
//forName()使用给定的类加载器,返回与带有给定字符串名的类或接口相关联的 Class 对象
//newInstance()用于创建此 Class 对象所表示的类的一个新实例
Object o = Class.forName(clazz).newInstance();//具有指定名的类的 Class 对象。
System.out.println(id);
System.out.println(clazz);
beans.put(id, o);

for(Element propertyElement : (List<Element>)element.getChildren("property")) {
String name = propertyElement.getAttributeValue("name"); //userDAO
String bean = propertyElement.getAttributeValue("bean"); //u
Object beanObject = beans.get(bean);//UserDAOImpl instance
// "set" + "U" + "serDAO"
String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
System.out.println("name.substring(0, 1)的值为: "+name.substring(0, 1));
System.out.println("name.substring(1)的值为: "+name.substring(1));
System.out.println("method name = " + methodName);
//getClass()返回一个对象的运行时类。
Method m = o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[0]);
//使用使用后一个参数在 前一个参数上指派该对象所表示方法的结果
m.invoke(o, beanObject);
}
}
}
public Object getBean(String id) {
return beans.get(id);
}
}


     //getClass()返回一个对象的运行时类。

     Method m = o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[0]);                                                                                      //使用后一个参数在 前一个参数上指派该对象所表示方法的结果 

     m.invoke(o, beanObject);

o为UserService实例的一个对象  

beanObject为UserDAOImpl类型的对象

Method getMethod(String name, Class<?>... parameterTypes)  

--返回一个 Method 对象,它反映此 Class 对象所表示的类或接口的指定公共成员方法。  

方法后面接收的就是Class类的对象,而如:String.class、int.class这些字节码才是Class类的对象
 

getInterfaces

public Class[] getInterfaces()

确定此对象所表示的类或接口实现的接口。
如果此对象表示一个类,则返回值是一个数组,它包含了表示该类所实现的所有接口的对象。数组中接口对象的顺序与此对象所表示的类的声明的
implements
子句中的接口名顺序一致。例如,给定声明:

class Shimmer implements FloorWax, DessertTopping { ... }


s
的值为
Shimmer
的一个实例;表达式:

s.getClass().getInterfaces()[0]


的值为表示
FloorWax
接口的
Class
对象;

s.getClass().getInterfaces()[1]


的值为表示
DessertTopping
接口的
Class
对象。
如果此对象表示一个接口,则该数组包含表示该接口扩展的所有接口的对象。数组中接口对象的顺序与此对象所表示的接口的声明的
extends
子句中的接口名顺序一致。

如果此对象表示一个不实现任何接口的类或接口,则此方法返回一个长度为 0 的数组。

如果此对象表示一个基本类型或 void,则此方法返回一个长度为 0 的数组。

返回:该类所实现的接口的一个数组。

按照我的理解简单来说,
//getClass()返回一个对象的运行时类。

 Method m = o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[0]);//使用使用后一个参数在 前一个参数上指派该对象所表示方法的结果 

m.invoke(o, beanObject);

就是调用UserService的setUserDAO方法!!!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: