سؤال

For example I have:

    public class Class1 implements Serializable {
       private static final long serialVersionUID = 1L;
       private int a;
       private int b;

       /*Getters and setters here*/
       public int getA();
    }

    public class Class2 implements Serializable {
       private static final long serialVersionUID = 1L;
       private int a;
       private int b;

       /*Getters and setters here*/
       public int getA();
    }

Now I want to make an ArrayList of those two types. This is what I'm trying:

List<Serializable> list= new ArrayList<Serializable>();

I have a method that returns Serializable, which returns the object in the list:

  public Serializable get(int i)
{
    return list.get(i);
}

However, when I'm trying to use the getter method from the two classes above( something like list.get(0).getA()) , there's an error saying the getA() method is not defined for Serializable. Am I missing something simple here? What should I use for the return type of the method above in order to use the getter method? Thanks.

هل كانت مفيدة؟

المحلول 2

You are returning a Serializable object, not a Class1 object. There are two routes to doing this, but I recommend the latter:

Serializable s = list.get(1);
if (s instanceof Class1) {
    Class1 clazz = (Class1) s;
    clazz.getA();
} //etc...

Or, using a common interface:

public interface YourClass extends Serializable {

    public int getA();

}

//...

List<YourClass> list = new ArrayList<>();

نصائح أخرى

Both those classes are Serializable but Serializable doesn't have any methods defined.

You will need to define either a base class or an interface yourself that contains the getA() method and then you can reference them using that.

Remember that while in this case the only Serializable classes you have are Class1 and Class2 but in most cases you cannot guarantee that. There could be another Class3 that implements Serializable but does not have the method getA.

This approach uses a interface that have a method named getA(). If both your classes implements this interface you will be able to call that method from a List of the interface type.

 public interface MyInterface {
      public void getA();
    }

 public class Class1 implements MyInterface, Serializable {
       private static final long serialVersionUID = 1L;
       private int a;
       private int b;

       /*Getters and setters here*/
       public int getA();
    }

    public class Class2 implements MyInterface, Serializable  {
       private static final long serialVersionUID = 1L;
       private int a;
       private int b;

       /*Getters and setters here*/
       public int getA();
    }

    List<MyInterface> list= new ArrayList<MyInterface>();

This way you can do list.get(0).getA();

Serializable interface in Java doesn't have any methods it's just a marker interface. If you want to have a common method you should create another interface with getA() and than implement this interface in your both classes.

You should create a interface containing the method getA(). Your list should have your interface as generic type.

public interface MyInterface
{
  public int getA();
}

public class Class1 implements MyInterface
{
  public int getA()
  {
    // Your implementation of getA()
    return 0;
  }
}

Now create somewhere in your code 2 objects of type Class1 and put the object in a List of generic type MyInterface:

List<MyInterface> myList = new ArrayList<MyInterface>();

Class1 obj1 = new Class1();
Class1 obj2 = new Class1();

myList.add(obj1);
myList.add(obj2);

Now you can iterate over your list and get objects of type MyInterface (instead of Serializable) so you can call method getA on the objects.

Create an interface that declares the methods common to all of your classes.

interface A extends Serializable {
   public int getA();
}

public class Class1 implements A {
   private static final long serialVersionUID = 1L;
   private int a;
   private int b;

   /*Getters and setters here*/
   public int getA();
}

public class Class2 implements A {
   private static final long serialVersionUID = 1L;
   private int a;
   private int b;

   /*Getters and setters here*/
   public int getA();
}

Then declare a List of A's:

List<A> mylist = new ArrayList<A>();

you can do your method if you want (but its not adding much):

public Serializable get(int i) {
   return mylist.get(i);
}

mylist is declared to have A objects which all implement the getA() method.

 Try this...

 List<Class1> list= new ArrayList<Class1>();
    Class1 c = new Class1();
    c.setA(2);
    c.setB(4);
    list.add(c);
    System.out.println(list.get(0).getA());

This will print 2

and

List<Serializable> list2= new ArrayList<Serializable>();
list2.add(c);
System.out.println(((Class1)list2.get(0)).getA());

This will also print 2

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top