评论

收藏

[Java] 实例讲解Java基础之反射

编程语言 编程语言 发布于:2021-09-18 15:39 | 阅读数:280 | 评论:0

今天小编就为大家分享一篇关于实例讲解Java基础之反射,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
前期准备
编写一个真实类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));
  }
}
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对CodeAE代码之家的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/qq_34191426/article/details/88141986

关注下面的标签,发现更多相似文章