简体   繁体   中英

How do I pass the same object across multiple classes java?

I have an object class that I want to use across multiple classes. Basically, I want this object to be used as a place where I can dump information into, and then unpack that information.

Let's say for example I have 5 classes

Class1 will return a box object. --> Foo(object) will store the box

Class2 will return a snake object. --> Foo(object) will store the snake

Class3 will return a circle object. --> Foo(object) will store the circle.

Class4 will return aa food object. --> Foo(object) will store the food

Class5 will return a box, snake, circle, and food object. --> unpack Foo(object) to return the box, snake, circle, and food object.

How can I do this? If possible can you provide some sample example code for this kind of scene? Let me know if any more clarification is required.

You can do it :

  1. create a class that you will create your object as an instance of it.
  2. make this class extended from Serializable class (allowed you to freeze and in freeze, you object)
  3. use intent to navigate between your activities and add your object as an intent data.

Edit
That's work with activities!
SECOND way

What if you make your object invoke its data from class extended from Sharedpreferences like

Create an object from class A which its constructor uses Sharedpreferences class to fill object data

You can create an interface with a dump method and implement the interface with all the classes you intend to work with. If your interface is called Dumper , you will need to implement it in all the classes where you intend to use it, if possible. If not, for instance, class EvilGuy is out of your control, you can simply create an EvilGuyWrapper class , with a member of EvilGuy and implementing Dumper .

Something like a collector, right?

Create a class that will accumulate objects:

public class EventCollector {
    private List<Object> events = new ArrayList<>();
    public void add(Object e) {
        events.add(e);
    }

}

Pass EventCollector instance to your box, snake, food, circle objects:

public class Circle {
     private EventCollector ec;
     public setEventCollector(EventCollector ec) {
         this.ec = ec;
     }

     public void pack() {
         ec.add(this);
     }
}

Do the same for food, box, snake, etc... Call pack method on all of them.

All objects will be in the EventCollectior at the end.

When working in Java remember that the language leans heavily towards having information available at compile time .

What you want to do is essentially to determine this at runtime, and you can only do this by returning the object and casting it to the class you expect at that point.

In your case, you need to either have a common superclass of all the different types you want to store, an interface implemented by all the types or just use Object. You can store it in any collection of this, like ArrayList.

Then when you get the object back you need to use "instanceof" to find out what you are looking at, and then cast it to the appropriate type.

Note that this is tedious and error prone, and you may want to reconsider and learn generics.

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