如何理解本书的摘录,#34; Think Java"?

时间:2016-05-26 23:40:33

标签: 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 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);
  }

}

3 个答案:

答案 0 :(得分:1)

您调用方法Time3并将一组字符串传递给它。但是该方法的签名只接受Time3对象,因此编译错误。

但是:Time3类有一个接受字符串的构造函数。创建一个新的String pls = printABCS(new Time3("A", "B", "c", "D", "E", "F,", "G")); 实例并将参数传递给它

{{1}}

如果它是一本你正在读的好书,它应该包含概念" Constructors"和#34; Signature(方法)",查找它们。

答案 1 :(得分:0)

希望这会对你有所帮助。

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

  }

答案 2 :(得分:0)

我认为你正在走上正轨,你只是错过了那里的小事。您需要创建类Time3的实例并将该实例放入printABC()方法中,因为它只接受实例。 loook

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