java获取类内容

      • Book类
  1. public class Book implements Serializable {
  2. private int id;
  3. private String name;
  4. private int tid;
  5. private double price;
  6. private String author;
  7. private String descri;
  8. private String photo;
  9. private Date pubdate;
  10. public static final double PI=3.14;
  11. //记住对方
  12. private Type type;
  13.  
  14. public Book() {//公有的
  15. }
  16. Book(int id){//包可见性
  17. this.id=id;
  18. }
  19. public Type getType() {
  20. return type;
  21. }
  22. public void setType(Type type) {
  23. this.type = type;
  24. }
  25. public int getId() {
  26. return id;
  27. }
  28. public void setId(int id) {
  29. this.id = id;
  30. }
  31. public String getName() {
  32. return name;
  33. }
  34. public void setName(String name) {
  35. this.name = name;
  36. }
  37. public int getTid() {
  38. return tid;
  39. }
  40. public void setTid(int tid) {
  41. this.tid = tid;
  42. }
  43. public double getPrice() {
  44. return price;
  45. }
  46. public void setPrice(double price) {
  47. this.price = price;
  48. }
  49. public String getAuthor() {
  50. return author;
  51. }
  52. public void setAuthor(String author) {
  53. this.author = author;
  54. }
  55. public String getDescri() {
  56. return descri;
  57. }
  58. public void setDescri(String descri) {
  59. this.descri = descri;
  60. }
  61. public String getPhoto() {
  62. return photo;
  63. }
  64. public void setPhoto(String photo) {
  65. this.photo = photo;
  66. }
  67. public Date getPubdate() {
  68. return pubdate;
  69. }
  70. public void setPubdate(Date pubdate) {
  71. this.pubdate = pubdate;
  72. }
  73. private void test(){}
  74. @Override
  75. public String toString() {
  76. return "Book [id=" + id + ", name=" + name + ", tid=" + tid + ", price=" + price
  77. + ", author=" + author
  78. + ", descri=" + descri + ", photo=" + photo + ", pubdate=" + pubdate
  79. + "]";
  80. }
  81. }
      • 看父类,实现的接口和构造函数
  1. public class ReflectDemo01 {
  2. public static void main(String[] args) throws NoSuchMethodException,
  3. SecurityException {
  4. //照相法 1:通过静态属性 class
  5. //该方式在源代码中规定好了看那个类,不能在运行时动态改变
  6. Class clazz1=Book.class;
  7. System.out.println("看父类");
  8. System.out.println(clazz1.getSuperclass());
  9. System.out.println("看实现了那些接口");
  10. Class[] cls=clazz1.getInterfaces();
  11. for (Class class1 : cls) {
  12. System.out.println(class1);
  13. }
  14. System.out.println("看所有构造函数");
  15. Constructor[] cs1=clazz1.getDeclaredConstructors();
  16. for (Constructor constructor : cs1) {
  17. System.out.println(constructor);
  18. }
  19. System.out.println("看所有 public 的函数");
  20. Constructor[] cs2=clazz1.getConstructors();
  21. for (Constructor constructor : cs2) {
  22. System.out.println(constructor);
  23. }
  24. System.out.println("看某个公有的");
  25. //Constructor cons1=clazz1.getConstructor(int.class);
  26. //System.out.println(cons1);
  27. Constructor cons2=clazz1.getConstructor();
  28. System.out.println(cons2);
  29. System.out.println("看某个");
  30. Constructor cons3=clazz1.getDeclaredConstructor(int.class);
  31. System.out.println(cons3);
  32. }
  33. }

z

      •  看方法 
  1. public class ReflectDemo02 {
  2. public static void main(String[] args)
  3. throws ClassNotFoundException, IOException, NoSuchMethodException,
  4. SecurityException {
  5. // 该方式在源代码中规定好了看那个类,不能在运行时动态改变
  6. Class clazz1 = Book.class;
  7. // 看所有公有方法:包括继承
  8. Method[] ms1 = clazz1.getMethods();
  9. for (Method method : ms1) {
  10. System.out.println(method);
  11. }
  12. // 看本类定义的方法:不包括继承
  13. System.out.println("--------------------");
  14. Method[] ms2 = clazz1.getDeclaredMethods();
  15. for (Method method : ms2) {
  16. System.out.println(method);
  17. }
  18. System.out.println("++++++++++++++++++++++");
  19. // 看某个公有的
  20. Method m1 = clazz1.getMethod("setPubdate", Date.class);
  21. System.out.println(m1);
  22. // Method m2=clazz1.getMethod("test");
  23. // System.out.println(m2);
  24. System.out.println("=============================");
  25. // 看本类定义的方法
  26. Method m3 = clazz1.getDeclaredMethod("setPubdate", Date.class);
  27. System.out.println(m3);
  28. Method m4=clazz1.getDeclaredMethod("test");
  29. System.out.println(m4);
  30. }
  31. }
      • 看字段
  1. public class ReflectDemo03 {
  2. public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException {
  3.   Class clazz1 = Book.class;
  4.    // 看所有公有字段,包括继承的
  5.   Field[] fs1 = clazz1.getFields();
  6.   for (Field field : fs1) {
  7.     System.out.println(field);
  8.   }
  9.     System.out.println("===========================");
  10.   // 看本类定义的
  11.   Field[] fs2 = clazz1.getDeclaredFields();
  12.   for (Field field : fs2) {
  13.     System.out.println(field);
  14.   }
  15.     System.out.println("===========================");
  16.   // 看本类定义的某个字段
  17.   Field field1 = clazz1.getDeclaredField("name");
  18.   System.out.println(field1);
  19.   // 看公有字段
  20.   //下面两行代码抛出异常:Exception in thread "main"
  21.   java.lang.NoSuchFieldException: name
  22.   //Field field2 = clazz1.getField("name");
  23.   //System.out.println(field2);
  24.   }
  25. }

