码迷,mamicode.com
首页 > 编程语言 > 详细

java反射技术实例

时间:2014-07-03 00:23:03      阅读:348      评论:0      收藏:0      [点我收藏+]

标签:style   http   java   color   数据   os   

 

java反射技术实例
?1. [代码][Java]代码     
package com.gufengxiachen.java.reflectiontest;


public class Person {
private String name;
private int age;
private static int total;
public Person(){
super();
total++;
}
public Person(String name,int age){
super();
this.name=name;
this.age=age;
total++;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public static int getTotal() {
return total;
}
public static void setTotal(int total) {
Person.total = total;
}jQuery特效

@Override
public String toString() {
// TODO 自动生成方法存根
return "Name:"+name+"\tAge:"+age;
}



}
2. [代码][Java]代码     
package com.gufengxiachen.java.reflectiontest;


import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Scanner;


public class TestReflection01 {
public static void main(String[] args) {
System.out.println("请输入类型名称:");
Scanner scan=new Scanner(System.in);
String className=scan.next();
System.out.println("当前类型:"+className);
try {
Class c=Class.forName(className);


Field[] fs=c.getDeclaredFields();
for(Field f:fs){
System.out.println("--------------------------");
System.out.println("属性:"+f.toString());
System.out.println("\t数据类型:"+f.getType());
System.out.println("\t属性名:"+f.getName());
int mod=f.getModifiers();
System.out.println("\t属性修饰符:"+Modifier.toString(mod)+Modifier.isPrivate(mod));

}

Method[] ms=c.getDeclaredMethods();
for(Method m:ms){
System.out.println("...........................................");
System.out.println("方法:"+m.toString());
System.out.println("\t方法名:"+m.getName());
int mod=m.getModifiers();
System.out.println("\t方法修饰符:"+Modifier.toString(mod));
System.out.println("\t方法参数列表:");
Class pts[]=m.getParameterTypes();
for(int i=0;i<pts.length;i++){
Class class1=pts[i];
if(i!=0)
System.out.println(",");
System.out.println(class1);
}
System.out.println();
System.out.println("\t返回值类型:"+m.getReturnType()); 
}http://www.huiyi8.com/jiaoben/?

Constructor[] cs=c.getDeclaredConstructors();
for(Constructor con:cs){
System.out.println("******************************************");
System.out.println("构造方法:"+con.toString());
System.out.println("方法名:"+con.getName());
int mod=con.getModifiers();
System.out.println("\t方法修饰符:"+Modifier.toString(mod));
System.out.print("\t方法参数列表:");
Class pts[]=con.getParameterTypes();
for(int i=0;i<pts.length;i++){
Class class1=pts[i];
if(i!=0)
System.out.print(",");
System.out.println(class1);
}
System.out.println();
}
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");

Class superClass=c.getSuperclass();
System.out.println("当前类的父类:"+superClass.toString());
System.out.println("##########################################");

Class[] interfaces=c.getInterfaces();
System.out.print("当前类所实现接口:");
for(Class class1:interfaces){
System.out.println(class1.toString()+"\t");
}
System.out.println("\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");

Package p=c.getPackage();
System.out.println("当前类所在包:"+p.toString());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}


}
3. [代码][Java]代码     跳至 [1] [2] [3] [全屏预览]
package com.gufengxiachen.java.reflectiontest;


public class User {
public String name;
public int age;
public static int total=100;
public User() {
// TODO 自动生成构造函数存根
super();
}
public User(String name,int age){
super();
this.name=name;
this.age=age;
}
public void setAll(String name,int age){
this.name=name;
this.age=age;
}
public static void showTotal(){
System.out.println("int static method showTotal,total= "+total);
}
@Override
public String toString() {
// TODO 自动生成方法存根
return "用户名:"+name+"\t密码:"+age;
}
}
?

java反射技术实例,布布扣,bubuko.com

java反射技术实例

标签:style   http   java   color   数据   os   

原文地址:http://www.cnblogs.com/xkzy/p/3818638.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!