课后作业
public class MethodOverload {
public static void main(String[] args) {
// 调用不同参数类型的square方法
System.out.println(square(7)); // 调用int参数的方法
System.out.println(square(7.5)); // 调用double参数的方法
}
// 计算整数的平方
public static int square(int x) {
return x * x;
}
// 计算双精度浮点数的平方(方法重载)
public static double square(double y) {
return y * y;
}
}
重载的 square 方法:
第一个 square 方法:接收 int 类型参数 x,返回 x * x 的整数结果。
第二个 square 方法:接收 double 类型参数 y,返回 y * y 的双精度浮点结果。
两个 square 方法方法名相同,但参数列表不同(一个参数是 int 类型,另一个是 double 类型)。
浙公网安备 33010602011771号