简体   繁体   中英

Is there a way of accessing the properties of an object in java by using a string to represent the property's name, just like in JavaScript?

In JavaScript you can access a property of an object in two ways:

object.property
object["property"]

In Java, I am only aware of the first way. Is there a way to access a Java object's properties by using a string as well?

You could use the Apache OGNL language , it is an external dependency that allows you to access properties with names in strings.

It has many more features, but you cannot expect that it simply expands your Java syntax - you will have to evaluate a String that contains an expression in that language.

In some cases you can embed it, like in Apache Camel, where it is "just another expression language".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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