我有一个类名Rocket
,它有2个方法名launch and land
(用于存储发射和着陆时发生撞机的可能性)和另外2个child class of Rocket U1 and U2
分别覆盖launch and land
。因此在Main中我编写了2种方法Simulation U1和模拟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;
}
}
我的spaceShip界面
interface SpaceShip {
boolean launch();
boolean land();
boolean canCarry(Item item,int totalCargo,int maxLimit);
int carry(Item item ,int currentWeight);
}
最佳答案
您可以将rocketList
的类型减弱为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");
}