简体   繁体   中英

getting objects from ArrayList<object>

I have 3 classes: one that is abstract named MyClass1 and two others that extend MyClass1 named MyClass2 and MyClass3 .

I create an ArrayList which is MyClass1 type ex :

ArrayList<MyClass1> myArray=new ArrayList<MyClass1>(); 

Then I create some objects in class MyClass2 and MyClass3 and add them in the ArrayList that I created previously. The question is how can I use the objects that I added in the ArrayList in order to call methods

As suggested, don't name your classes Object.

You will have to cast the objects in order to call methods declared in subclasses:

abstract class FirstClass {

    public void firstMethod() {

    }
}

class SecondClass extends FirstClass {

    public void secondMethod() {

    }
}

class ThirdClass extends FirstClass {

    public void thirdMethod() {

    }
}

....

public void run() {
    ArrayList<FirstClass> list = new ArrayList<>();
    list.add(new SecondClass());
    list.add(new ThirdClass());

    for (FirstClass obj : list) {
        obj.firstMethod(); //No cast needed

        if (obj instanceof SecondClass) {
            SecondClass objOfSecond = (SecondClass) obj;
            objOfSecond();
        }

        if (obj instanceof ThirdClass) {
            ThirdClass objOfThird = (ThirdClass) obj;
            objOfThird();
        }
    }
}

Option 1: If your Base class ( Object1 ) got the Method, you can call it on every Child Class ( Object2 / Obejct3 )

Option 2: If you want to call a method on your Child Class which is not defined in your base class, you need to cast your Object to the Specific class in order to call the method.

Example:

for (Object1 myObject1 : myArray){
  if (myObject1 instanceof Object2){
    Object2 myObject2 = (Object2)myObject1;
    myObject2.myMethod();
  }
}

And yes, dont name your class Object since this name is already used by Java as "Base/Root Object".

The question was a bit difficult to understand, given the use of Object as a keyword in Java, an word with specific meaning when talking about object-oriented languages, and now a specific class, so I'll rename the class we're talking about to SomeObject .

When your ArrayList of SomeObject objects is declared, it will look like this:

List<SomeObject> myArray = new ArrayList<>();

You then add instances of SomeObject:

myArray.add(new SomeObject());

And then you can access objects by index, and call methods directly on that object. Here, I call the someMethod method of the SomeObject class.

int i = 0; //used as index
myArray.get(i).someMethod();

Keep in mind that if SomeObject has subclasses (such as SomeOtherObject ), you are definitely allowed to add instances of SomeOtherObject to your SomeObject ArrayList. But, you will run into issues if you have added multiple subclasses to the ArrayList, and then try to call a method that is unique to a subclass.

Hope this helps!

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