java获取类内容
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获取类内容的更多相关文章
- java获取类的信息
关键技术剖析 1.java.lang.reflect包实现了java的反射机制,在使用反射机制时,需要导入该包. 2.Class类的forName方法能够根据类名加载类,获得类的Class对象. Cl ...
- java反射类内容获取
private void DtoReflect(Object obj, MqDto mqDto) throws Exception { Map map = getMap(mqDto); if(obj= ...
- java获取类路径下文件的绝对路径
获取文件绝对路径 在idea中,默认的当前路径是project的根路径,如果你使用idea的默认路径,只要离开idea换到其他位置,可能当前路径就不是project的根路径了. 使用一下通用方式的前提 ...
- java 获取pdf内容
1. 说明 将pdf中的文字读取处理还有一些限制:1. 文档的安全属性不能过于严格 2. 不能存在图片. 2. 直接贴相关的源码 有两种读取方式,maven对应的pom文件 <dependenc ...
- java 获取类路径
package com.jason.test; import java.io.File; import java.io.IOException; import java.net.URL; public ...
- java获取类的3种方式
1.Class.forName("全类名"):将字节吗文件加载进内存,返回Class对象,多用于配指文件,将类名定义在配置文件中,便于利用java的反射机制生成类对象,加载类. / ...
- Java获取类路径的方式
Java环境中,如何获取当前类的路径.如何获取项目根路径等: @Test public void showURL() throws IOException { // 第一种:获取类加载的根路径 Fil ...
- java获取类的全类名----类名.class.getName()的作用是获取这个类的全类名
类名.class.getName()的作用是获取这个类的全类名
- java 获取类路劲注意点
在resin里用MyConstants.class.getResource("/").getPath(),这个方法时,获取到的路劲少[项目名称],最好用MyConstants.cl ...
- java获取类路径
String file = MessageTask3.class.getResource("").getFile(); File: public static final Stri ...
随机推荐
- 关闭Windows form窗体
原文https://www.cnblogs.com/HappyEDay/p/5713707.html 在c#中退出WinForm程序包括有很多方法 this.Close(); Application ...
- 2.VS编写XML实例程序
在VS中编写XML实例程序 1.如下,在 VS 中分别新建 XML 文件.XML 解析类.XML 实体类 2.在项目中代码,如下 (1)XML 文件(注:在 VS 解决方案资源管理器中选中 XML ...
- el-input 限制输入框只能输入数字和小数
方法一: oninput ="value=value.replace(/[^\d]/g,'')" //只能输入数字 oninput ="value=value.repla ...
- Mac10.13-10.15 下玩星际争霸1.16
星际争霸DMG 存储在城通网盘,下载后挂载,复制到 应用程序 里就可以玩了 1, ctfile://xturlDG9QOlg_V29WOwI8UzEKZQdjUWEOOFJ7VCEHYFIxBzlTY ...
- Docker--搭建 Gitlab 容器并上传本地项目代码
本文参考:https://www.cnblogs.com/poloyy/p/13969756.html 搭建 Gitlab 容器 搜索 gitlab 镜像 docker search gitlab 创 ...
- 替代学习物联网-云服务-02阿里云MQTT
1.支付宝登录,进入物联网平台 https://iot.console.aliyun.com/product 2.新建产品 3.添加设备 4.设备连接参数 5.连接到阿里云
- Map 使用
1.替换map中的某个key Map<String,Object> map = new HashMap<>(); map.put("新key",map.re ...
- IDEA导出带源码的war包
做作业时实验要求导出带源码的war包,网上找了一圈没找着,遂自行探索,摸出了些门道,在此分享. File->Project Structure->Project Setting->A ...
- flutter json_serializable数据模型的建立和封装
为了方便数据使用,我们将服务器拿到的数据转换为map类型,但是在使用是大量的数据会让使用map时头大,比如每个map都key都需要手动输入,很是麻烦. 本文使用了json_serializable将m ...
- elementUI 中日历自定义内容 配置具体内容
效果图如下: 代码如下: <template> <div class="con-main"> <div class="con-list&qu ...