简体   繁体   中英

Best way to call same method from different classes in java

I have several classes with exactly the same methods

public class Car{
    int goBrrr();
}
public class Bus{
    int goBrrr();
}

etc.

I need to create ArrayList with objects of these classes, and use identical methods in loop

for (Vehicle vehicle: vehicleList{
    vehicle.goBrrr();
}

Classes cannot be changed.

Which solution is better: Create new class with the required fields and methods and fill it in the constructor

public final class Vehicle{
    int wheels;
    String engine;
    ...
    
    Vehicle (Car car) {...}
    Vehicle (Truck truck) {...}
    ...

    int goBrrr();
}

Create an interface

interface Vehicle{
    int goBrrr();
}

and extend classes

class ExtCar extends Car implement Vehicle{
    @Override
    int goBrrr();
}
class ExtBus extends Bus implement Vehicle{
    @Override
    int goBrrr();
} 

Or maybe there is better solution?

As mentioned in below code, you can create Vehicle as interface. And provide that common method, and for individual implementation of Vehicle you can provide your custom implementation of that method.

While creating list you just need to add Object of Car/Bus/Truck , automatically according to type it will call method of that class.

interface Vehicle{
    int goBrrr();
}
class Car implements Vehicle{

    @Override
    public int goBrrr() {
        System.out.println("Car");
        return 0;
    }
}
class Bus implements Vehicle{

    @Override
    public int goBrrr() {
        System.out.println("Bus");
        return 0;
    }
}
class Main{
    public static void main(String[] args) {
        List<Vehicle> vehicleList= new ArrayList<>();
        vehicleList.add(new Bus());
        vehicleList.add(new Car());
        for(Vehicle vehicle:vehicleList){
            vehicle.goBrrr();
        }
    }
}

This will print output as below

Bus
Car

I would recommend using the interface Vehicle to implement the goBrrr method on Bus and Car. In the below example I have returned a value for each goBrrr method and demonstrated how the list be will be used in the Main psvm method. The use of abstraction in the vehicle interface allows the List to remain open for extension to other classes (such as truck) and closed to modification (when new classes are added, the main class doesn't need to change)

class Main{

    public static void main(String[] args) {

        Vehicle vehicle = new Bus();
        List<Vehicle> vehicleList = new ArrayList<>();

        vehicleList.add(vehicle);
        vehicleList.add(vehicle);
        vehicleList.add(vehicle);

        for(Vehicle v : vehicleList){
            System.out.println(v.goBrrr());
        }

    }
}
interface Vehicle {
    int goBrrr();
}
final class Car implements Vehicle {

    public int goBrrr(){
        return 10;
    }
}
final class Bus implements Vehicle {
    public int goBrrr(){
        return 11;
    }
}

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