亚洲精品中文字幕无乱码_久久亚洲精品无码AV大片_最新国产免费Av网址_国产精品3级片

SUN認(rèn)證 百分網(wǎng)手機(jī)站

SUN認(rèn)證考試知識(shí)點(diǎn):Java反射機(jī)制的應(yīng)用

時(shí)間:2017-11-05 14:30:21 SUN認(rèn)證 我要投稿

SUN認(rèn)證考試知識(shí)點(diǎn):Java反射機(jī)制的應(yīng)用

  目標(biāo):用一個(gè)代理類實(shí)現(xiàn)兩個(gè)相似類的調(diào)用

  1.兩個(gè)相似類

  Man類

  Women類:

  代理類:

  package Reflect;

  import java.lang.reflect.Method;

  public class Person { //代理類

  private Object target; //目標(biāo)對(duì)象

  private String methodName; //目標(biāo)方法

  private Object[] params; //參數(shù)數(shù)組

  private Method method;

  public Object getTarget() {

  return target;

  }

  public void setTarget(Object target) {

  this.target = target;

  }

  public String getMethodName() {

  return methodName;

  }

  public void setMethodName(String methodName) {

  this.methodName = methodName;

  }

  public Object[] getParams() {

  return params;

  }

  public void setParams(Object[] params) {

  this.params = params;

  }

  public Person(){

  }

  /**

  * _重新設(shè)置目標(biāo)方法

  */

  private void reSetMethod(String methodName){

  if(params!=null){ //目標(biāo)方法有參數(shù)

  int paramsLength = params.length;

  Class[] paramsTypes = new Class[paramsLength];

  for(int i=0;i

  paramsTypes[i] = params[i].getClass();

  }

  try {

  method = target.getClass().getMethod(methodName,paramsTypes);

  } catch (SecurityException e) {

  e.printStackTrace();

  } catch (NoSuchMethodException e) {

  e.printStackTrace();

  }

  }

  else //目標(biāo)方法參數(shù)為null

  {

  try {

  method = target.getClass().getMethod(methodName,null);

  } catch (SecurityException e) {

  e.printStackTrace();

  } catch (NoSuchMethodException e) {

  e.printStackTrace();

  }

  }

  }

  /**

  * _重新設(shè)置目標(biāo)對(duì)象和方法

  */

  private void reSetTarget(Object target,String methodName){

  this.target = target;

  this.reSetMethod(methodName);

  }

  /**

  * _動(dòng)態(tài)調(diào)用已綁定方法

  */

  public void doMethod() throws Exception{

  reSetTarget(target,methodName);

  this.method.invoke(target, params);

  }

  }

【SUN認(rèn)證考試知識(shí)點(diǎn):Java反射機(jī)制的應(yīng)用】相關(guān)文章:

1.Java反射機(jī)制常用API

2.sun java認(rèn)證考試介紹

3.Sun java認(rèn)證考試真題答案

4.Sun Java認(rèn)證考試科目

5.Sun java認(rèn)證考試答案

6.SUN認(rèn)證考試知識(shí)點(diǎn):java servlet生命周期

7.sun認(rèn)證考試知識(shí)點(diǎn):Java中基本數(shù)據(jù)類型

8.SUN認(rèn)證考試知識(shí)點(diǎn):java讀取文件內(nèi)容的函數(shù)