我是java编程的新手,虽然我知道基础知识,但我觉得我的代码很长且没必要。我想知道是否有办法将elkStarter()
和foxStarter()
方法合并到一个starter()
方法中,以便我可以减少代码。
//Opening question, askes user to pick either an elk or fox
public void systemStart(){
for(int i =0;i<=100;i++){
System.out.println("- - - - - - - - - - - - - - - - - -\n");
}
System.out.println("***STARTING NEW MOVEMENT MODEL***\n"+"-----------------------------------\n");
iteration = 0;
String animalType = JOptionPane.showInputDialog
("Would you like an elk or a fox?");
switch (animalType.toLowerCase()){
case "elk": elkStarter("elk_1");
break;
case "fox": Foxtarter("fox_1");
break;
default: JOptionPane.showMessageDialog (null, "You didn't enter elk or fox..Please try again.");
runAnotherModel();
break;
}
}
//systemStater goes to when "elk" is selected as the animal type.
public void elkStarter(){
Elk elk_1 = new Elk();
elk_1.namer("elk_1");
System.out.println("You have just created an Elk called " + elk_1.name + ".\n" +
"It has begun its life at (0,-0).");
elk_1.findPosition();
JOptionPane.showMessageDialog (null, "Lets let our new elk roam a bit, shall we?");
int loop = 0;
while(loop==0){
String animalDirection = JOptionPane.showInputDialog
("Which way should our elk move? (forward(f), backward(b), up(u), down(d)) or type stop(s) to quit.");
switch(animalDirection.toLowerCase()){
case "forward": case "f":
elk_1.walkForward();
elk_1.findPosition();
break;
case "backward": case "b":
elk_1.walkBackward();
elk_1.findPosition();
break;
case "up": case "u":
elk_1.walkUp();
elk_1.findPosition();
break;
case "down": case "d":
elk_1.walkDown();
elk_1.findPosition();
break;
case "stop": case "s":
JOptionPane.showMessageDialog (null, "GoodBye! Thanks for trying my model!");
loop=1;
break;
default: JOptionPane.showMessageDialog (null, "ummmm sorry that wasn't a direction...Please try again.");
break;
}
}
}
//systemStater goes to when "fox" is selected as the animal type.
public void foxStarter(){
Fox fox_1 = new Fox();
fox_1.namer("fox_1");
System.out.println("You have just created an Fox called " + fox_1.name + ".\n" +
"It has begun its life at (0,-0).");
fox_1.findPosition();
JOptionPane.showMessageDialog (null, "Lets let our new fox roam a bit, shall we?");
int loop = 0;
while(loop==0){
String animalDirection = JOptionPane.showInputDialog
("Which way should our fox move? (forward(f), backward(b), up(u), down(d)) or type stop(s) to quit.");
switch(animalDirection.toLowerCase()){
case "forward": case "f":
fox_1.walkForward();
fox_1.findPosition();
break;
case "backward": case "b":
fox_1.walkBackward();
fox_1.findPosition();
break;
case "up": case "u":
fox_1.walkUp();
fox_1.findPosition();
break;
case "down": case "d":
fox_1.walkDown();
fox_1.findPosition();
break;
case "stop": case "s":
JOptionPane.showMessageDialog (null, "GoodBye! Thanks for trying my model!");
loop=1;
break;
default: JOptionPane.showMessageDialog (null, "ummmm sorry that wasn't a direction...Please try again.");
break;
}
}
}
答案 0 :(得分:1)
如果类Elk
和Fox
具有相同的父类,例如 - Animal
,那么我相信有多种方法可以使两个启动函数成为一个。< / p>
您可以在要使用的对象中调用start函数的调用者,例如 -
public void commonStarter(Animal animal) {
... // Do your logic on animal.
}
或者您可以向starter
函数发送一个参数,该参数将决定是创建Elk
还是Fox
,然后根据该参数创建正确的参数你的班级 -
public void commonStarter(boolean isElk) {
Animal animal = None;
if(isElk) {
animal = new Elk();
} else {
animal = new Fox();
}
... // Do your logic.
}
答案 1 :(得分:0)
最好的办法是创建父类/接口,然后使用布尔值调用该函数,该布尔值定义对象是Lion还是Elk。这称为类层次结构。
所以你只需要替换代码的一部分。
Animal somename;
if (isElk)
somename = new Elk();
else
somename = new Fox();
其中isElk是一个布尔值,如果用户想要创建一个新的Elk对象,则为true;如果用户想要创建一个新的Fox对象,则为false。
答案 2 :(得分:0)
您可以根据用户输入创建一个界面并调用所需的实现。
类似的东西:
public interface Starter {
void start();
}
public class ElkStarter implements Starter {
private String name;
// + constructor, getters, setters
@Override
public void start() {
// ...
}
}
// ...
public Starter getStarter(String animalType) {
switch (animalType.toLowerCase()) {
case "elk": return new ElkStart("elk_1");
case "fox": return new FoxStart("fox_1");
default: return null;
}
}
public void systemStart() {
// ...
Starter starter = getStarter(animalType);
// at this point, you do not know what Starter actually is
if(starter != null) {
starter.start();
} else {
// ...
}
}