开发者

Copy of the internal data structure of a class

开发者 https://www.devze.com 2022-12-14 15:17 出处:网络
Out of curiosity, I\'d li开发者_运维知识库ke to know if any class exists in Java with a method that returns a copy of its data structure. I ask because in class the teacher said a method like this bre

Out of curiosity, I'd li开发者_运维知识库ke to know if any class exists in Java with a method that returns a copy of its data structure. I ask because in class the teacher said a method like this breaks privacy, but I think that getting a copy of the structure is useful if you want to rearrange the structure. I'd like an example. Thanks.


I'm not entirely sure what you mean by the "data structure" of a class, but assuming you mean the members it contains, what you're looking for is reflection.

Try this tutorial.


Maybe you are missing the point: If you build a class which encapsulates some kind of internal data then you should NOT add a method which returns the internal data structure, but only the data that is encapsulated.

(Which is kind of the idea of encapsulation)

There should not be any need to "rearrange" your internal representation from the outside - because it is supposed to be internal and thus transparent in its use. (Meaning: You should not even be able to say what kind of data structure is used)


If you serialize it, any object (that is serializable) will happily prints its internal structure to a binary stream. And yes, this breaks encapsulation.

And yes, no one stops you from going to change the binary output and read it in again to create an altered object.

 

NB: there are more strange issues regarding serialization. For example, when deserializing objects new objects are created without their constructor ever being called. Et cetera. Serialization of objects is the maybe least object-oriented thing one can do with objects.


You're mixing up some concepts here.

Classes really are "data structures + methods", so in general you'd need a class to represent your class. Hey, a nice custom-built one would be the class your data is already in. You may be thinking of a class as a collection of simple fields, but this is not always the case.

As others have mentioned, you can extract the data via reflection, e.g.

public Map<String,Object> fields() {
  Map output=new hashMap<String,Object>(); 
  for (Field f:getClass().getFields())
   {
        try{
           output.put(f.getName(), f.get(this));
         }
         catch(... //IllegalArgument, IllegalAccess.. {... }
   }
  return output;
}

You can get into encapsulation issues here, in general the class should provide the data that you need to see from it, I tend to use things like this only for debugging.

I'm not sure what you mean by "rearrange the structure". The class generally represents the structure of the data. If there's a transformation you want to accomplish, it probably belongs in a class method, e.g. are you thinking of something like a co-ordinates class that can provide a transformed version of itself into polar co-ordinates?


A simple way to see the internal representation of an object is to serialise it using XStream. This will generate an XML representation of the class and its components (and so on).

Does this break encapsulation ? Yes - in the sense that you're able to inspect the internal structure of the class. You can take that XML, change it, and provided that it matches the .class structure that it came from, deserialise it back into a .class instance.

0

精彩评论

暂无评论...
验证码 换一张
取 消