geting fileds and data types of all members of a Object
i have a method as follows.
test(Object obj){开发者_开发问答
}
now within this method i want to get all fileds along with the datatypes of those fileds. How can i do that??
you will need to use reflection. see this link from the Java tutorial.
keep in mind that reflection can be pretty expensive at runtime, so use it wisely.
As Omry suggested, you need to use Reflection. Link 1Link 2
Using reflection is very resource intensive, violates almost all OOP principles and should be avoided unless really needed. It can get very nasty when you start accessing private data members/methods and violating the presentation invariants of the objects you're inspecting. Might i suggest using the instanceof operator to find out if your object is of a particular class and going from there?
精彩评论