"Think Java",如何理解书中的这段摘录?



问题出现在行中

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);
    }
}

最新更新