java获取类内容

      • Book类
public class Book implements Serializable {
private int id;
private String name;
private int tid;
private double price;
private String author;
private String descri;
private String photo;
private Date pubdate;
public static final double PI=3.14;
//记住对方
private Type type; public Book() {//公有的
}
Book(int id){//包可见性
this.id=id;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTid() {
return tid;
}
public void setTid(int tid) {
this.tid = tid;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDescri() {
return descri;
}
public void setDescri(String descri) {
this.descri = descri;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
public Date getPubdate() {
return pubdate;
}
public void setPubdate(Date pubdate) {
this.pubdate = pubdate;
}
private void test(){}
@Override
public String toString() {
return "Book [id=" + id + ", name=" + name + ", tid=" + tid + ", price=" + price
+ ", author=" + author
+ ", descri=" + descri + ", photo=" + photo + ", pubdate=" + pubdate
+ "]";
}
}
      • 看父类,实现的接口和构造函数
public class ReflectDemo01 {
public static void main(String[] args) throws NoSuchMethodException,
SecurityException {
//照相法 1:通过静态属性 class
//该方式在源代码中规定好了看那个类,不能在运行时动态改变
Class clazz1=Book.class;
System.out.println("看父类");
System.out.println(clazz1.getSuperclass());
System.out.println("看实现了那些接口");
Class[] cls=clazz1.getInterfaces();
for (Class class1 : cls) {
System.out.println(class1);
}
System.out.println("看所有构造函数");
Constructor[] cs1=clazz1.getDeclaredConstructors();
for (Constructor constructor : cs1) {
System.out.println(constructor);
}
System.out.println("看所有 public 的函数");
Constructor[] cs2=clazz1.getConstructors();
for (Constructor constructor : cs2) {
System.out.println(constructor);
}
System.out.println("看某个公有的");
//Constructor cons1=clazz1.getConstructor(int.class);
//System.out.println(cons1);
Constructor cons2=clazz1.getConstructor();
System.out.println(cons2);
System.out.println("看某个");
Constructor cons3=clazz1.getDeclaredConstructor(int.class);
System.out.println(cons3);
}
}

z

      •  看方法 
public class ReflectDemo02 {
public static void main(String[] args)
throws ClassNotFoundException, IOException, NoSuchMethodException,
SecurityException {
// 该方式在源代码中规定好了看那个类,不能在运行时动态改变
Class clazz1 = Book.class;
// 看所有公有方法:包括继承
Method[] ms1 = clazz1.getMethods();
for (Method method : ms1) {
System.out.println(method);
}
// 看本类定义的方法:不包括继承
System.out.println("--------------------");
Method[] ms2 = clazz1.getDeclaredMethods();
for (Method method : ms2) {
System.out.println(method);
}
System.out.println("++++++++++++++++++++++");
// 看某个公有的
Method m1 = clazz1.getMethod("setPubdate", Date.class);
System.out.println(m1);
// Method m2=clazz1.getMethod("test");
// System.out.println(m2);
System.out.println("=============================");
// 看本类定义的方法
Method m3 = clazz1.getDeclaredMethod("setPubdate", Date.class);
System.out.println(m3);
Method m4=clazz1.getDeclaredMethod("test");
System.out.println(m4);
}
}
      • 看字段
public class ReflectDemo03 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException {
  Class clazz1 = Book.class;
   // 看所有公有字段,包括继承的
  Field[] fs1 = clazz1.getFields();
  for (Field field : fs1) {
    System.out.println(field);
  }
    System.out.println("===========================");
  // 看本类定义的
  Field[] fs2 = clazz1.getDeclaredFields();
  for (Field field : fs2) {
    System.out.println(field);
  }
    System.out.println("===========================");
  // 看本类定义的某个字段
  Field field1 = clazz1.getDeclaredField("name");
  System.out.println(field1);
  // 看公有字段
  //下面两行代码抛出异常:Exception in thread "main"
  java.lang.NoSuchFieldException: name
  //Field field2 = clazz1.getField("name");
  //System.out.println(field2);
  }
}

构造函数的调用

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

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. Vue基础(2)双向绑定

    双向数据绑定 通过修改标签,例:切换radio.checkbox......都会对绑定的数据有影响 通过事件触发方法,修改data中数据,反向作用于radio.checkbox...... 1.v-m ...

  2. 12个有用的JavaScript数组技巧

    数组是Javascript最常见的概念之一,它为我们提供了处理数据的许多可能性,熟悉数组的一些常用操作是很有必要的. 1.数组去重 1.from()叠加new Set()方法 字符串或数值型数组的去重 ...

  3. DEV GridControl 主从表 (层次表)

    DataTable dtData= DbHelperOra.Query(strSql2.ToString()).Tables[0]; //主表 dtusers.TableName = "病人 ...

  4. C++ 单向链表手动实现(课后作业版)

    单向链表,并实现增删查改等功能 首先定义节点类,类成员包含当前节点的值和下一个节点的地址 /node definition template <typename T> class Node ...

  5. git 产生冲突的处理方式

    理解你操作图形化的时候, git 在什么? 了解你在做的文件的git状态? 1. 添加文件 git add . -A git commit -m "your commit here" ...

  6. kibana启动时报错:Validation Failed: 1: this action would add [2] total shards, but this cluster currently has [999]/[1000] maximum shards open

    解决方案: curl -XPUT -H "Content-Type:application/json" -d '{"persistent":{"clu ...

  7. Windows MFC HTTP 函数流程

    Windows MFC HTTP 函数流程 1 //建立连接 2 pInternetSession = new CInternetSession(AfxGetAppName()); 3 4 5 6 / ...

  8. go理论知识总结

    基于const常量理解个中类型的内存分配引入参考 官方:Constant expressions may contain only constant operands and are evaluate ...

  9. vue组件淡入浅出动画

    点击动画 hello hello world! .test-enter, .test-leave-to { opacity: 0 } .test-enter-to, .test-leave { opa ...

  10. 【jmeter】请求域名解析失败,添加本地代理

    jmeter HTTP请求URL中使用域名 http://xxx.xxx.xxx,异常:java.net.UnkownHostException 原因:请求域名没有被解析成功,该http请求没有通过本 ...