简体   繁体   English

找不到构造函数符号

[英]Cannot find constructor symbol

Here is my code for my Ingredient class: 这是我的成分类的代码:

public class Ingredient {

 /* Attribute declarations */
 private String name; //  name
 private int calorieCount; //calorie count

  /* Constructor */
 public Ingredient(String name, int calorieCount) {
  this.name = name;
  this.calorieCount = calorieCount;
 }

 public String getName(){
  return name;
 }

  public int getCalorieCount(){
  return calorieCount;
 }

  public static void main (String[] args)
  {
    Ingredient item1 = new Ingredient ("Butter", "100");
    System.out.println(item1);
  }
}

When I try and run it, I get a compiler error: 当我尝试运行它时,出现编译器错误:

1 error found:
File: C:\eclipse\workspace\Assignment NEW1\Ingredient.java  [line: 28]
Error: C:\eclipse\workspace\Assignment NEW1\Ingredient.java:28: cannot find symbol
symbol  : constructor Ingredient(java.lang.String,java.lang.String)
location: class Ingredient

What am I doing wrong? 我究竟做错了什么?

You are passing 100 as string in your constructor: - 您在构造函数中将100作为字符串传递:-

Ingredient item1 = new Ingredient ("Butter", "100");

Change it to: - 更改为:-

Ingredient item1 = new Ingredient ("Butter", 100);

You are supposed to pass second parameter as int. 您应该将第二个参数作为int传递。 you passed string "100“, however. change it to number 100 instead of "100". 您传递了字符串“ 100”,但是将其更改为数字100而不是“ 100”。

每当你编译的一些符号时间误差是找不到的,永远记住,你已经在使用你的程序的东西存在或无法使用现有的类路径中找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM