Java Reflection - Classes

本文详细介绍了如何使用Java反射API来获取类的详细信息,包括类名、修饰符、包信息、超类、实现的接口、构造器、方法、字段及注解等。通过代码示例展示了如何获取这些信息,并提供了进一步研究各主题的链接。

http://tutorials.jenkov.com/java-reflection/classes.html



Using Java Reflection you can inspect Java classes at runtime. Inspecting classes is often the first thing you do when using Reflection. From the classes you can obtain information about

plus a lot more information related to Java classes. For a full list you should consult the JavaDoc for java.lang.Class. This text will briefly touch upon all accessing of the above mentioned information. Some of the topics will also be examined in greater detail in separate texts. For instance, this text will show you how to obtain all methods or a specific method, but a separate text will show you how to invoke that method, how to find the method matching a given set of arguments if more than one method exists with the same name, what exceptions are thrown from method invocation via reflection, how to spot a getter/setter etc. The purpose of this text is primarily to introduce the Class object and the information you can obtain from it.

The Class Object

Before you can do any inspection on a class you need to obtain its java.lang.Class object. All types in Java including the primitive types (int, long, float etc.) including arrays have an associated Class object. If you know the name of the class at compile time you can obtain a Class object like this:

    Class myObjectClass = MyObject.class

If you don't know the name at compile time, but have the class name as a string at runtime, you can do like this:

String className = ... //obtain class name as string at runtime Class class = Class.forName(className);

When using the Class.forName() method you must supply the fully qualified class name. That is the class name including all package names. For instance, if MyObject is located in package com.jenkov.myapp then the fully qualified class name is com.jenkov.myapp.MyObject

The Class.forName() method may throw a ClassNotFoundException if the class cannot be found on the classpath at runtime.

Class Name

From a Class object you can obtain its name in two versions. The fully qualified class name (including package name) is obtained using the getName() method like this:

    Class aClass = ... //obtain Class object. See prev. section
    String className = aClass.getName();

If you want the class name without the pacakge name you can obtain it using the getSimpleName() method, like this:

    Class  aClass          = ... //obtain Class object. See prev. section
    String simpleClassName = aClass.getSimpleName();

Modifiers

You can access the modifiers of a class via the Class object. The class modifiers are the keywords "public", "private", "static" etc. You obtain the class modifiers like this:

  Class  aClass = ... //obtain Class object. See prev. section
  int modifiers = aClass.getModifiers();

The modifiers are packed into an int where each modifier is a flag bit that is either set or cleared. You can check the modifiers using these methods in the class java.lang.reflect.Modifier:

    Modifier.isAbstract(int modifiers)
    Modifier.isFinal(int modifiers)
    Modifier.isInterface(int modifiers)
    Modifier.isNative(int modifiers)
    Modifier.isPrivate(int modifiers)
    Modifier.isProtected(int modifiers)
    Modifier.isPublic(int modifiers)
    Modifier.isStatic(int modifiers)
    Modifier.isStrict(int modifiers)
    Modifier.isSynchronized(int modifiers)
    Modifier.isTransient(int modifiers)
    Modifier.isVolatile(int modifiers)

Package Info

You can obtain information about the package from a Class object like this:

Class  aClass = ... //obtain Class object. See prev. section
Package package = aClass.getPackage();

From the Package object you have access to information about the package like its name. You can also access information specified for this package in the Manifest file of the JAR file this package is located in on the classpath. For instance, you can specify package version numbers in the Manifest file. You can read more about the Package class here: java.lang.Package

Superclass

From the Class object you can access the superclass of the class. Here is how:

Class superclass = aClass.getSuperclass();

The superclass class object is a Class object like any other, so you can continue doing class reflection on that too.

Implemented Interfaces

It is possible to get a list of the interfaces implemented by a given class. Here is how:

Class  aClass = ... //obtain Class object. See prev. section
Class[] interfaces = aClass.getInterfaces();

A class can implement many interfaces. Therefore an array of Class is returned. Interfaces are also represented by Class objects in Java Reflection.

NOTE: Only the interfaces specifically declared implemented by a given class is returned. If a superclass of the class implements an interface, but the class doesn't specifically state that it also implements that interface, that interface will not be returned in the array. Even if the class in practice implements that interface, because the superclass does.

To get a complete list of the interfaces implemented by a given class you will have to consult both the class and its superclasses recursively.

Constructors

You can access the constructors of a class like this:

 Constructor[] constructors = aClass.getConstructors();

Constructors are covered in more detail in the text on Constructors.

Methods

You can access the methods of a class like this:

 Method[] method = aClass.getMethods();

Methods are covered in more detail in the text on Methods.

Fields

You can access the fields (member variables) of a class like this:

 Field[] method = aClass.getFields();

Fields are covered in more detail in the text on Fields.

Annotations

You can access the class annotations of a class like this:

 Annotation[] annotations = aClass.getAnnotations();

Annotations are covered in more detail in the text on Annotations.


MATLAB代码实现了一个基于多种智能优化算法优化RBF神经网络的回归预测模型,其核心是通过智能优化算法自动寻找最优的RBF扩展参数(spread),以提升预测精度。 1.主要功能 多算法优化RBF网络:使用多种智能优化算法优化RBF神经网络的核心参数spread。 回归预测:对输入特征进行回归预测,适用于连续值输出问题。 性能对比:对比不同优化算法在训练集和测试集上的预测性能,绘制适应度曲线、预测对比图、误差指标柱状图等。 2.算法步骤 数据准备:导入数据,随机打乱,划分训练集和测试集(默认7:3)。 数据归一化:使用mapminmax将输入和输出归一化到[0,1]区间。 标准RBF建模:使用固定spread=100建立基准RBF模型。 智能优化循环: 调用优化算法(从指定文件夹中读取算法文件)优化spread参数。 使用优化后的spread重新训练RBF网络。 评估预测结果,保存性能指标。 结果可视化: 绘制适应度曲线、训练集/测试集预测对比图。 绘制误差指标(MAE、RMSE、MAPE、MBE)柱状图。 十种智能优化算法分别是: GWO:灰狼算法 HBA:蜜獾算法 IAO:改进天鹰优化算法,改进①:Tent混沌映射种群初始化,改进②:自适应权重 MFO:飞蛾扑火算法 MPA:海洋捕食者算法 NGO:北方苍鹰算法 OOA:鱼鹰优化算法 RTH:红尾鹰算法 WOA:鲸鱼算法 ZOA:斑马算法
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值