Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA“calculateFee无法解析为类型”_Java_Class_Methods - Fatal编程技术网

JAVA“calculateFee无法解析为类型”

JAVA“calculateFee无法解析为类型”,java,class,methods,Java,Class,Methods,我在Java类中遇到了这个问题。我想要一些导游 问题是: calculateFee无法解析为类型 AuctionSale.java public class AuctionSale public double calculateFee() { if (takingOffers == true) { return 0; } else if (reservePrice > 0) { return reservePrice/20 +

我在Java类中遇到了这个问题。我想要一些导游

问题是:

calculateFee无法解析为类型

AuctionSale.java

public class AuctionSale


  public double calculateFee() {

     if (takingOffers == true) {
        return 0;
     } else if (reservePrice > 0) {
        return reservePrice/20 + currentOffer/20;
     } else {
        return currentOffer/10;
     }
  }
AuctionSaleSystem.java

public class AuctionSaleSystem

 private static void displaySalesReport()
 {
      System.out.println("Current Auction Sales Report:");
  System.out.println();


  for (int i=0; i<sales.length; i++) {
     String itemNumber = sales[i].getItemNumber();
     String description = sales[i].getDescription();
     Double brokerFee = 0.00;

     // Stuck at this point, not able to get past the error
     // "calculateFee cannot be resolved to a type"  :(
     calculateFee brokerFee = new calculateFee();

     brokerFee = brokerFee.calculateFee();


     System.out.printf("Item Number: %s, Description: %s, Broker fee: %d %n", 
                       itemNumber, description, brokerFee);
  }
}

calculateFee是一种方法,而不是一种类型。改为

AuctionSale brokerFee = new AuctionSale();
double result = brokerFee.calculateFee();

 System.out.printf("Item Number: %s, Description: %s, Broker fee: %d %n", 
                       itemNumber, description, result);
您应该学习Java基础知识

calculateFee不是一个类,而是一个方法,因此您无法实例化它。访问该方法的正确方法是实例化AuctionSale并继续调用calculateFee方法

AuctionSale auctioneer = new AuctionSale();
brokerFee = auctioneer.calculateFee();

System.out.printf("Item Number: %s, Description: %s, Broker fee: %d %n", 
                   itemNumber, description, brokerFee);

是一个教java面向对象编程的好网站。我建议您仔细阅读。

如果您在这一行中遇到错误-

brokerFee = brokerFee.calculateFee();
calculateFee brokerFee = new calculateFee();
那么,调用calculateFee的brokerFee对象可能不是AuctionSale类的对象。正确的方法应该是-

AuctionSale brokerFee = new AuctionSale();
double value = brokerFee.calculateFee();

如果您在此行中遇到错误-

brokerFee = brokerFee.calculateFee();
calculateFee brokerFee = new calculateFee();
然后,JVM希望“calculateFee”是一个类,因为您正在调用构造函数。但是我想你没有这个名字的类。您有一个使用calculateFee方法的AuctionSale类。因此出现了错误

我想如果你改变

calculateFee brokerFee = new calculateFee();

brokerFee = brokerFee.calculateFee();
对此-

AuctionSale brokerFee = new AuctionSale();
double value = brokerFee.calculateFee();

你应该没事。

为什么会有这么多的反对票?如果我理解正确的话,calculateFee是类AuctionSale的一种非静态方法。因此,要能够调用它,必须首先获取该类的一个实例。所以它应该是这样的:AuctionSale brokerFee=新的AuctionSale;双倍费用=经纪人费。计算费;