Java:动态属性 [英] Java: dynamic properties

查看:303
本文介绍了Java:动态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

女士们先生们,



我是新来的Java,原谅我,如果它很明显,但我没有发现很多。



我想在运行时为类创建动态属性(变量)(定义可以通过添加或更改属性和方法在运行时更改的对象)。






原因:我想在GAE中存储数据模型,可以在编译应用程序后动态扩展(是的,DataStore允许)。应该添加什么属性存储在DataStore中(它就像使用机器人来构建机器人...有趣)。



Python允许我添加属性运行。 Groovy似乎也允许。 纯粹的Java世界中唯一表明在这个方向的东西似乎是动态代理。



但是,如果他们做这个伎俩,我还不清楚

解决方案

Java没有动态添加属性的功能。它也没有能力在运行时动态创建类或在运行时更改它们。 Java是强大且静态地键入。你可以做的最好的就是把这样的属性放到一个 Map 或类似的文件中。



编辑:强>好的,显然有一些澄清是有序的。 OP专门提到的GAE,这些方法的将可以工作,但我会提及他们,因为有些似乎在他们的缺席中被例外。



Java Compiler API ( Java 6+)允许您在运行时编译Java类。从技术上讲,您可以编写一个Java源文件来查看您想要的内容,编译并加载它。



Java字节码库可以在运行时重写类。这样的库被JPA(和其他)使用。您可以通过这种方式修改类。



然而,OP所指的是a)参考GAE和b)更符合Javascript允许的顺序通过动态添加,删除或更改属性来在运行时修改类或特定实例。 Java肯定不会这样做,具体而言不在GAE上。



上面的例子就像将一个类转换为 char * 在C ++中,所以你可以看到私人成员并不意味着C ++没有私有成员。即使它们是Java的一部分,您本质上也绕过了Java运行时这两种方法。


Ladies & Gentlemen,

I´m new to Java, forgive me if it´s obvious, but I haven´t found much about it.

I´d like to create dynamic properties (variables) for a class at runtime (define an object that can be altered at runtime by adding or changing properties and methods).


Reason: I want to store a data model in GAE that can be extended dynamically after compilation of the app (yes, the DataStore allows that). What properties should be added are stored in the DataStore as well (It´s like using Robots to built Robots...funny).

Python allows me to add properties at Runtime. Groovy seems to allow that, too. The only thing in the "pure" Java world indicating in that direction seems to be "Dynamic Proxies".

But I couldn´t figure out yet if they do the trick.

解决方案

Java doesn't have the capability to dynamically add properties. Nor does it have the ability to dynamically create classes at runtime or change them at runtime. Java is strongly and statically typed. The best you can do is put such properties into a Map or similar.

Edit: Ok, apparently some clarifications are in order. The OP specifically mentioned GAE, which none of these methods will work on but I'll mention them since some seem to take exception to their absence.

The Java Compiler API (Java 6+) allows you to compile Java classes at runtime. Technically you could write out a Java source file to look exactly how you want, compile it and load it.

Java bytecode libraries can rewrite classes at runtime. This is used by such libraries as JPA (and others). You could modify classes this way.

What the OP is referring to however is a) in reference to working on GAE and b) more in the order of how Javascript allows you to modify classes or particular instances at runtime by dynamically adding, removing or change properties. Java certainly doesn't do this and specifically doesn't on the GAE.

The above is not an exception to this just like casting a class to char * in C++ so you can read private members doesn't mean C++ doesn't have private members. You're essentially bypassing the Java runtime with both of these methods even though they're part of Java.

这篇关于Java:动态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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