public void UpdateScaleByCalcaulateType(ShipModelCalculateType calculateType = ShipModelCalculateType.Length, double length = 0, double width = 0, double height = 0) { if (Ship3DModelInfo == null || (length == 0 && width == 0 && height == 0) || calculateType == ShipModelCalculateType.None) { return; } float scale_x = 1; float scale_y = 1; float scale_z = 1; if (length > 0 && Ship3DModelInfo.Ship3DModelLength > 0) { scale_z = (float)(length / Ship3DModelInfo.Ship3DModelLength); } if (width > 0 && Ship3DModelInfo.Ship3DModelWidth > 0) { scale_x = (float)(width / Ship3DModelInfo.Ship3DModelWidth); } if (height > 0 && Ship3DModelInfo.Ship3DModelHeight > 0) { scale_y = (float)(height / Ship3DModelInfo.Ship3DModelHeight); } if (calculateType == ShipModelCalculateType.Length) { transform.localScale = new Vector3(scale_z, scale_z, scale_z); } else if (calculateType == ShipModelCalculateType.ALL) { transform.localScale = new Vector3(scale_x, scale_y, scale_z); } }与public void UpdateScaleByCalcaulateType(ShipModelCalculateType calculateType = ShipModelCalculateType.Length, double length = 0, double width = 0, double height = 0) { if (Ship3DModelInfo == null || (length == 0 && width == 0 && height == 0) || calculateType == ShipModelCalculateType.None) { return; } bool isUpdate = false; float scale_x = 1; float scale_y = 1; float scale_z = 1; switch (calculateType) { case ShipModelCalculateType.Length: { if (length > 0 && Ship3DModelInfo.Ship3DModelLength > 0) { isUpdate = true; scale_z = (float)(length / Ship3DModelInfo.Ship3DModelLength); } } break; case ShipModelCalculateType.Width: { if (width > 0 && Ship3DModelInfo.Ship3DModelWidth > 0) { isUpdate = true; scale_x = (float)(width / Ship3DModelInfo.Ship3DModelWidth); } } break; case ShipModelCalculateType.Height: { if (height > 0 && Ship3DModelInfo.Ship3DModelHeight > 0) { isUpdate = true; scale_y = (float)(height / Ship3DModelInfo.Ship3DModelHeight); } } break; case ShipModelCalculateType.ALL: case ShipModelCalculateType.None: default: break; } if (isUpdate) { transform.localScale = new Vector3(scale_x, scale_y, scale_z); } }有什么不一样的地方
时间: 2024-05-02 19:18:43 浏览: 10
这两个方法的功能是一样的,都是根据传入的参数来更新物体的缩放比例。但是代码实现上有一些不同:
1. 第一个方法使用了 if-else 的判断语句,而第二个方法使用了 switch-case 的结构。
2. 第二个方法在 switch-case 中对每一种情况都单独进行了判断,而第一个方法则是在每一个 if 中进行判断。
3. 第二个方法使用了一个 bool 变量 isUpdate 来记录是否需要更新缩放比例,而第一个方法则没有。
总的来说,第二个方法的代码更加清晰和简洁,也更加易于扩展和维护。
相关问题
package b; public class yx { public static void main(String[] args) { Cuboid v = new Cuboid(); v.volume(); } } class Rectangle{ double length; double width; } class Cuboid extends Rectangle{ double height; public Cuboid(double length,double width,double height) { this.length=length; this.width=width; this.height=height; } void volume() { length=10; width=5; height=2; System.out.println("体积为:" +length*width*height); } }
你的代码中存在一些问题:
1. `yx` 类和 `Cuboid` 类不在同一个包中,需要将 `Cuboid` 类声明为 `public` 类型,或将两个类放在同一个包中。
2. 在 `yx` 类的 `main` 方法中,创建了一个没有参数的 `Cuboid` 对象 `v`,但是你的 `Cuboid` 类中只定义了一个有参数的构造方法,因此需要在创建对象时传入三个参数。
3. `volume` 方法中的计算公式有误,应该是长乘以宽乘以高。
下面是修改后的示例代码:
```java
package b;
public class yx {
public static void main(String[] args) {
Cuboid v = new Cuboid(10, 5, 2);
v.volume();
}
}
class Rectangle {
double length;
double width;
}
public class Cuboid extends Rectangle {
double height;
public Cuboid(double length, double width, double height) {
this.length = length;
this.width = width;
this.height = height;
}
void volume() {
System.out.println("体积为:" + length * width * height);
}
}
```
希望能够帮助到你。
定义一个构造方法Cuboid(double length double width double height)
好的,以下是一个定义了三个参数的构造方法 `Cuboid(double length, double width, double height)` 的示例代码:
```java
public class Cuboid {
private double length;
private double width;
private double height;
public Cuboid(double length, double width, double height) {
this.length = length;
this.width = width;
this.height = height;
}
// 其他方法...
}
```
这个构造方法接收三个参数,分别是 `length`、`width` 和 `height`,并且将它们分别赋值给类中的三个成员变量。其中 `this` 表示当前对象,用于区分成员变量和方法参数的同名情况。
你可以根据实际需求修改参数类型和变量名。希望能对你有所帮助。