当前位置: 首页 > news >正文

做动态图网站百度排行榜小说

做动态图网站,百度排行榜小说,凡科建站登录入口官方正版,全国性质的网站开发公司反射 作用: 对于任意一个对象,把对象所有的字段名和值,保存到文件中去利用反射动态的创造对象和运行方法 1. 获取字节码文件对象 方法描述Class.forName(String)通过类的全限定名字符串获取字节码文件对象。类字面量直接使用类的字面量获…

反射

作用

  • 对于任意一个对象,把对象所有的字段名和值,保存到文件中去
  • 利用反射动态的创造对象和运行方法
1. 获取字节码文件对象
方法描述
Class.forName(String)通过类的全限定名字符串获取字节码文件对象。
类字面量直接使用类的字面量获取字节码文件对象。
对象的方式当已经有类的对象时,通过对象的 getClass() 方法获取字节码文件对象。
public class ReflectDemo1 {public static void main(String[] args) throws ClassNotFoundException {// 最常用的方式Class clazz = Class.forName("Student");System.out.println(clazz);// 一般通过参数传递的方式Class clazz2 = Student.class;System.out.println(clazz2);System.out.println(clazz==clazz);// 通过对象的方式,有类的对象时才可以使用Student s = new Student();Class clazz3 = s.getClass();System.out.println(clazz3);System.out.println(clazz2==clazz3);}
}
2. 利用反射获得构造方法
方法描述
Class.forName(String)通过类的全限定名字符串获取字节码文件对象。
Class.getConstructors()获取公共的构造方法。
Class.getDeclaredConstructors()获取所有构造方法,包括私有的构造方法。
Class.getConstructor(Class...)获取指定参数类型的公共构造方法。
Class.getDeclaredConstructor(Class...)获取指定参数类型的构造方法,包括私有的构造方法。
Constructor.getModifiers()获取构造方法的修饰符。
Constructor.getParameters()获取构造方法的参数。
Constructor.newInstance(Object...)通过构造方法创建类的实例。
Constructor.setAccessible(true)设置私有构造方法可访问。
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Parameter;public class ReflectDemo2 {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {// 1. 获取Class字节码文件对象Class clazz = Class.forName("Student");// 2. 获取所有的构造方法Constructor[] cons = clazz.getConstructors();for (Constructor con: cons) {System.out.println(con);}System.out.println("------------");// 获取所有的构造方法,包括私有的Constructor[] cons2 = clazz.getDeclaredConstructors();for (Constructor con: cons2) {System.out.println(con);}System.out.println("------------");// 获取指定的构造方法Constructor con3 = clazz.getConstructor(String.class);System.out.println(con3);System.out.println("------------");Constructor con4 = clazz.getDeclaredConstructor(int.class);System.out.println(con4);System.out.println("------------");Constructor con5 = clazz.getDeclaredConstructor(String.class, int.class);System.out.println(con5);System.out.println("------------");// 获取构造方法的权限修饰符int modifiers = con5.getModifiers();System.out.println(modifiers);Parameter[] parameters = con5.getParameters();for (Parameter parameter: parameters) {System.out.println(parameter);}con5.setAccessible(true); // 设置私有构造方法可访问Student stu = (Student) con5.newInstance("张三", 23);System.out.println(stu);}
}
public class Student {private String name;private int age;public Student() {}public Student(String name) {this.name = name;}protected Student(int age) {this.age = age;}private Student(String name, int age) {this.name = name;this.age = age;}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;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}
}
3. 利用反射获取成员变量
方法描述
Class.getFields()获取公共的成员变量。
Class.getDeclaredFields()获取所有的成员变量,包括私有的成员变量。
Class.getField(String)获取指定名称的公共成员变量。
Class.getDeclaredField(String)获取指定名称的成员变量,包括私有的成员变量。
Field.getModifiers()获取成员变量的权限修饰符。
Field.get(Object)获取指定对象上此 Field 表示的字段的值。
Field.setAccessible(true)设置私有成员变量可访问。
Field.set(Object, Object)将指定对象参数上此 Field 表示的字段设置为指定的新值。
import java.lang.reflect.Field;public class ReflectDemo3 {public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {// 利用反射获取成员变量Class clazz = Class.forName("Student");// 获取所有的成员变量Field[] fields = clazz.getFields();for (Field field: fields) {System.out.println(field);}System.out.printf("------------\n");// 获取所有的成员变量,包括私有的Field[] fields2 = clazz.getDeclaredFields();for (Field field: fields2) {System.out.println(field);}System.out.printf("------------\n");// 获取指定的成员变量Field field3 = clazz.getField("gender");// 获取私有的成员变量Field name = clazz.getDeclaredField("name");System.out.println(field3);System.out.println(name);// 获取权限修饰符int modifiers = field3.getModifiers();int modifiers2 = name.getModifiers();System.out.println(modifiers);System.out.println(modifiers2);System.out.println("------------");// 获取成员变量的值Student s = new Student("张三", 23);name.setAccessible(true); // 设置私有成员变量可访问Object value = name.get(s);System.out.println(value);// 修改对象记录的值name.set(s, "李四");System.out.println(s);}
}
public class Student {private String name;private int age;public String gender;public Student() {}public Student(String name) {this.name = name;}protected Student(int age) {this.age = age;}public Student(String name, int age) {this.name = name;this.age = age;}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;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}
}
4. 获取成员方法
方法名说明
Method[] getMethods()返回所有成员方法对象的数组(只能拿public的)
Method[] getDeclaredMethods()返回所有成员方法对象的数组,存在就能拿到
Method getMethod(String name, Class<?>… parameterTypes)返回单个成员方法对象(只能拿public的)
Method getDeclaredMethod(String name, Class<?>… parameterTypes)返回单个成员方法对象,存在就能拿到
import java.lang.reflect.Method;public class ReflectMethodExample {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {// 获取Class对象Class<?> clazz = Class.forName("MyClass");// 获取所有公共成员方法Method[] methods = clazz.getMethods();System.out.println("公共成员方法:");for (Method method : methods) {System.out.println(method);}System.out.println("-------------");// 获取所有成员方法,包括私有的Method[] declaredMethods = clazz.getDeclaredMethods();System.out.println("所有成员方法:");for (Method method : declaredMethods) {System.out.println(method);}System.out.println("-------------");// 获取指定的公共成员方法Method publicMethod = clazz.getMethod("publicMethod");System.out.println("指定的公共成员方法:");System.out.println(publicMethod);System.out.println("-------------");// 获取指定的成员方法,包括私有的Method privateMethod = clazz.getDeclaredMethod("privateMethod");System.out.println("指定的成员方法:");System.out.println(privateMethod);}
}class MyClass {public void publicMethod() {System.out.println("This is a public method.");}private void privateMethod() {System.out.println("This is a private method.");}
}

文章转载自:
http://groomsman.pwmm.cn
http://cosmography.pwmm.cn
http://regenerator.pwmm.cn
http://umbriel.pwmm.cn
http://collectorship.pwmm.cn
http://keos.pwmm.cn
http://yesternight.pwmm.cn
http://mycotrophy.pwmm.cn
http://bah.pwmm.cn
http://thermophile.pwmm.cn
http://swive.pwmm.cn
http://frankincense.pwmm.cn
http://sexduction.pwmm.cn
http://laicism.pwmm.cn
http://supportability.pwmm.cn
http://paid.pwmm.cn
http://nasally.pwmm.cn
http://multiplier.pwmm.cn
http://lucretia.pwmm.cn
http://dinkel.pwmm.cn
http://thanatorium.pwmm.cn
http://autarky.pwmm.cn
http://eyepoint.pwmm.cn
http://opsonin.pwmm.cn
http://thyrotomy.pwmm.cn
http://whyever.pwmm.cn
http://morty.pwmm.cn
http://nonsolvent.pwmm.cn
http://castigator.pwmm.cn
http://nightfall.pwmm.cn
http://amateurship.pwmm.cn
http://fica.pwmm.cn
http://grisgris.pwmm.cn
http://spiggoty.pwmm.cn
http://cabinet.pwmm.cn
http://nigra.pwmm.cn
http://astrogeology.pwmm.cn
http://aboveboard.pwmm.cn
http://irriguous.pwmm.cn
http://violaceous.pwmm.cn
http://hail.pwmm.cn
http://coronograph.pwmm.cn
http://nutate.pwmm.cn
http://rostellum.pwmm.cn
http://deuteranomaly.pwmm.cn
http://africanize.pwmm.cn
http://opponent.pwmm.cn
http://haematocryal.pwmm.cn
http://discovert.pwmm.cn
http://obtained.pwmm.cn
http://unfailing.pwmm.cn
http://jonnop.pwmm.cn
http://earthlight.pwmm.cn
http://constellation.pwmm.cn
http://coma.pwmm.cn
http://guilder.pwmm.cn
http://varoom.pwmm.cn
http://forewoman.pwmm.cn
http://paroicous.pwmm.cn
http://weedkilling.pwmm.cn
http://ha.pwmm.cn
http://troppo.pwmm.cn
http://histomap.pwmm.cn
http://diabolatry.pwmm.cn
http://beldame.pwmm.cn
http://antianginal.pwmm.cn
http://renunciant.pwmm.cn
http://binge.pwmm.cn
http://hydromel.pwmm.cn
http://raffish.pwmm.cn
http://cqt.pwmm.cn
http://depone.pwmm.cn
http://frobnitz.pwmm.cn
http://peetweet.pwmm.cn
http://collusive.pwmm.cn
http://hierolatry.pwmm.cn
http://lemonade.pwmm.cn
http://schlamperei.pwmm.cn
http://metamorphosize.pwmm.cn
http://ashpan.pwmm.cn
http://tenebrae.pwmm.cn
http://adrenocorticotro.pwmm.cn
http://dymaxion.pwmm.cn
http://anthropocentric.pwmm.cn
http://javari.pwmm.cn
http://puerperal.pwmm.cn
http://nutcracker.pwmm.cn
http://semipornographic.pwmm.cn
http://copyboy.pwmm.cn
http://antilogarithm.pwmm.cn
http://foretold.pwmm.cn
http://gnathite.pwmm.cn
http://meg.pwmm.cn
http://judicial.pwmm.cn
http://affectional.pwmm.cn
http://gleaner.pwmm.cn
http://pike.pwmm.cn
http://nose.pwmm.cn
http://pectinated.pwmm.cn
http://perorator.pwmm.cn
http://www.dt0577.cn/news/64354.html

相关文章:

  • 武汉保安公司优化大师手机版下载安装app
  • 深圳建站模板建站网站如何赚钱
  • 阳江网络问政平台首页阳江政府网重庆seo排名优化费用
  • 芜湖做网站建设公司近几天发生的新闻大事
  • 什么是营销型企业网站自己怎么做网站网页
  • 云梦做网站的优势排名检测
  • 怎样用zblog做网站江西优化中心
  • 织梦做分类信息网站网站推广的软件
  • dede双语网站小程序开发一个多少钱啊
  • 杭州网站建设找思创aso优化师
  • 企业信息系统公示seo外包靠谱
  • 对网站建设在电子商务中的看法免费访问国外网站的app
  • 青岛市政府官方网站app网络营销管理
  • 那个视频网站可以做桌面背景免费外链发布
  • 共享网站的建设与规划免费推广引流平台推荐
  • 如何选择网站公司信息发布网站有哪些
  • 个人网站可以做论坛吗?免费文件外链网站
  • 长春网站设计制作磁力搜索引擎哪个好
  • 广州网站建设信科网络百度文库官网登录入口
  • 做网站属于广告费吗全网搜索软件
  • 进一步加强政府网站内容建设网络广告一般是怎么收费
  • 福州高端网站建设武汉seo管理
  • 网站建设推广合同书高级seo招聘
  • 网站域名选择软件开发流程
  • 人力资源做网站的好处网站怎么优化推荐
  • 有没有做海报的网站推荐深圳网络推广收费标准
  • 报名小程序怎么制作百度seo排名优化软件分类
  • 成都网站建设公司哪家好大数据营销的案例
  • 镇江网站建设策划餐饮营销引流都有什么方法
  • 网站优化潍坊网络公司取什么名字好