如何理解《思考Java》一书中的这段摘录?
问题出现在生产线上如何理解《思考Java》一书中的这段摘录?,java,object,Java,Object,问题出现在生产线上 String pls = printABCS("A", "B", "c", "D", "E", "F,", "G");" 我也不知道为什么,我已经试了一个小时了,但似乎什么都没用。为什么我运行代码时,结果是 "Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method printABCS(Time3) in the type Time3 is not appl
String pls = printABCS("A", "B", "c", "D", "E", "F,", "G");"
我也不知道为什么,我已经试了一个小时了,但似乎什么都没用。为什么我运行代码时,结果是
"Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method printABCS(Time3) in the type Time3 is not applicable for the arguments (String, String, String, String, String, String, String)
at chapter11.Time3.main(Time3.java:16)"
谢谢你花时间帮忙
public class Time3 {
String a, b, c, d, e, f, g;
public Time3(String a, String b, String c, String d, String e, String f, String g) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
this.f = f;
this.g = g;
}
public static void main(String[] args) {
String pls = printABCS("A", "B", "c", "D", "E", "F,", "G");
}
public static String printABCS(Time3 p) {
return (p.a + p.b + p.c + p.d + p.e + p.f + p.g);
}
}
调用printABCS方法并向其传递一组字符串。但是该方法的签名只接受Time3对象,因此编译错误
但是:Time3类有一个接受字符串的构造函数。创建一个新的Time3实例并将参数传递给它
String pls = printABCS(new Time3("A", "B", "c", "D", "E", "F,", "G"));
如果您正在阅读一本好书,它应该包含概念构造函数和方法签名,请查找它们。希望这对您有所帮助
public class Time3 {
String a, b, c, d, e, f, g;
public Time3(String a, String b, String c, String d, String e, String f, String g) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
this.f = f;
this.g = g;
}
public static void main(String[] args) {
Time3 t = new Time3("A", "B", "c", "D", "E", "F,", "G");
String pls = printABCS(t);
System.out.println(pls);
}
public static String printABCS(Time3 p) {
return (p.a + p.b + p.c + p.d + p.e + p.f + p.g);
}
}
我想你已经走上正轨了,你只是错过了一些小东西。您需要创建类Time3的实例,并将该实例放入printABC方法中,因为它只接受一个实例。鲁克
public class Time3 {
private String a, b, c, d, e, f, g;
public Time3(String a,String b, String c, String d, String e, String f, String g){
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
this.f = f;
this.g = g;
}
public static String printABC(Time3 p){
return p.a + p.b + p.c + p.d + p.e + p.f + p.g;
}
public static void main(String[] args) {
Time3 time1 = new Time3("A", "B", "C", "D", "E", "F", "G");
String pls = printABC(time1);
System.out.println(pls);
}
}
该方法需要一个Time3。你在传递一堆线。因此错误不适用于参数字符串,…。正如@AndyThomas所说,您试图将错误的对象传递给方法。printABCS方法声明接受一个名为Time3的对象作为输入。你传递了7条线。它不知道如何处理它,因此产生了编译问题。如果可能的话,请考虑构造Time3对象,否则更改方法签名以使用STRIGNS,谢谢您的帮助!我现在明白多了!非常感谢你。