Reflection Basics and Class Class

博客介绍了标准J2SE平台库中的反射API,它能让类进行自我反射。可通过反射API发现类、方法和字段的名称,还能调用方法和访问数组。详细说明了如何获取类的实例、类名、超类,以及如何获取和调用类的方法,包括特定方法的调用。

The standard J2SE platform libraries include a reflection API. This API allows classes to reflect on themselves, to see their inner selves. The reflection API lets you discover the name of classes, methods, and fields. You can also invoke those methods and access arrays without using square brackets.

The heart of the reflection API is the Class class. This class allows you to find out the name of a class, its access modifier, fields, methods, and so forth. For any instance of a class, you can get its Class class by calling the getClass method:

Class c = anInstance.getClass();

If you don't happen to have an instance of class (and don't want to create one), just attach .class to the end of the class name and you have the Class instance for that class.

Class c = MyClass.class;

The same even works for primitives:

Class c = int.class;

This last one might seem odd, but it allows you to specify argument types when calling methods (via Reflection) that accept primitive arguments.

One thing typically done is create a Class by passing its string name to the forName method of Class.

Class c = Class.forName(“java.awt.Button”);

This is done so that at the compile time you don't have to have the class within the quoted string available.

Once you have a Class class, you can find out the name of the class with its getName method:

Class c = java.awt.Button.class;

System.out.println(“Name: ” + c.getName());

Or, you can find out its superclass with its getSuperClass:

System.out.println(“Super: ” + c.getSuperClass().getName());

Moving from classes to methods takes us to the Method class, found in the java.lang.reflect package. With the Method class, you can discover all methods of a class (with getMethods) and even invoke them (with invoke).

The following program demostrates getting the mothods of a class:

import java.lang.reflect.*;



public class ListMethods {

public static void main(String[] args) {

if (args.length == 0) {

System.err.println(“Please include full qualified class name on command line”);

return;

}

for (int i = 0, n = args.length; i < n; ++i) {

listMethods(args[i]);

}

}



private static void listMethods(String name) {

try {

Class c = Class.forName(name);

System.out.println(“----” + c.getName() + “----”);

Method[] methods = c.getMethods();

for (int i = 0, n = methods.length; i < n; ++i) {

System.out.println(methods[i].getName());

System.out.println(“/t”, methods[i]);

}

} catch (ClassNotFoundException e) {

System.out.println(“Bad classname: “+ name);

}

}

}

Notice that the output includes all methods available through its superclass, too. To limit the output to only those methods declared in the class itself, change the getMethods call to getDeclaredMethods.

While the getMethods of Class allows you to get all the methods in a class, more typically, you want a specific method of a class. For that, there's the getMethod(String name, Class[] types) method. Once you have that method, you can invoke it with the invoke(Object instance, Object[] args) method. For static methods, the instance argument can be null.

The value for the arguments don't matter when finding a method, only the class types. At invoke time, you pass in the actual arguments values. For primitive types, you must box them up as objects (like using Integer for int).

The following demostrates invoking a method through reflection, and inserting a String in the middle of a StringBuffer.

StringBuffer buffer = new StringBuffer(“Held”);

Class c = buffer.getClass();

Class[] types = {int.class, String.class};

Method method = c.getMethod(“insert”, types);

Object[] theArgs = {new Integer(2), “llo, wor”};

method.invoke(buffer, theArgs);

带开环升压转换器和逆变器的太阳能光伏系统 太阳能光伏系统驱动开环升压转换器和SPWM逆变器提供波形稳定、设计简单的交流电的模型 Simulink模型展示了一个完整的基于太阳能光伏的直流到交流电力转换系统,该系统由简单、透明、易于理解的模块构建而成。该系统从配置为提供真实直流输出电压的光伏阵列开始,然后由开环DC-DC升压转换器进行处理。升压转换器将光伏电压提高到适合为单相全桥逆变器供电的稳定直流链路电平。 逆变器使用正弦PWM(SPWM)开关来产生干净的交流输出波形,使该模型成为研究直流-交流转换基本操作的理想选择。该设计避免了闭环和MPPT的复杂性,使用户能够专注于光伏接口、升压转换和逆变器开关的核心概念。 此模型包含的主要功能: •太阳能光伏阵列在标准条件下产生~200V电压 •具有固定占空比操作的开环升压转换器 •直流链路电容器,用于平滑和稳定转换器输出 •单相全桥SPWM逆变器 •交流负载,用于观察实际输出行为 •显示光伏电压、升压输出、直流链路电压、逆变器交流波形和负载电流的组织良好的范围 •完全可编辑的结构,适合分析、实验和扩展 该模型旨在为太阳能直流-交流转换提供一个干净高效的仿真框架。布局简单明了,允许用户快速了解信号流,检查各个阶段,并根据需要修改参数。 系统架构有意保持模块化,因此可以轻松扩展,例如通过添加MPPT、动态负载行为、闭环升压控制或并网逆变器概念。该模型为进一步开发或整合到更大的可再生能源模拟中奠定了坚实的基础。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值