java获取类内容
2024-09-07 06:26:17
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);
}
}
最新文章
- UIPickerView的使用(一)
- 用tpcc测试对比 innodb 和 tokudb
- Unity4升级Unity5后Image Effects问题
- 【洛谷P1541】乌龟棋
- JMeter学习参数化User Defined Variables与User Parameters
- Makefile.am讲解
- mongodump备份数据库
- C#基本语句与C++区别
- python 网络编程第三版
- 从VS转MyEclipse的15天使用体验
- [Swust OJ 801]--Ordered Fractions
- 基于webpack的React项目搭建(三)
- Python学子之如何退出python 命令行
- 【java】java基本用法记录
- Animator 动画第一次播放正常,之后播放都不正常的问题解决
- 1179: 零起点学算法86——小明A+B(未弄懂)
- python相关的安装软件
- 整合 JIRA 和 Confluence 6
- git之二: git可视化工具sourcetree
- Java与groovy混编 —— 一种兼顾接口清晰和实现敏捷的开发方式
热门文章
- js 方法记录
- 全国分乡镇第六次人口普查数据shp数据库省市区县街道
- Java基于springboot大学生宿舍寝室考勤人脸识别管理系统
- Python学习笔记组织文件之将美国风格日期的文件改名为欧洲风格的日期
- SpringBoot2.2.2+SpringCloud-Hoxton.SR1整合eureka/gateway
- next.js引入antd报错
- Count Triplets That Can Form Two Arrays of Equal XOR
- libev中的__attribute__优化
- 07 如果再使用animateCC2018或者苹果系统使用animate时出现Uncaught ReferenceError: lib is not defined的错误
- c++学习 5 预处理