首页 > 解决方案 > 在Java中如何使用接受其父类的单个函数处理所有子类

问题描述

我有一个类名Rocket,它有 2 个方法名launch and land(存储在发射和着陆时崩溃的概率)和 2 个child class of Rocket U1 and U2分别覆盖的方法名。所以launch and land在 Main 我写了 2 个方法 Simulation U1 和 Simulation U2 分别接受 U1 和 U2 的对象和计算任务是否成功(并计算其他东西,如成本等,但完全相同)。我的两种方法 Simulation U1 和 Simulation U2 做的事情完全相同,但唯一的区别是第一个接受 U1 对象,第二个接受 U2 对象。那么是否有任何方法通过我编写一个接受 Rocket 对象的方法,并且根据传递的对象可以访问该类的相应方法。

void runSimulationU1(ArrayList<U1> rocketList) {
totalCost=0;
    U1 rocketU1;
    for (int i = 0; i < rocketList.size(); i++) {
        rocketU1 = rocketList.get(i);
        boolean x = true;
        while (x) {
            if ((rocketU1.launch() && (rocketU1.land()))) {
                totalCost += rocketU1.cost;
                x = false;
            } else {
                totalCost += rocketU1.cost;
            }
        }

    }
    System.out.println("Total cost:"+totalCost+" Million");
}
void runSimulationU2(ArrayList<U2> rocketList) {
totalCost=0;
    U2 rocketU2;
    for (int i = 0; i < rocketList.size(); i++) {
        rocketU2 = rocketList.get(i);
        boolean x = true;
        while (x) {
            if ((rocketU2.launch() && (rocketU2.land()))) {
                totalCost += rocketU2.cost;
                x = false;
            } else {
                totalCost += rocketU2.cost;
            }
        }

    }

    System.out.println("Total cost:"+totalCost+" Million");

}

正如你所看到的,这段代码是重复的,所以有什么办法可以让这段代码更完美。

我的火箭课

public abstract class Rocket implements SpaceShip {
    int cost=0;
    @Override
    public abstract boolean launch();

    @Override
    public  abstract boolean land() ;
    @Override
    public boolean canCarry(Item item,int totalCargo,int maxLimit) {
        if(totalCargo+item.weight<=maxLimit){
            return true;
        }
        else {
            return false;
        }
    }

    @Override
    public int carry(Item item,int currentWeight) {
        currentWeight = currentWeight+item.weight;
        return currentWeight;
    }
}

我的U2班

public class U2 extends Rocket {
 final int cost = 120;
 final int maxLimit =29_000;
 public int totalCargo=18_000;
 U2(int totalCargo){
     this.totalCargo=totalCargo;
 }
public boolean land(){
    double probability = (8/100)*(totalCargo/maxLimit);
    double randomValue =Math.random();
    return randomValue>=probability;
}
public boolean launch(){
    double probability = (4/100)*(totalCargo/maxLimit);
    double randomValue =Math.random();
    return randomValue>=probability;
}

} 我的 U1 课

public class U1 extends Rocket {
final int cost = 100;
final int maxLimit =18_000;
public  int totalCargo=10_000;
U1(int totalCargo){
    this.totalCargo=totalCargo;
}
public boolean land(){
    double probability = (1/100)*(totalCargo/maxLimit);
    double randomValue =Math.random();
    return randomValue>=probability;
}
public boolean launch(){
    double probability = (5/100)*(totalCargo/maxLimit);
    double randomValue =Math.random();
    return randomValue>=probability;
}

} 我的飞船界面

 interface SpaceShip {
boolean launch();
boolean land();
boolean canCarry(Item item,int totalCargo,int maxLimit);
int carry(Item item ,int currentWeight);
}

标签: javaclassoopinheritancearraylist

解决方案


你可以弱化rocketListto的类型Iterable<? extends >。它将允许接受任何类型的对象,runSimulation这是Iterable<? extends Rocket>(例如,,,,, )的子类型。ArrayList<U1>ArrayList<U2>HashSet<Rocket>Collection<U1>Collection<? extends U2>

void runSimulation(Iterable<? extends Rocket> rockets) {
    int totalCost = 0;
    for (Rocket rocket : rockets)
        do {
            totalCost += rocket.getCost();
        } while (!(rocket.launch() && rocket.land()));
    System.out.println("Total cost:" + totalCost + " Million");
}

推荐阅读