• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

Java_反射

error 来源:top0qidian 9次浏览
package com.hbsi.reflect;

public class Demo1 {

 public static void main(String[] args) throws ClassNotFoundException{
  
  //1.forName()
  Class clazz=Class.forName("com.hbsi.reflect.Person");
  System.out.println(clazz.getName());
  
  //2.
  Class clazz1=Person.class;
  System.out.println(clazz1.getName());
  
  //3
  Person p=new Person();
  Class clazz2=p.getClass();
  System.out.println(clazz2.getName());
  
  Class clazz3=Integer.class;
  System.out.println(clazz3.getName());
  
  
 }
 
}
package com.hbsi.reflect;

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

import org.junit.Test;

public class Demo2 {
 
 
 
 //反射出无参的构造方法,并创建一个person对象
 @Test
 public void test1() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException{
  
  //加载类
  Class clazz=Class.forName("com.hbsi.reflect.Person");
  
  //反射出构造方法
  Constructor c=clazz.getConstructor(null);
  
  Person p=(Person)c.newInstance(null);
  
  System.out.println(p.getName());
  
 }
 //反射出 public Person(String name)
 @Test
 public void test2() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException{
  
  //加载类
  Class clazz=Class.forName("com.hbsi.reflect.Person");
  
  //反射出构造方法
  Constructor c=clazz.getConstructor(String.class);
  
  Person p=(Person)c.newInstance("lisi");
  
  System.out.println(p.getName());
  
 }
 
 //反射出public Person(String name,int age)
 @Test
 public void test3() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException{
  
  //加载类
  Class clazz=Class.forName("com.hbsi.reflect.Person");
  
  //反射出构造方法
  Constructor c=clazz.getConstructor(String.class,int.class);
  
  Person p=(Person)c.newInstance("lisi",23);
  
  System.out.println(p.getName());
  
 }
 
 //反射private Person(int age)
 @Test
 public void test4() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException{
  
  //加载类
  Class clazz=Class.forName("com.hbsi.reflect.Person");
  
  //反射出构造方法
  Constructor c=clazz.getDeclaredConstructor(int.class);
  
  c.setAccessible(true); //如果是private的,必须设置访问权限
  
  Person p=(Person)c.newInstance(24);
  
  System.out.println(p.getName());
  
 }
 
 
 @Test
 public void test5() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
  
  //加载类
  Class clazz=Class.forName("com.hbsi.reflect.Person");
  
  Person p=(Person)clazz.newInstance();
  
  System.out.println(p.getName());
  
 }

}
package com.hbsi.reflect;

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

import org.junit.Test;

public class Demo3 {
 
 // public void run()
 @Test
 public void test1() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
  
  Person p=new Person();
  
  Class clazz=Class.forName("com.hbsi.reflect.Person");
  
  Method m=clazz.getMethod("run",null);
  
  m.invoke(p,null);
  
  
  
 }
 
 //public void run(String name)
 @Test
 public void test2() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
  
  Person p=new Person();
  
  Class clazz=Class.forName("com.hbsi.reflect.Person");
  
  Method m=clazz.getMethod("run",String.class);
  
  m.invoke(p,"zhangsan");
  
  
  
 }
 
 //public int sum(int i,int j)
 
 @Test
 public void test3() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
  
  Person p=new Person();
  
  Class clazz=Class.forName("com.hbsi.reflect.Person");
  
  Method m=clazz.getMethod("sum",int.class,int.class);
  
  int value=(Integer)m.invoke(p,12,2);
  
  System.out.println(value);
  
 }
 
 //private void  sum1()
 @Test
 public void test4() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
  
  Person p=new Person();
  
  Class clazz=Class.forName("com.hbsi.reflect.Person");
  
  Method m=clazz.getDeclaredMethod("sum1",null);
  
  m.setAccessible(true);
  
  m.invoke(p,null);
  
 }
 
 //public static void sum2()
 
 @SuppressWarnings("unchecked")
 @Test
 public void test5() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
  
  //Person p=new Person();
  
  Class clazz=Class.forName("com.hbsi.reflect.Person");
  
  Method m=clazz.getMethod("sum2",null);
   
  m.invoke(null,null);
  
 }
 

}
package com.hbsi.reflect;

import java.lang.reflect.Field;

import org.junit.Test;

public class Demo4 {
 
 @Test
 public void test1() throws ClassNotFoundException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{
  
  Person p=new Person();
  
  Class clazz=Class.forName("com.hbsi.reflect.Person");
  
  Field f=clazz.getField("sex");
  
  String sex=(String)f.get(p);
  
  f.set(p, "nv");
  
  //Class type=f.getType();
  
  System.out.println(p.sex);
  //System.out.println(type);
  
  
  
  
 }

}
package com.hbsi.reflect;

public class Person {
 
 public String sex="nan";
 
 public String getSex() {
  return sex;
 }

 public void setSex(String sex) {
  this.sex = sex;
 }
 private String name="zhangsan";
 private int age;
 
 public Person(){
  //System.out.println("无参构造");
 }
 
 public Person(String name){
  //this.name=name;
  System.out.println(name);
 }
 
 public Person(String name,int age){
  System.out.println(name+"..."+age);
 }
 
 private Person(int age){
  System.out.println("私有");
 }
 
 
 
 public void run(){
  System.out.println("run...");
 }
 
 public void run(String name){
  System.out.println("run..."+name);
 }
 
 public void run(String name,int age){
  System.out.println("run..."+name+"..."+age);
 }
 
 public int sum(int i,int j){
  return i+j;
 }
 
 private void  sum1(){
  System.out.println("sum1...");
 }
 
 
 public static void sum2(){
  System.out.println("sum2...");
 }
 
 
 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;
 }
 

}


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)