动态编辑/ Java中的Andr​​oid创建类 [英] Dynamically editing/creating classes in Java Android

查看:256
本文介绍了动态编辑/ Java中的Andr​​oid创建类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找一种方式来动态定义类和Android的实例化它们在运行时。从我的理解,这在Android中已经完成,我只是需要一些帮助计算出来。

I am looking for a way to dynamically define classes and instantiate them in Android, at runtime. From my understanding, this is already done in Android, I just need some help figuring it out.

我可以一个类似的结果,在Javascript和PHP。我知道它可以在Java中使用类似ASM,BCEL或者CGLIB来完成。但是,我不知道有足够的了解任何一个了解他们是否会在Android上运行。或者,他们将工作,有什么意义呢?

I can a similiar result in Javascript and PHP. I know it can be done in Java using something like ASM, BCEL or CGlib. However, I do not know enough about any of these to understand if they will work on Android. Or, of they will work, what are the implications?

如果,假设,所有三个将在Android的工作,可有人点我在正确的方向在何处开始了解它的使用,以及如何使用它?

If, hypothetically, all three will work in Android, can someone point me in the correct direction as to where to start understanding which to use, and how to use it?

我没有做过太多的Java编程,而我才刚刚最近与它在Android的工作,所以,我AP preciate所有帮助/纠正我可以得到的。随着中说,我会AP preciate如果你的答案并不是简单:不要这样做。我在寻找如何做到这一点具体,而不是如何做是正确的。至少,直到我的应用程序自带轰然倒下。 :)

I haven't done much Java programming, and I have only just recently been working with it in Android, so, I appreciate all of the help/correction I can get. With that said, I would appreciate if your answer is NOT simply: Don't do this. I am looking for how to do this specifically, not how to do it right. At least, not until my app comes crashing down. :)

我认为,这已经发生在Android在以下几种情况:(不是100%确定)

I believe that this already happens in Android in the following situations: (Not 100% SURE)

创建从JSON对象。
AIDL

Creating an object from JSON. AIDL

推荐答案

不这样做:)

其实,我怀疑有一些这样的行为JSON库;这两个公认的方法,我知道(我不是这方面的专家,虽然)是要么建立某种形式的数据结构的控股name-value对 - 即添加内容到数据结构,但不能创建一个新的类 - 或者prepare将从一个JSON对象来填充类的模板。

I actually doubt there are JSON libraries that behave this way; the two accepted ways I know (I am not an expert on this, though) are either to create some sort of data structure holding name-value pairs - i.e. add stuff to a data structure but not create a new class - or prepare a template of a class which will be populated from a JSON object.

Java的,而静态类型,是不是真的适合在运行时创建全新的类,并没有为无反射的支持 - 尽管有一个用于访问未知类型的对象(例如查询其所有领域的支持/方法)。

Java, being statically-typed, is not really suitable for creating whole new classes at run-time, and there is no reflection support for that - though there is support for accessing objects of unknown types (e.g. querying for all their fields / methods).

什么的能做的就是手动编写一个Java类文件 - 无论是在Java中code,然后在字节$编译它以某种方式或直接C $ç - 然后加载该文件在运行时。它的丑陋,但它会奏效。然后,它只是一样的类的任何运行时加载 - 要么你依靠加载的类的基类/接口上,或者你必须使用反射做什么有意义的事情吧。

What you can do is to manually write a java class to a file - either in Java code and then compile it somehow, or directly in bytecode - and then load that file at runtime. It's ugly, but it will work. Then it's just the same as any runtime loading of classes - either you rely on the base class / interface of the loaded class, or you have to use reflection to do anything meaningful with it.

这篇关于动态编辑/ Java中的Andr​​oid创建类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