前期准备
编写一个真实类phone,实现list接口
public class Phone implements List { public double price; public String name; public Phone() { } public Phone(double price, String name) { this.price = price; this.name = name; } public double getPrice() { return price; } public void gege(String h){ System.out.println("gege的"+h); } public void setPrice(double price) { this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Phone{" + "price=" + price + ", name='" + name + '/'' + '}'; } @Override public int size() { return 0; } @Override public boolean isEmpty() { return false; } @Override public boolean contains(Object o) { return false; } @Override public Iterator iterator() { return null; } @Override public Object[] toArray() { return new Object[0]; } @Override public boolean add(Object o) { return false; } @Override public boolean remove(Object o) { return false; } @Override public boolean addAll(Collection c) { return false; } @Override public boolean addAll(int index, Collection c) { return false; } @Override public void clear() { } @Override public Object get(int index) { return null; } @Override public Object set(int index, Object element) { return null; } @Override public void add(int index, Object element) { } @Override public Object remove(int index) { return null; } @Override public int indexOf(Object o) { return 0; } @Override public int lastIndexOf(Object o) { return 0; } @Override public ListIterator listIterator() { return null; } @Override public ListIterator listIterator(int index) { return null; } @Override public List subList(int fromIndex, int toIndex) { return null; } @Override public boolean retainAll(Collection c) { return false; } @Override public boolean removeAll(Collection c) { return false; } @Override public boolean containsAll(Collection c) { return false; } @Override public Object[] toArray(Object[] a) { return new Object[0]; }}
1.反射之4种new对象
public class Test2 { public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException { //第一种 Phone p = new Phone(2999,"小米"); System.out.println(p);//Phone{price=2999.0, name='小米'} //第二种 需要一个空参构造 Class<Phone> phoneClass = Phone.class; Phone phone = phoneClass.newInstance(); phone.setName("华为"); phone.setPrice(3499); System.out.println(phone);//Phone{price=3499.0, name='华为'} //第三种 Class<?> aclass = Class.forName("com.demo.bean.Phone"); Phone p2 = (Phone) aclass.newInstance(); p2.setPrice(2999); p2.setName("魅族"); System.out.println(p2);//Phone{price=2999.0, name='魅族'} //第四种,需要一个配置文件phone.properties String name = ResourceBundle.getBundle("phone").getString("myphone"); Class<?> bClass = Class.forName(name); Phone p3 = (Phone) bClass.newInstance(); p3.setPrice(3299); p3.setName("锤子"); System.out.println(p3);//Phone{price=3299.0, name='锤子'} }}
配置文件phone.properties
myphone=com.demo.bean.Phone
2. 反射之获取类、父类、实现接口
public class Test3 { public static void main(String[] args) throws ClassNotFoundException { String string = ResourceBundle.getBundle("phone").getString("myphone"); Class<?> aClass = Class.forName(string); //获取类的完整路径 System.out.println(aClass.getName());//com.demo.bean.Phone //获取类的简单名字 System.out.println(aClass.getSimpleName());//Phone //获取类的父类 Class<?> superclass = aClass.getSuperclass(); System.out.println(superclass.getName());//java.lang.Object System.out.println(superclass.getSimpleName());//Object //获得类的接口 Class<?>[] interfaces = aClass.getInterfaces(); for (Class<?> in:interfaces ) { System.out.println(in.getSimpleName()); } }}
3.反射之获取空参、有参构造
public class Test4 { public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException { String string = ResourceBundle.getBundle("phone").getString("myphone"); Class<?> aClass = Class.forName(string); //调用的是无参的构造方法 Phone p1 = (Phone) aClass.newInstance(); p1.setName("华为"); p1.setPrice(2999);//Phone{price=2999.0, name='华为'} System.out.println(p1); //获得无参的构造方法 Constructor<?> constructor = aClass.getConstructor(); System.out.println(constructor);//public com.demo.bean.Phone() //获得所有的构造方法 Constructor<?>[] constructors = aClass.getConstructors(); for (Constructor<?> c:constructors ) { System.out.println(c); } }}
4.反射之获取方法
public class Test5 { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException,InstantiationException,IllegalAccessException,InvocationTargetException{ String string = ResourceBundle.getBundle("phone").getString("myphone"); Class<?> aClass = Class.forName(string); //包含了父类的方法 Method[] methods = aClass.getMethods(); for (Method m:methods ) { System.out.println(m); } //本类中的方法,没有父类的方法 Method[] declaredMethods = aClass.getDeclaredMethods(); for (Method m:declaredMethods ) { System.out.println(m); } Method gege = aClass.getMethod("gege",String.class); //获取gege方法的权限修饰符 System.out.println(Modifier.toString(gege.getModifiers())); //获取gege方法的返回值类型 System.out.println(gege.getReturnType()); //设置gege的参数值 Object o = aClass.newInstance(); gege.invoke(o,"aa"); }}
5.反射之获取字段
public class Test6 { public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException { String string = ResourceBundle.getBundle("phone").getString("myphone"); Class<?> aClass = Class.forName(string); //只能调用public 字段,但是能得到父类的字段 Field[] fields = aClass.getFields(); for (Field f:fields ) { System.out.println(f.getName()); } //只能调用public 字段,只能得到本类中的字段 Field[] declaredFields = aClass.getDeclaredFields(); for (Field f:declaredFields ) { System.out.println(f.getName()); } //获取某一字段的数据类型 Field name = aClass.getField("name"); String simpleName = name.getType().getSimpleName(); System.out.println(simpleName); name.setAccessible(true); Object o = aClass.newInstance(); name.set(o,"华为"); System.out.println(name.get(o)); }}
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对VeVb武林网的支持。
注:相关教程知识阅读请移步到JAVA教程频道。