我创建了两个类应用程序,类是1. InVoice 2. InVoiceTest,我将InVoice类导入InVoiceTest,这里是InVoice类
public class InVoice
{
private String name;
private String description;
private int quantity;
private double price;
public InVoice (String n, String d, int q, double p)
{
name=n;
description=d;
quantity=q;
price=p;
}
public void set (String n, String d, int q, double p)
{
name=n;
description=d;
quantity=q;
price=p;
}
public String getname()
{
return name;
}
public String getdescription()
{
return description;
}
public int getquantity()
{
return quantity;
}
public double getprice()
{
return price;
}
}
这是InVoiceTest类
import java.util.Scanner;
public class InVoiceTest
{
public static void main (String [] aa)
{
InVoice object=new InVoice();
Scanner obj=new Scanner (System.in);
System.out.print("Enter Item name: ");
String name=obj.nextLine();
System.out.print("\nEnter Item description: ");
String description=obj.nextLine();
System.out.print("\nEnter quantity: ");
int quantity=obj.nextInt();
System.out.print("\nEnter price: ");
double price=obj.nextDouble();
object.set(name,description, quantity, price);
}
}
这两个类都在同一目录中,我通过命令提示符编译它们,并且这个错误一次又一次地显示
InVoiceTest.class can not find symbol
symbol: constructor InVoice()
location: class InVoice
InVoice object=new InVoice();
答案 0 :(得分:1)
如果您没有构造函数,Java编译器将为您创建一个无参数构造函数。
只要提供具有参数的构造函数,编译器就不会创建无参数构造函数。
因此,如果向InVoice添加无参数构造函数(如下所示),它应该可以工作。
public InVoice() {
}
答案 1 :(得分:0)
另一种方式是:
import java.util.Scanner;
public class InVoiceTest
{
public static void main (String [] aa)
{
InVoice object=new InVoice("abc","efgh",1,1.0);
//Scanner obj=new Scanner (System.in);
System.out.printf("%s %s\n", "The Item name is: ",
object.getname());
//String name=obj.nextLine();
System.out.printf("%s %s\n","The Item description is: ", object.getdescription());
//String description=obj.nextLine();
System.out.printf("%s %d\n","The quantity is: ",object.getquantity());
//int quantity=obj.nextInt();
System.out.printf("%s %s\n","The price is: ",object.getprice());
//double price=obj.nextDouble();
// object.set(name,description, quantity, price);
System.out.printf("\n%s:\n\n%s\n",
"Updated object", object);
}
}