package reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Test {
publicstatic void main(String[] args) {
//创建反射第一种:
Class c1=new Person().getClass();
Constructor[] con =c1.getConstructors();
for(Constructorc:con){
System.out.println(c);
}
//创建反射第二种:
Class cla=Person.class;
Constructor[]cons=cla.getConstructors();
for(Constructorc:cons){
System.out.println(c);
}
try{
/*
* 第三种方式:
* Class:正在运行的 Java 应用程序中的类和接口
* forName(类或接口的路径):映射类或接口,即使在同一个包下,也需要定义包名.类名
*
*/
//创建反射第三种:
Classclas=Class.forName("reflect.Person");
//获取包名 //getName():此 Class 对象所表示的实体(类、接口、数组类、基本类型或 void)名称
System.out.println(clas.getPackage());
System.out.println(clas.getPackage().getName());
//获取有几个访问修饰符
System.out.println(clas.getModifiers());
//获取当前类的父类
System.out.println(clas.getSuperclass());
//获取接口名地址 [Ljava.lang.Class;@c3c749
System.out.println(clas.getInterfaces());
//获取接口名 interface reflect.Play
System.out.println("***********");
Class[] c=clas.getInterfaces();
for(Classc2:c){
System.out.println(c2);
}
//获取声明的全部属性
System.out.println("$$$$$$$$");
Field[] f=clas.getDeclaredFields();
for(Fieldfi:f){
System.out.println(fi);
}
//获取声明的全部方法
System.out.println("#########");
Method[]me=clas.getDeclaredMethods();
for(Methodm:me){
System.out.println(m);
}
//构造器 然后实例化并考虑向下转型
System.out.println("***********");
Constructorconstr=clas.getConstructor(String.class,int.class,String.class);
Personperson=(Person) constr.newInstance("tara",48,"london");
System.out.println(person);
//构造器然后实例化并考虑向下转型
//通过反射获取有参构造方法
System.out.println("#########");
Constructor constr1=clas.getConstructor(String.class,int.class,String.class);
Person person1=(Person)constr1.newInstance("tara",48,"london");
Field fie=clas.getDeclaredField("name");//获取指定属性
fie.setAccessible(true); //访问修饰符的开关
fie.set(person1,"xiaobai");//set方法
System.out.println(person1);
//通过反射获取无参构造方法
System.out.println("***********");
Person per=(Person)clas.newInstance();//向下转型
per.setName("mark"); //set方法
per.setAge(21); //set方法
per.setAddress("bj");//set方法
System.out.println(per);
//获取有参声明的方法 invoke调用实例化的对象
System.out.println("#########");
Method meth=clas.getDeclaredMethod("str", String.class);
System.out.println(meth.invoke(per,"hello"));
String s=(String)meth.invoke(per,"in.args");
System.out.println(s);
//获取无参声明的方法 invoke调用实例化的对象
Method met233 = clas.getDeclaredMethod("basketball");
met233.invoke(per233);
案例一:
Book:
package Book;
public class Book {
private String name;
private double price;
private String publish;
public Book(String name, double price, String publish) {
this.name = name;
this.price = price;
this.publish = publish;
}
public Book() {
}
public void show(){
System.out.println("书名:"+this.name+",价格:"+this.price+",出版社:"+this.publish);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getPublish() {
return publish;
}
public void setPublish(String publish) {
this.publish = publish;
}
@Override
public String toString() {
return "Book [name=" + name + ", price=" + price + ", publish="
+ publish + "]";
}
}
test:
package Book;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Properties;
import org.junit.Test;
import reflect.Person;
public class Jtest {
@Test
public void test(){
/*try {
File f=new File("C:/Users/Administrator/Workspaces/MyEclipse 8.5/Reflect/bookProperties.txt");
f.createNewFile();
FileWriter fw=new FileWriter("C:/Users/Administrator/Workspaces/MyEclipse 8.5/Reflect/bookProperties.txt");
BufferedWriter bw=new BufferedWriter(fw,1024);
bw.write("name=java");
bw.newLine();
bw.write("price=50");
bw.newLine();
bw.write("publish=清华大学出版社");
bw.close();
fw.close();
FileReader fr=new FileReader("C:/Users/Administrator/Workspaces/MyEclipse 8.5/Reflect/bookProperties.txt");
BufferedReader br=new BufferedReader(fr,1024);
String str;
while((str=br.readLine())!=null){//读取文件,按行读取
System.out.println(str);
}
//System.out.println(br);
br.close();
fr.close();
Class clas=Class.forName("Book.Book");
//无参构造
Book bk=(Book) clas.newInstance();//向下转型
bk.setName("java"); //set方法
bk.setPrice(50); //set方法
bk.setPublish("清华大学出版社");//set方法
System.out.println(bk);
//有参构造
Constructor constr233 = clas.getConstructor(String.class,double.class,String.class);
Book book233 = (Book)constr233.newInstance("java",50,"清华大学出版社"); //向下转型
//获取声明的属性
Field fie233 = clas.getDeclaredField("name");
fie233.setAccessible(true);//访问修饰符的开关
fie233.set(book233, "javaweb");
System.out.println(book233);
//获取无参声明的方法 invoke调用实例化的对象
Method met233 = clas.getDeclaredMethod("show");
met233.invoke(book233);
} catch (Exception e) {
System.out.println("打印错误");
e.printStackTrace();
}*/
//Properties:持久属性集 key=value
Properties p=new Properties();
try {
//读取配置文件book.properties;
p.load(new FileReader("d:/demo.properties"));
String name=p.getProperty("name");
String price=p.getProperty("price");
double i=Double.parseDouble(price);
String publish=p.getProperty("publish");
System.out.println(name);
System.out.println(i);
System.out.println(publish);
Class clas=Class.forName("Book.Book");
Constructor constr=clas.getConstructor(String.class,double.class,String.class);
Book book=(Book) constr.newInstance(name,i,publish);
System.out.println(book);
Method met233 = clas.getDeclaredMethod("show");
met233.invoke(book);
} catch (Exception e) {
e.printStackTrace();
}
}
}
案例二:
PrivateTest:
package PRtest;
public class PrivateTest {
private String name="hellokitty";
public PrivateTest(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "姓名:"+this.name;
}
}
test:
package PRtest;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
public class ReflectTest {
public static void main(String[] args) {
try {
Class clas=Class.forName("PRtest.PrivateTest");
Constructor constr233 = clas.getConstructor(String.class);
PrivateTest pr233 = (PrivateTest)constr233.newInstance("hellokitty"); //向下转型
Field fie=clas.getDeclaredField("name");
fie.setAccessible(true);
fie.set(pr233,"jerry and tom");
// System.out.println(pr233);
String str=pr233.getName();
System.out.println(str);
} catch (Exception e) {
System.out.println("error");
e.printStackTrace();
}
}
}
案例三:
Play:
package reflect;
public interface Play {
public abstract void basketball();
}
Person:
package reflect;
public class Person implements Play{
private String name;
private int age;
private String address;
public Person(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public Person() {
super();
}
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
//返回该对象的字符串内容
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", address=" + address + "]";
}
public void study(){
System.out.println("学生学习");
}
public String str(String s){
return s;
}
@Override
public void basketball(){
System.out.println("篮球");
}
}
test:
package reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) {
//创建反射第一种:
Class c1=new Person().getClass();
Constructor[] con = c1.getConstructors();
for(Constructor c:con){
System.out.println(c);
}
//创建反射第二种:
Class cla=Person.class;
Constructor[] cons=cla.getConstructors();
for(Constructor c:cons){
System.out.println(c);
}
try {
/*
* 第三种方式:
* Class:正在运行的 Java 应用程序中的类和接口
* forName(类或接口的路径):映射类或接口,即使在同一个包下,也需要定义包名.类名
*
*/
//创建反射第三种:
Class clas=Class.forName("reflect.Person");
//获取包名 //getName():此 Class 对象所表示的实体(类、接口、数组类、基本类型或 void)名称
System.out.println(clas.getPackage());
System.out.println(clas.getPackage().getName());
//获取有几个访问修饰符
System.out.println(clas.getModifiers());
//获取当前类的父类
System.out.println(clas.getSuperclass());
//获取接口名地址 [Ljava.lang.Class;@c3c749
System.out.println(clas.getInterfaces());
//获取接口名 interface reflect.Play
System.out.println("***********");
Class[] c=clas.getInterfaces();
for(Class c2:c){
System.out.println(c2);
}
//获取声明的全部属性
System.out.println("$$$$$$$$");
Field[] f=clas.getDeclaredFields();
for(Field fi:f){
System.out.println(fi);
}
//获取声明的全部方法
System.out.println("#########");
Method[] me=clas.getDeclaredMethods();
for(Method m:me){
System.out.println(m);
}
//构造器 然后实例化并考虑向下转型
System.out.println("***********");
Constructor constr=clas.getConstructor(String.class,int.class,String.class);
Person person=(Person) constr.newInstance("tara",48,"london");
System.out.println(person);
//通过反射获取有参构造方法
System.out.println("#########");
Constructor constr1=clas.getConstructor(String.class,int.class,String.class);
Person person1=(Person) constr1.newInstance("tara",48,"london");
Field fie=clas.getDeclaredField("name");//获取指定属性
fie.setAccessible(true); //访问修饰符的开关
fie.set(person1, "xiaobai");//set方法
System.out.println(person1);
//通过反射获取无参构造方法
System.out.println("***********");
Person per=(Person) clas.newInstance();//向下转型
per.setName("mark"); //set方法
per.setAge(21); //set方法
per.setAddress("bj");//set方法
System.out.println(per);
//获取有参声明的方法 invoke调用实例化的对象
System.out.println("#########");
Method meth=clas.getDeclaredMethod("str", String.class);
System.out.println(meth.invoke(per, "hello"));
String s=(String) meth.invoke(per, "in.args");
System.out.println(s);
/**
* teacher:
*/
//getConstructor(参数类型为可变型参数:参数类型或参数个数是随着构造方法的参数类型和个数进行定义):获取映射中的构造方法
/*获取有参构造方法:
* 1.先获取参数类型和参数个数
* 2.通过newInstrance("有参")将对象属性赋值
*
* 反射中newInstance代表的是创建对象
*
*/
Constructor constr233 = clas.getConstructor(String.class,int.class,String.class);
Person person233 = (Person)constr233.newInstance("张三",22,"北京"); //向下转型
//获取声明的属性
Field fie233 = clas.getDeclaredField("age");
//访问修饰符的开关,默认为false 如果需要通过反射直接修改属性,那么需要将权限开关设置为true
fie233.setAccessible(true);//访问修饰符的开关
fie233.set(person233, 27);//set方法
System.out.println(person233);
//通过反射获取无参构造方法
Person per233 = (Person)clas.newInstance();//向下转型
per233.setName("李四");
per233.setAge(21);
per233.setAddress("上海");
System.out.println(per233);
//获取有参声明的方法 invoke调用实例化的对象
Method meth233 = clas.getDeclaredMethod("str", String.class);
System.out.println(meth233.invoke(per233, "你好"));
String s233 = (String)meth233.invoke(per233, "你好");
System.out.println(s233);
//获取无参声明的方法 invoke调用实例化的对象
Method met233 = clas.getDeclaredMethod("basketball");
met233.invoke(per233);
} catch (Exception e) {
e.printStackTrace();
}
}
//创建反射第一种:
// Class cl = new Person().getClass();
// Constructor[] con = cl.getConstructors();
for(Constructor c:con){
System.out.println(c);
}
//第二种方式:
// Class cla = Person.class;
// Constructor[] cons = cla.getConstructors();
// for(Constructor c:cons){
// System.out.println(c);
// }
}
案例四:
Student:
package Student;
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
System.out.println(toString());
}
public Student() {
super();
System.out.println(toString());
}
@Override
public String toString() {
return "姓名:"+this.name+",年龄:"+this.age;
}
}
Test:
package Student;
import java.lang.reflect.Constructor;
public class NewInstanceTest {
public static void main(String[] args) {
try {
Class clas=Class.forName("Student.Student");
Constructor constr=clas.getConstructor(String.class,int.class);
Student stu=(Student) constr.newInstance("anli",15);
System.out.println(stu.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Student :
package Study;
public class Student {
private String name;
private int age;
private String address;
public Student(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public Student() {
super();
}
public void study(String name, int age, String address){
System.out.println("姓名:"+this.name+",年龄:"+this.age+",地址:"+this.address);
}
}
test:
package Study;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
public class jTest {
public static void main(String[] args) {
try {
File f=new File("C:/Users/Administrator/Workspaces/MyEclipse 8.5/Reflect/stu.txt");
f.createNewFile();
FileWriter fw=new FileWriter("C:/Users/Administrator/Workspaces/MyEclipse 8.5/Reflect/stu.txt");
BufferedWriter bw=new BufferedWriter(fw,1024);
bw.write("学生学习");
bw.close();
fw.close();
FileReader fr=new FileReader("C:/Users/Administrator/Workspaces/MyEclipse 8.5/Reflect/stu.txt");
BufferedReader br=new BufferedReader(fr,1024);
String str;
while((str=br.readLine())!=null){//读取文件,按行读取
System.out.println(str);
}
br.close();
fr.close();
Class clas=Class.forName("Study.Student");
Constructor constr233 = clas.getConstructor(String.class,int.class,String.class);
Student stu=(Student) constr233.newInstance("zs",32,"London"); //向下转型
//获取有参声明的方法 invoke调用实例化的对象
Method met233 = clas.getDeclaredMethod("study",String.class,int.class,String.class);
System.out.println(met233.invoke(stu,"zs",32,"London"));
} catch (Exception e) {
System.out.println("exception!!!!!!");
e.printStackTrace();
}
}
}