构造函数的调用

  • 通用方法
  1. public class ReflectDemo01 {
  2. public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException,
      IllegalArgumentException, InvocationTargetException {
  3.   // 1 找到像
  4.   Class<Book> clazz1 = Book.class;
  5.   // 2 找到构造函数
  6.   Constructor<Book> cs = clazz1.getDeclaredConstructor(int.class);
  7.     System.out.println(cs);
  8.   cs.setAccessible(true);//不设置访问不了,因为不再同一个包,修改这种行为
  9.   // 3 调用构造函数
  10.   Book book = cs.newInstance(1);
  11.   System.out.println(book);
  12.   }
  13. }
  • 无参函数调用
  1. public class ReflectDemo02 {
  2.   public static void main(String[] args) throws NoSuchMethodException,
  3.   SecurityException, InstantiationException, IllegalAccessException,
  4.   IllegalArgumentException, InvocationTargetException {
  5.     // 1 找到像
  6.     Class<Book> clazz1 = Book.class;
  7.     // 2 找到构造函数
  8.     Constructor<Book> cs = clazz1.getDeclaredConstructor();
  9.     // 3 调用构造函数
  10.     Book book = cs.newInstance();
  11.     System.out.println(book);
  12.     //为了简化调用无参数构造函数,可以直接调用像的方法
  13.     Book book2=clazz1.newInstance();
  14.     System.out.println(book2);
  15.   }
  16. }

java获取类内容的更多相关文章

  1. java获取类的信息

    关键技术剖析 1.java.lang.reflect包实现了java的反射机制,在使用反射机制时,需要导入该包. 2.Class类的forName方法能够根据类名加载类,获得类的Class对象. Cl ...

  2. java反射类内容获取

    private void DtoReflect(Object obj, MqDto mqDto) throws Exception { Map map = getMap(mqDto); if(obj= ...

  3. java获取类路径下文件的绝对路径

    获取文件绝对路径 在idea中,默认的当前路径是project的根路径,如果你使用idea的默认路径,只要离开idea换到其他位置,可能当前路径就不是project的根路径了. 使用一下通用方式的前提 ...

  4. java 获取pdf内容

    1. 说明 将pdf中的文字读取处理还有一些限制:1. 文档的安全属性不能过于严格 2. 不能存在图片. 2. 直接贴相关的源码 有两种读取方式,maven对应的pom文件 <dependenc ...

  5. java 获取类路径

    package com.jason.test; import java.io.File; import java.io.IOException; import java.net.URL; public ...

  6. java获取类的3种方式

    1.Class.forName("全类名"):将字节吗文件加载进内存,返回Class对象,多用于配指文件,将类名定义在配置文件中,便于利用java的反射机制生成类对象,加载类. / ...

  7. Java获取类路径的方式

    Java环境中,如何获取当前类的路径.如何获取项目根路径等: @Test public void showURL() throws IOException { // 第一种:获取类加载的根路径 Fil ...

  8. java获取类的全类名----类名.class.getName()的作用是获取这个类的全类名

    类名.class.getName()的作用是获取这个类的全类名

  9. java 获取类路劲注意点

    在resin里用MyConstants.class.getResource("/").getPath(),这个方法时,获取到的路劲少[项目名称],最好用MyConstants.cl ...

  10. java获取类路径

    String file = MessageTask3.class.getResource("").getFile(); File: public static final Stri ...

随机推荐

  1. 关闭Windows form窗体

    原文https://www.cnblogs.com/HappyEDay/p/5713707.html  在c#中退出WinForm程序包括有很多方法 this.Close(); Application ...

  2. 2.VS编写XML实例程序

    在VS中编写XML实例程序 1.如下,在 VS 中分别新建  XML 文件.XML 解析类.XML 实体类 2.在项目中代码,如下 (1)XML 文件(注:在 VS 解决方案资源管理器中选中 XML ...

  3. el-input 限制输入框只能输入数字和小数

    方法一: oninput ="value=value.replace(/[^\d]/g,'')" //只能输入数字 oninput ="value=value.repla ...

  4. Mac10.13-10.15 下玩星际争霸1.16

    星际争霸DMG 存储在城通网盘,下载后挂载,复制到 应用程序 里就可以玩了 1, ctfile://xturlDG9QOlg_V29WOwI8UzEKZQdjUWEOOFJ7VCEHYFIxBzlTY ...

  5. Docker--搭建 Gitlab 容器并上传本地项目代码

    本文参考:https://www.cnblogs.com/poloyy/p/13969756.html 搭建 Gitlab 容器 搜索 gitlab 镜像 docker search gitlab 创 ...

  6. 替代学习物联网-云服务-02阿里云MQTT

     1.支付宝登录,进入物联网平台 https://iot.console.aliyun.com/product  2.新建产品  3.添加设备 4.设备连接参数  5.连接到阿里云

  7. Map 使用

    1.替换map中的某个key Map<String,Object> map = new HashMap<>(); map.put("新key",map.re ...

  8. IDEA导出带源码的war包

    做作业时实验要求导出带源码的war包,网上找了一圈没找着,遂自行探索,摸出了些门道,在此分享. File->Project Structure->Project Setting->A ...

  9. flutter json_serializable数据模型的建立和封装

    为了方便数据使用,我们将服务器拿到的数据转换为map类型,但是在使用是大量的数据会让使用map时头大,比如每个map都key都需要手动输入,很是麻烦. 本文使用了json_serializable将m ...

  10. elementUI 中日历自定义内容 配置具体内容

    效果图如下: 代码如下: <template> <div class="con-main"> <div class="con-list&qu ...