我目前正在编写一个非常简单的脚本,我收到的错误在提供的示例中没有弹出。我有一组代码和一个客户端来测试它。我尝试运行代码时收到此错误。我得到了三个实例,我尝试在客户端使用“新类”。
ClassClient.java:5: error: constructor Class in class Class cannot be applied to given types;
Class code = new Class("1234");
^
required: String,String,String
found: String
reason: actual and formal argument lists differ in length
这是第一组代码:
public class Class
{
//attributes
public String code;
private String name;
private String credit;
//constructor
public Class(String newCode, String newName, String newCredit)
{
setCode(newCode);
setName(newName);
setCredit(newCredit);
}
//get|sets
public String getCode()
{
return code;
}
public void setCode(String newCode)
{
code = newCode;
}
public String getName()
{
return name;
}
public void setName(String newName)
{
name = newName;
}
public String getCredit()
{
return credit;
}
public void setCredit(String newCredit)
{
credit = newCredit;
}
public String toString()
{
return ("The code for this class is: "+code + "\n" + "The name for this class is :"+name + "\n" + "The number of credits this course has is :"+credit);
}
public boolean equals(Object o)
{
if(!(o instanceof Class))
return false;
else
{
Class x = (Class) o;
Class y = (Class) o;
Class z = (Class) o;
return(code.equals(x.code) || name.equals(y.name) || credit.equals(z.credit));
}
}//endelse
}//end class
以下是测试代码的客户端:
public class ClassClient
{
public static void main(String [] args)
{
Class code = new Class("1234");
Class name = new Class("Java");
Class credit = new Class("4");
System.out.println("The code for this class is: "+code + "\n" + "The name for this class is :"+name + "\n" + "The number of credits this course has is :"+credit);
}
}
答案 0 :(得分:1)
Constructor
中{p> class
接受3
String
个参数而不是1
。
您可以更改
Class code = new Class("1234"); // you are calling single argument
Class name = new Class("Java"); // but there is no matching
Class credit = new Class("4"); // constructor for this
要
Class cl = new Class("1234","Java","4");
除了获取值,您还应该使用getter。
您可以更改
System.out.println("The code for this class is: "+code + "\n"
+ "The name for this class is :"+name + "\n"
+ "The number of credits this course has is :"+credit);
要
System.out.println("The code for this class is: "+cl.getCode() + "\n"
+ "The name for this class is :"+cl.getName() + "\n"
+ "The number of credits this course has is "+cl.getCredit());
答案 1 :(得分:0)
如果你的Class类中有一个构造函数,你的ClassClient是正确的。
//constructor
public Class(String somethig)
{
// set somethig
}
但在你的构造函数中它期望三个参数。但你只提供了一个。