展开全部
public class Test {
public Test() {
}
public static void main(String[] args) throws IllegalArgumentException,
SecurityException, IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
Bean bean = new Bean();
System.out.println(bean);
Bean.class.getMethod("setName", String.class).invoke(bean, "Jerry");
Bean.class.getMethod("setAge", int.class).invoke(bean, 25);
System.out.println("After reflection....\n" + bean);
}
}
class Bean {
private String name;
private int age;
public Bean() {
}
public Bean(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "NAME: " + this.getName() + "\nAGE: " + this.getAge();
}
}
public Test() {
}
public static void main(String[] args) throws IllegalArgumentException,
SecurityException, IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
Bean bean = new Bean();
System.out.println(bean);
Bean.class.getMethod("setName", String.class).invoke(bean, "Jerry");
Bean.class.getMethod("setAge", int.class).invoke(bean, 25);
System.out.println("After reflection....\n" + bean);
}
}
class Bean {
private String name;
private int age;
public Bean() {
}
public Bean(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "NAME: " + this.getName() + "\nAGE: " + this.getAge();
}
}
展开全部
try
{
//利用反射调用对应的方法
Class invokeClass = Class.forName( sClassName );
Object invokeObject = invokeClass.newInstance();
Class[] paraType = { Class.forName("javax.servlet.http.HttpServletRequest"),
Class.forName("javax.servlet.http.HttpServletResponse") };
Object[] paraObj = { request, response };
Method invokeMethod = invokeClass.getMethod( sMethodName, paraType );
//sNextPage 为利用反射调用对应的类返回的页面路径
String sNextPage = (String) invokeMethod.invoke( invokeObject, paraObj );
if ( sNextPage != null && !sNextPage.equals("") )
{
response.sendRedirect( "../" + sNextPage );
return;
}
}
catch(Exception e)
{
//页面处理出错,跳转到首页
mySession.invalidate();
response.sendRedirect("../index.htm");
return;
}
{
//利用反射调用对应的方法
Class invokeClass = Class.forName( sClassName );
Object invokeObject = invokeClass.newInstance();
Class[] paraType = { Class.forName("javax.servlet.http.HttpServletRequest"),
Class.forName("javax.servlet.http.HttpServletResponse") };
Object[] paraObj = { request, response };
Method invokeMethod = invokeClass.getMethod( sMethodName, paraType );
//sNextPage 为利用反射调用对应的类返回的页面路径
String sNextPage = (String) invokeMethod.invoke( invokeObject, paraObj );
if ( sNextPage != null && !sNextPage.equals("") )
{
response.sendRedirect( "../" + sNextPage );
return;
}
}
catch(Exception e)
{
//页面处理出错,跳转到首页
mySession.invalidate();
response.sendRedirect("../index.htm");
return;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package cn;
import java.lang.reflect.*;
public class SimpleClassDesc {
/**
* @param args
*/
public static void main(String[] args) {
Class type=null;
try
{
System.out.println(args[0]);
type=Class.forName(args[0]);
}catch(Exception e)
{
e.printStackTrace();
}
Class superClass=type.getSuperclass();
if(superClass!=null)
{
System.out.println("extends "+superClass.getCanonicalName());
}
Method[] methods=type.getDeclaredMethods();
for(int i=0;i<methods.length;i++)
{
if(Modifier.isPublic(methods[i].getModifiers()))
{
System.out.println(""+methods[i]);
}
}
}
}
利用java反射机制打印出给定类的直接超类,并列出它所声明的public方法。
import java.lang.reflect.*;
public class SimpleClassDesc {
/**
* @param args
*/
public static void main(String[] args) {
Class type=null;
try
{
System.out.println(args[0]);
type=Class.forName(args[0]);
}catch(Exception e)
{
e.printStackTrace();
}
Class superClass=type.getSuperclass();
if(superClass!=null)
{
System.out.println("extends "+superClass.getCanonicalName());
}
Method[] methods=type.getDeclaredMethods();
for(int i=0;i<methods.length;i++)
{
if(Modifier.isPublic(methods[i].getModifiers()))
{
System.out.println(""+methods[i]);
}
}
}
}
利用java反射机制打印出给定类的直接超类,并列出它所声明的public方法。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.lang.reflect.InvocationTargetException;
public class SimRef {
public SimRef() {
}
public static void main(String[] args) throws IllegalArgumentException,
SecurityException, IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
Bean bean = new Bean();
System.out.println(bean);
Bean.class.getMethod("setName", String.class).invoke(bean, "Jerry");
Bean.class.getMethod("setAge", int.class).invoke(bean, 25);
System.out.println("After reflection....\n" + bean);
}
}
class Bean {
private String name;
private int age;
public Bean() {
}
public Bean(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "NAME: " + this.getName() + "\nAGE: " + this.getAge();
}
}
public class SimRef {
public SimRef() {
}
public static void main(String[] args) throws IllegalArgumentException,
SecurityException, IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
Bean bean = new Bean();
System.out.println(bean);
Bean.class.getMethod("setName", String.class).invoke(bean, "Jerry");
Bean.class.getMethod("setAge", int.class).invoke(bean, 25);
System.out.println("After reflection....\n" + bean);
}
}
class Bean {
private String name;
private int age;
public Bean() {
}
public Bean(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "NAME: " + this.getName() + "\nAGE: " + this.getAge();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
// 最简单就是这样
package reflect;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Reflect {
public static void b() {
System.out.println("b is executing...");
}
public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
Method m = Reflect.class.getMethod("b", null);
m.invoke(null, null);
}
}
package reflect;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Reflect {
public static void b() {
System.out.println("b is executing...");
}
public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
Method m = Reflect.class.getMethod("b", null);
m.invoke(null, null);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询