class Computer{ void compute(int a,int b){ intc=a+b; System.out.println(a+"+"+b+"="+c); } void compute(double a,double b){ doublec=a+b; System.out.println(a+"+"+b+"="+c); } } publicclass Excise7_3{ publicstatic void main(String arg[]){ Computerc=new Computer(); c.compute(10,20); c.compute(10.5,20.5); } } 问题: (1) 请解释“重载”的概念。 (2) 假设在main方法中增加一条语句c.compute(“java”,”program”);后仍能正确执行,请完善上面的程序。
时间: 2024-02-14 07:15:09 浏览: 12
(1) 重载是指在同一个类中,可以定义多个同名的方法,但这些方法的参数个数、类型或顺序不同,编译器根据具体的参数类型和数量来决定调用哪个方法。
(2) 需要在Computer类中增加一个新的方法:
```
void compute(String a, String b){
String c = a + b;
System.out.println(a + "+" + b + "=" + c);
}
```
这个方法接收两个字符串类型的参数,将它们拼接起来,并输出结果。这样,程序就可以正确执行c.compute("java", "program")这条语句了。
相关问题
package transpotion; import java.lang.reflect.InvocationTargetException; public class ComputeTime { public static void main(String[] args) { System.out.println("Mode of transportation is:"+args[0]); System.out.println("A="+args[1]); System.out.println("B="+args[2]); System.out.println("C="+args[3]); double a=Double.parseDouble(args[1]); double b=Double.parseDouble(args[2]); double c=Double.parseDouble(args[3]); double v,t; try { Common d=(Common) Class.forName("transpotion." +args[0]).getDeclaredConstructor().newInstance(); v=d.speed(a,b,c); t=1000/v; System.out.println("速度=:"+v+"km/h"); System.out.println("时间=:"+(float)t+"h"); } catch(ClassNotFoundException| InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { System.out.println("class not found"); } } } package transpotion; public interface Common { double speed(double a,double b,double c); } package transpotion; public class Car007 implements Common{ public double speed(double a, double b, double c) { if(c==0) { System.out.println("Divisor cannot be 0"); return 0; } else return ab/c; } } package transpotion; public class Plane implements Common { public double speed(double a, double b, double c) { return a+b+c; } } package transpotion; public class Ship implements Common { public double speed(double a,double b,double c) { return ab+a*c; } }可以为我讲解一下这段代码中的String tools = args[0]; int a = Integer.parseInt(args[1]); int b = Integer.parseInt(args[2]); int c = Integer.parseInt(args[3]); Common d = (Common) Class.forName("LAB2." + tools).newInstance(); float time = d.time(a, b, c); System.out.println(time);这部分吗,最好详细点
好的,这段代码的作用是根据不同的交通工具计算从 A 到 B 的时间。具体来说,它根据命令行参数传递的交通工具类型(`args[0]`)、出发地点 A(`args[1]`)、目的地 B(`args[2]`)和速度或航行时间 C(`args[3]`),计算从 A 到 B 所需的时间,并输出结果。
下面是代码的具体实现:
```java
String tools = args[0]; // 获取交通工具类型
int a = Integer.parseInt(args[1]); // 获取出发地点 A
int b = Integer.parseInt(args[2]); // 获取目的地 B
int c = Integer.parseInt(args[3]); // 获取速度或航行时间 C
Common d = (Common) Class.forName("transpotion." + tools).newInstance(); // 通过反射机制创建交通工具对象
double time = d.speed(a, b, c); // 调用交通工具对象的 speed() 方法计算时间
System.out.println(time); // 输出时间
```
首先,我们从命令行参数中获取交通工具类型、出发地点 A、目的地 B 和速度或航行时间 C,分别保存在 `tools`、`a`、`b` 和 `c` 变量中。
然后,我们使用反射机制创建交通工具对象。具体来说,我们通过 `Class.forName()` 方法获取交通工具类的 Class 对象,然后调用它的 `newInstance()` 方法来创建一个新的交通工具对象。这里我们使用了强制类型转换将对象转换成 `Common` 接口类型,以便调用 `speed()` 方法。
最后,我们调用交通工具对象的 `speed()` 方法,传递出发地点 A、目的地 B 和速度或航行时间 C 作为参数,计算从 A 到 B 所需的时间,并将结果保存在 `time` 变量中。
需要注意的是,这里的 `Common` 接口定义了一个 `speed()` 方法,用于计算从 A 到 B 所需的时间。具体的实现是在各个交通工具类中完成的。因此,我们可以通过调用 `Common` 接口的 `speed()` 方法,来得到不同交通工具的时间计算结果。
用C语言函数的方式求s=a+aa+aaa+aaaa+aa...a的值代码
```c
#include <stdio.h>
int compute(int a, int n) {
int sum = 0, temp = a;
for (int i = 1; i <= n; i++) {
sum += temp;
temp = temp * 10 + a;
}
return sum;
}
int main() {
int a, n;
printf("Input a and n:\n");
scanf("%d %d", &a, &n);
printf("s = %d\n", compute(a, n));
return 0;
}
```
运行结果:
```
Input a and n:
2 5
s = 24690
```