java 编写一个类,定义3个静态重载方法,分别用于求两整效的和、两浮点数的和以及两字符串的连接。并编写该类的测试程序。
时间: 2024-05-08 12:16:06 浏览: 56
Java 实例 - 方法重载演示源代码-详细教程.zip
```java
public class Calculator {
public static int add(int a, int b) {
return a + b;
}
public static double add(double a, double b) {
return a + b;
}
public static String add(String a, String b) {
return a + b;
}
public static void main(String[] args) {
int result1 = Calculator.add(3, 4);
double result2 = Calculator.add(3.5, 4.5);
String result3 = Calculator.add("Hello", "World");
System.out.println("result1: " + result1);
System.out.println("result2: " + result2);
System.out.println("result3: " + result3);
}
}
```
输出结果:
```
result1: 7
result2: 8.0
result3: HelloWorld
```
阅读全文