设为首页 加入收藏

TOP

Java反射机制实例(一)
2014-11-23 19:56:36 】 浏览:497
Tags:Java 反射 机制 实例

下面是Java常见的反射机制的实现。


/**
* 用于反射的类
*/


public class Role {

private String name = "默认的不带参数的构造函数";

private String type ="默认是public";

private static Role instance = null;


//不带参数的
public Role() {
}

//带参数的
public Role(String name) {
this.name = name;
}

//私有带参数的构造函数
private Role(String name,String type) {
this.name = name;
this.type = type;
}


private void setName(String name) {
this.name = name;
}


public String getName() {
return name;
}

@Override
public String toString() {
System.out.println(getName()+":"+getType());
return this.getName();
}

public synchronized static Role getInstance() {
if(instance == null) {
instance = new Role("带参数的","俺是private");
}
return instance;
}


public String getType() {
return type;
}


private void setType(String type) {
this.type = type;
}
}




import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class RefletTest {


/**
* @param args
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static void main(String[] args) throws ClassNotFoundException,
InstantiationException, IllegalAccessException {
// 反射例子1
System.out.println();
System.out.println("==========反射例子之1==========");
Class cls1 = Role.class;
// 只能反射默认的构造函数
Role role1 = cls1.newInstance();
role1.toString();


// 反射的例子2
System.out.println();
System.out.println("==========反射例子之2==========");
Class cls2 = (Class) Class.forName("nc.model.reflect.Role");
// 只能反射默认的构造函数
Role role2 = cls2.newInstance();
role2.toString();


// 反射的例子3 获取类的构造器
/**
* public Constructor< >[] getConstructors()
* 返回类中所有的public构造器集合,默认构造器的下标为0 public Constructor
* getConstructor(Class< >... parameterTypes) 返回指定public构造器,参数为构造器参数类型集合
* public Constructor< >[] getDeclaredConstructors() 返回类中所有的构造器,包括私有
* public Constructor getDeclaredConstructor(Class< >...
* parameterTypes) 返回任意指定的构造器
*/
System.out.println();
System.out.println("==========反射例子之3==========");
// 先看看public
try {
Constructor constructor1 = cls2
.getConstructor(new Class[] { String.class });
Role role3 = constructor1.newInstance("public型的带参数的");
role3.toString();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}


System.out.println();
System.out.println("==========反射例子之4==========");
Constructor constructor2;
Role role4 = null;
try {
constructor2 = cls2.getDeclaredConstructor(new Class[] { //只能使用getDecalredConstructor方法,而不能使用getConstructor
String.class, String.class });
constructor2.setAccessible(true);// 设置访问权限,私有时,必须设
role4 = constructor2.newInstance("带参数的", "private型的");
role4.toString();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇PHP常用函数集锦 下一篇Linux下用C实现Ping监测与HTTP报..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目