【问题标题】:can not find error in java [closed]在java中找不到错误[关闭]
【发布时间】:2015-01-19 03:37:45
【问题描述】:

我创建了两个类应用程序,类是 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();

【问题讨论】:

  • InVoice 没有默认构造函数 (InVoice())
  • 这和C++有什么关系?

标签: java compiler-errors


【解决方案1】:

如果您没有构造函数,Java 编译器将为您创建一个无参数构造函数。

只要您提供了一个有参数的构造函数,编译器就不会创建无参数构造函数。

因此,如果您向 InVoice 添加无参数构造函数(如下所示),它应该可以工作。

public InVoice() {
}

【讨论】:

  • 是的,它成功了,谢谢
  • @ammi 至少接受答案
【解决方案2】:

另一种方式是:

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

}

}

【讨论】:

    猜你喜欢
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-27
    • 2010-10-25
    相关资源
    最近更新 更多