实例化对象编译错误

时间:2014-12-05 00:49:00

标签: java compiler-errors

我一直在研究Java程序,学习如何使用和创建构造函数。出于某种原因,我不断收到错误,告诉我我的程序在实例化对象时无法找到符号。

这是资源类:

public class Pizza
{
   private int size;
   private String topping;
   private double cost;


   public Pizza()
   {
      size = 10;
      topping = "cheese";
      cost = 9.00;
   }

   public Pizza(int s, String t, double c)
   {
      s = size;
      t = topping;
      c = cost;
   }

   public int getSize() {
      return size;
   }

   public void setSize(int s) {
      s = size;
   }

   public String getTopping(){
      return topping;
   }

   public void setTopping(String t){
      topping = t;
   }

   public void setCost(double c) {
      cost = c;
   }   

   public double getCost(double c){
      return cost;
   }


   public String toString()
   {
      return String.format("%d inch %s pizza will cost $%,.2f\n", size, topping, cost);

   }
}

以下是司机班:

public class PizzaTest
{
   public static void main(String[] args)
   {

      Pizza orderTwo = new Pizza();

      System.out.printf("%-25s %s", "Pizza #1", orderTwo);
   }
}

我一直在仔细检查代码,但我似乎无法找到任何语法错误。任何建议表示赞赏。编译器错误:

PizzaTest.java:6: error: cannot find symbol Pizza orderTwo = new Pizza();
^ symbol: class Pizza location: class PizzaTest
PizzaTest.java:6: error: cannot find symbol Pizza orderTwo = new Pizza();
^ symbol: class Pizza location: class PizzaTest
2 errors

4 个答案:

答案 0 :(得分:0)

您很可能需要从PizzaTest类导入对Pizza类的引用。

import <packagename>.Pizza;

答案 1 :(得分:0)

显然,PizzaPizzaTest位于不同的包中,您尚未将import声明添加到PizzaTest

答案 2 :(得分:-1)

这是错误的地方

   public Pizza(int s, String t, double c)
   {
      s = size;
      t = topping;
      c = cost;
   }

应该是相反的方式

   public Pizza(int s, String t, double c)
   {
      sise = s;
      topping = t;
      cost = c;
   }

同样在setSize()函数中你也可以采用相反的方式

答案 3 :(得分:-1)

此方法中不应存在参数,请删除double c

public double getCost(double c){
  return cost;
}
相关问题