简体   繁体   English

通过Java Android中的Reflection使用对象的功能

[英]Use a function of an object through Reflection in Java Android

I have a little problem on a Java Android application, I'm developing a little game and I'm loading there dynamically some pictures throw reflection. 我在Java Android应用程序上遇到了一个小问题,我正在开发一个小游戏,并在其中动态加载一些图片,以进行反射。

I'm having an Asset class with a lot of objects called Image (for each resource in my game I have an object of Image class), this Image objects in fact are bitmaps with some extra functions. 我有一个Asset类,其中包含许多称为Image的对象(对于我的游戏中的每个资源,我都有一个Image类的对象),实际上,这个Image对象是具有一些额外功能的位图。 Exactly they have a function called Resize that I need to use. 确实,它们具有我需要使用的称为调整大小的功能。

At the moment I'm loading all the images dynamically throw reflection in a loop, inside that loop I have more or less something like this (not exactly this but I have wrote it in that way so you can understand how it works): 目前,我正在动态加载所有图像,并在一个循环中进行反射,在该循环中,我或多或少有这样的内容(不完全是这样,但我已经以此方式编写了它,以便您可以理解它是如何工作的):

    Assets assetsRef = new Assets();
    String name = "picture97";
    picture = assetsRef.getClass().getField(name);
    picture.set(picture, g.newImage(name, ImageFormat.RGB565));

However after loading all those pictures I want to apply the function called Resize. 但是,在加载所有这些图片之后,我想应用称为“调整大小”的功能。 In other and taking into consideration the previous code lines, I would like to use the function that is contained on the object called picture97, that is a variable of type Image inside the Assets class. 在其他方面,并考虑到前面的代码行,我想使用包含在名为picture97的对象上的函数,该函数是Assets类中Image类型的变量。

Anyone can help me? 有人可以帮助我吗? I'm quite sure that there should be an easy way to maybe select an object through reflection, but by the moment I have not been able to do so. 我很确定应该有一种简单的方法可以通过反射选择对象,但是到目前为止我还没有做到这一点。

Lot of thanks 非常感谢

as

Image image = (Image) picture.get(); // from Field to the object which is stored in the field


Method  method = image.getClass().getDeclaredMethod("Resize",  (Class<?>[]) null);
method.invoke(image, (Object[]) null);

This applies to calling methods without parameters. 这适用于不带参数的调用方法。 If your method "Resize" requires paramters, you got to supply the types of them to the call for retrieving the method as well as passing the arguments on invoke. 如果您的方法“ Resize”需要参数,则必须将其类型提供给调用以检索方法并在invoke时传递参数。

Method is the reflection object used for investigating methods of classes, calling "invoke" on a Method - object requires the targeted object to be passed as argument to invoke. Method是用于研究类方法的反射对象,在Method上调用“调用”-对象需要将目标对象作为参数传递来调用。 The Class[] argument passed to getDeclaredMethod represent the signature of the method you are trying to invoke as there maybe more then one method with the same name but different parameters and parameter types (overloading) , say void Resize(), void Resize(int x, int y). 传递给getDeclaredMethod的Class []参数表示您尝试调用的方法的签名,因为可能存在一个名称相同但参数和参数类型不同(重载)的方法,例如void Resize(),void Resize(int) x,int y)。 The call to invoke is similar to directly calling the method on the object, so you got to provide the parameters you would in your normal call. 调用的调用类似于直接在对象上调用方法,因此您必须提供在常规调用中将要提供的参数。 Because the reflection api must provide a generic way of calling your method, this arguments must be cast to Oject[], imagine that otherwise an overload of Method.invoke would have to be available for any methods signature, and because this includes your own defined class types, this wouldn't be possible. 因为反射api必须提供调用方法的通用方法,所以必须将此参数强制转换为Oject [],请设想否则,Method.invoke的重载将必须可用于任何方法签名,并且因为这包括您自己定义的方法类类型,这是不可能的。 (Well, it would, if the class would be generated at runtime and after investigating the code in some cases, but then dynamic class loading would have to retrigger the generation, loading and compiling as well as linking of the Method - class, which would be some kind of overkill). (好吧,如果在某些情况下会在运行时以及研究代码后生成类,那么动态类加载将不得不重新触发Method-类的生成,加载和编译以及链接,被某种矫kill过正)。

But indeed in this situation, where you both know the exact type of your object and the method you want to invoke (which is not changing), I would rather recommend you to cast the value of the field to Image and then invoke Resize on that object, like so: 但是确实在这种情况下,您既知道对象的确切类型,又想知道要调用的方法(没有改变),我建议您将字段的值强制转换为Image,然后在该对象上调用Resize。对象,如下所示:

Image image = (Image) picture.get(); 图片image =(Image)picture.get(); image.Resize(); image.Resize();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM