反射的基本使用实例
2014-12-22 17:22
204 查看
package com.franky.reflex; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * @原理:反射就是通过取得指定类名的Class类,来获取类中的成员方法和成员变量 * @实例:反射的基本使用实例 * @author Administrator * */ public class ReflexDemo { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { getObj(); getMethod1(); getMethod2(); getMethod3(); } /** * 获取非静态有参方法 * @throws Exception */ private static void getMethod3() throws Exception { String name = "com.franky.domain.Student"; Class clazz = Class.forName(name); Method method = clazz.getMethod("show", String.class,int.class); Object obj = clazz.newInstance(); method.invoke(obj , "jim",33); } /** * 获取静态无参方法 * @throws Exception */ private static void getMethod2() throws Exception { String name = "com.franky.domain.Student"; Class clazz = Class.forName(name); Method method = clazz.getMethod("staticShow", null); method.invoke(null, null); } /** * 获取非静态无参方法 * @throws Exception */ private static void getMethod1() throws Exception { String name = "com.franky.domain.Student"; Class clazz = Class.forName(name); Method method = clazz.getMethod("show", null); Object obj = clazz.newInstance(); method.invoke(obj , null); } /** * 获取对象 和 字段 * @throws Exception */ private static void getObj() throws Exception { //定义类名 String className = "com.franky.domain.Student"; //得到类的Class对象 Class clazz = Class.forName(className); System.out.println(clazz); //实例化无参构造方法 Object obj = clazz.newInstance(); // Student s = (Student)obj; //实例化有参构造方法 Constructor cons = clazz.getConstructor(String.class,double.class,double.class,double.class); Object obj1= cons.newInstance("tom",23.5,34.6,23.2); //得到私有的字段 Field filed = clazz.getDeclaredField("sum"); //取消权限检查 filed.setAccessible(true); //设定字段值 filed.set(obj1, 55.5); //得到字段值 System.out.println(filed.get(obj1)); } }
</pre><p>使用的Student类</p><p></p><pre name="code" class="java">package com.franky.domain; public class Student implements Comparable<Student>{ private String name; private double english; private double history; private double math; private double sum; @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); long temp; temp = Double.doubleToLongBits(sum); result = prime * result + (int) (temp ^ (temp >>> 32)); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (Double.doubleToLongBits(sum) != Double.doubleToLongBits(other.sum)) return false; return true; } public Student(String name, double english, double history, double math) { super(); this.name = name; this.english = english; this.history = history; this.math = math; this.sum = english+history+math; } public Student(){ super(); System.out.println("this is constructer"); } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getEnglish() { return english; } public void setEnglish(double english) { this.english = english; } public double getHistory() { return history; } @Override public String toString() { return "Student [name=" + name + ", sum=" + sum + "]"; } public void setHistory(double history) { this.history = history; } public double getMath() { return math; } public void setMath(double math) { this.math = math; } public void setSum(double sum) { this.sum = sum; } public double getSum(){ return sum; } @Override public int compareTo(Student o) { int temp = (int) (this.sum - o.sum); return temp==0?this.name.compareTo(o.name):temp; } public void show(){ System.out.println("show is running!"); } public static void staticShow(){ System.out.println("staticshow is running!"); } public void show(String name,int age){ System.out.println(name+":"+age); } }
相关文章推荐
- 扩展方法的几个实例,扩展基本类型、接口、通过反射让扩展方法使用私有成员等
- 反射的基本使用实例
- Sniffer Pro的基本使用和实例
- 使用反射技术动态创建类对象(实例代码)
- 引用】C#反射实例(一) 利用反射使用类库
- 960 Grid System 基本使用实例
- [c#美味] 使用反射动态创建实例并调用方法
- 使用反射动态创建类的实例
- 反射-实例化一个未知的类(使用System.AppDomain类)
- 使用反射机制用一个类实例的值为DataTable添加一行
- 使用反射机制将一个类实例的值赋给DataTable某一行
- 960 Grid System 基本使用实例
- 反射使用的简单实例
- [转载]使用反射技术动态创建类对象(实例代码)
- Sniffer Pro的基本使用和实例
- 没有为实例"db2inst1"指定"快速连接管理器"(FCM)基本端口.将使用缺省参数
- 使用基本的ASP.NET服务器端控件的综合实例
- Sniffer Pro的基本使用和实例[图文]
- 使用反射机制将一个类实例的值赋给DataTable某一行
- c# 反射使用实例