如何将if-else块转换为switch case?(JAVA)
我一直在想办法,但我发现打开Controller.vehicle有问题,因为它是一个浮动值。我试着把它转换成字符串,但它不起作用,我担心它会失去一些精度如何将if-else块转换为switch case?(JAVA),java,switch-statement,Java,Switch Statement,我一直在想办法,但我发现打开Controller.vehicle有问题,因为它是一个浮动值。我试着把它转换成字符串,但它不起作用,我担心它会失去一些精度 if(Controller.vehicle instanceof Boat) { file = new File(System.getProperty("user.dir")+"/src/img/boat.png"); } else if(Controller.vehicle instanceof Shi
if(Controller.vehicle instanceof Boat) {
file = new File(System.getProperty("user.dir")+"/src/img/boat.png");
}
else if(Controller.vehicle instanceof Ship) {
file = new File(System.getProperty("user.dir")+"/src/img/ship.png");
}
else if(Controller.vehicle instanceof Truck) {
file = new File(System.getProperty("user.dir")+"/src/img/truck.png");
}
else if(Controller.vehicle instanceof Motorcycle) {
file = new File(System.getProperty("user.dir")+"/src/img/motorcycle.png");
}
else if(Controller.vehicle instanceof Bus) {
file = new File(System.getProperty("user.dir")+"/src/img/bus.png");
}
else if(Controller.vehicle instanceof Car) {
file = new File(System.getProperty("user.dir")+"/src/img/car.png");
}
else if(Controller.vehicle instanceof Bicycle) {
file = new File(System.getProperty("user.dir")+"/src/img/bicycle.png");
}
else if(Controller.vehicle instanceof Helicopter) {
file = new File(System.getProperty("user.dir")+"/src/img/helicopter.png");
}
else if(Controller.vehicle instanceof Airplane) {
file = new File(System.getProperty("user.dir")+"/src/img/airplane.png");
}
else if(Controller.vehicle instanceof Tram) {
file = new File(System.getProperty("user.dir")+"/src/img/tram.png");
}
else if(Controller.vehicle instanceof Train) {
file = new File(System.getProperty("user.dir")+"/src/img/train.png");
}
使用多实例操作通常意味着您的设计不好 无论vehicle类是什么,它都应该有一个抽象的getImageFile()方法,它的每个子类(Ship、Truck等)都应该重写该方法以获得正确的图像。因此,上面的方法只包含一行:
file = Controller.vehicle.getImageFile();
这将考虑到一些图像文件可能是.jpg,一些可能是.png,等等。使用多实例操作通常意味着设计不好 无论vehicle类是什么,它都应该有一个抽象的getImageFile()方法,它的每个子类(Ship、Truck等)都应该重写该方法以获得正确的图像。因此,上面的方法只包含一行:
file = Controller.vehicle.getImageFile();
这将允许一些图像文件可能是.jpg,一些可能是.png等。在这种特殊情况下,您可以尝试执行以下操作:
file = new File(System.getProperty("user.dir") + "/src/img/" +
Controller.vehicle.getClass().getSimpleName().toLowerCase() + ".png");
在这种情况下,您可以尝试执行以下操作:
file = new File(System.getProperty("user.dir") + "/src/img/" +
Controller.vehicle.getClass().getSimpleName().toLowerCase() + ".png");
不要打开课堂,
而是让类公开文件名
一些代码:
public interface ThingWithImage
{
public string getImageFileName();
}
public class Boat implements ThingWithImage
{
@Override
public string getImageFileName()
{
return "boat.png";
}
}
一点工作,
还可以配置每个类的图像文件名。
例如(使用弹簧):
不要打开课堂,
而是让类公开文件名
一些代码:
public interface ThingWithImage
{
public string getImageFileName();
}
public class Boat implements ThingWithImage
{
@Override
public string getImageFileName()
{
return "boat.png";
}
}
一点工作,
还可以配置每个类的图像文件名。
例如(使用弹簧):
我认为要获得最佳实践,你们应该阅读这篇文章。 正如Leo和Fred所写的,vehicle类(可能是抽象类或接口)应该有一个名为getName()的函数,并且每个植入都应该重写这个方法并返回自己的名称
abstract class Vehicle{
public abstract String getName();
}
class Airplane extends Vehicle{
public String getName(){ return "airplane";}
}
你应该这样使用它:
file = new File(System.getProperty("user.dir")+"/src/img/"+Controller.vehicle.getName()+".png");
我认为要获得最佳实践,你们应该阅读这篇文章。 正如Leo和Fred所写的,vehicle类(可能是抽象类或接口)应该有一个名为getName()的函数,并且每个植入都应该重写这个方法并返回自己的名称
abstract class Vehicle{
public abstract String getName();
}
class Airplane extends Vehicle{
public String getName(){ return "airplane";}
}
你应该这样使用它:
file = new File(System.getProperty("user.dir")+"/src/img/"+Controller.vehicle.getName()+".png");
您肯定需要使用多态性。您肯定需要使用多态性。如果具体类是在if/else中测试的类的子类,则这不起作用。如果具体类是在if/else中测试的类的子类,则这不起作用。