【问题标题】:Error; cannot find symbol; Symbol: variable super错误;找不到标志;符号:变量超
【发布时间】:2013-12-08 02:14:27
【问题描述】:

我正在开发 3 个包含 CarRental.java、LuxuryCarRental.java 和 UseCarRental.java 类的程序,在我的 LuxuryCarRental.java 上,我不断收到错误,错误;找不到标志;符号:类的变量super,这是我的程序,我对Java比较陌生,所以请详细说明!提前致谢!

import java.text.DecimalFormat;


public class LuxuryCarRental extends CarRental{

private boolean chauffeur;
private double dailyChauffeurFee;


public LuxuryCarRental(String renterName, int renterZip, String sizeOfCar,int rentalDays, boolean chauffeur) {
    super(renterName, renterZip, sizeOfCar, rentalDays);
    this.chauffeur = chauffeur;
}


public void display(){

    super.dailyRentalFee = 79.99;


    this.dailyChauffeurFee = 0;
    if(chauffeur){
        this.dailyChauffeurFee = 200;
    }


    super.totalRentalFee = super.dailyRentalFee * super.getRentalDays() + this.dailyChauffeurFee * super.getRentalDays();


    DecimalFormat df = new DecimalFormat("0.00");


    System.out.println("Car Rental - Renter Name : " + super.getRenterName() + ", Renter Zip: " + super.getRenterZip() + 
            ", Rental Days : " + super.getRentalDays() + 
            ", Daily Rental Fee: " + dailyRentalFee + ", Daily Chauffer Fee: " + dailyChauffeurFee + 
            ", Total Rental Fee: " + df.format(totalRentalFee));
}

}

这是我所有三个程序中相互对应的所有类。

  public class CarRental {

private String renterName;
private int renterZip;
private String sizeOfCar;
private int rentalDays;
protected double dailyRentalFee;
protected double totalRentalFee;


public class UseCarRental

public class LuxuryCarRental extends CarRental {

private boolean chauffeur;

private double dailyChauffeurFee;

public CarRental(String renterName, int renterZip, String sizeOfCar, int rentalDays)
{
    renterName = renterName;
    renterZip = renterZip;
    sizeOfCar = sizeOfCar;
    rentalDays = rentalDays;

还有我修改后的代码:

public class CarRental 
{

public static void main(String[] args) 
{

private String renterName;
private int renterZip;
private String sizeOfCar;
private int rentalDays;
protected double dailyRentalFee;
protected double totalRentalFee;
}


public CarRental(String renterName, int renterZip, String sizeOfCar, int rentalDays)
{
    renterName = renterName;
    renterZip = renterZip;
    sizeOfCar = sizeOfCar;
    rentalDays = rentalDays;
}

 public void setDailyRentalFee(double dailyRentalFee) 

 {

 this.dailyRentalFee = dailyRentalFee;

 }

public double getDailyRentalFee() 

 {

 return dailyRentalFee;

 }


public void display(){

    if(sizeOfCar.equalsIgnoreCase("economy"))
  {
        dailyRentalFee = 29.99;         
    } 

  else if(sizeOfCar.equalsIgnoreCase("midsize"))
  {
        dailyRentalFee = 38.99;         
    } else {
        dailyRentalFee = 43.50;         
    }

    //calculates total rental fee
    this.totalRentalFee = this.dailyRentalFee * rentalDays;

    DecimalFormat df = new DecimalFormat("0.00");

    //displays output
    System.out.println("Car Rental - Renter Name : " + renterName + ", Renter Zip: " + renterZip + 
            ", Size of car: " + sizeOfCar + ", Rental Days : " + rentalDays + 
            ", Daily Rental Fee: " + dailyRentalFee + ", Total Rental Fee: " + df.format(totalRentalFee));
}


public String getRenterName() 
{
    return renterName;
}


public int getRenterZip() 
{
    return renterZip;
}


public int getRentalDays() 
{
    return rentalDays;
}

}

【问题讨论】:

    标签: java class variables symbols


    【解决方案1】:
    super.dailyRentalFee = 79.99;
    

    这不起作用。您使用过的所有其他地方也是如此。

    我假设你的班级有一个private 字段dailyRentalFee? 改为protected。或者使用public/protected getter 和 setter。

    您在一个子类中,您应该将其视为超类的扩展。超类中可用的所有内容都在子类中可用,前提是您不使用private 访问,而是使用protected(在当前类和子类中可用)或public(在任何可以访问当前类的地方可用) )。

    例子:

    class SuperClass {
       protected int someValue = 5;
       private int anotherValue = 10;
    }
    
    class SubClass extends SuperClass {
       public void doSomething() {
          someValue = 6; // I can access it because it's protected instead of private
          anotherValue = 1; // I can't access it because it's private and only accessible in the SuperClass
       }
    }
    

    总结一下:

    • 去掉super.Xsuper()用来调用超类的构造函数
    • 使用protectedpublic 访问标识符而不是private

    【讨论】:

    • 如果字段受到保护,您可以使用this.dailyRentalFee = 79.99dailyRentalFee=79.99。无需指定它们来自super
    • 是的@Jeroen 我正在为我的dailyRentalFee 使用受保护的双字段
    • @FlightOdyssey:打字时我意识到我从来没有明确说过。现在就在里面。
    • 另外,在我摆脱 super LuxuryCarRental.java:4: 错误:找不到符号 public class LuxuryCarRental extends CarRental { ^ symbol:类 CarRental
    • 这意味着你的班级CarRental没有找到。 CarRental 是否定义为 public 和/或在您当前的命名空间中?
    【解决方案2】:

    首先,super 无法按照您使用的方式工作。 当您扩展一个类时,在您的情况下为CarRental,您继承了该类的所有publicprotected 成员。所以要使用你的超类的变量,你不必前缀super,你可以像子类一样使用变量。所以而不是

    super.dailyRentalFee = 79.99;
    

    使用

    dailyRentalFee = 79.99;  // since dailyRentalFee is protected in class CarRental
                             // this will work
    

    同样,

    super.totalRentalFee = super.dailyRentalFee * super.getRentalDays() + this.dailyChauffeurFee * super.getRentalDays();
    

    应该写成

    totalRentalFee = dailyRentalFee * getRentalDays() + this.dailyChauffeurFee * getRentalDays();
    

    前提是,getRentalDays 方法在 CarRental 类中是公共的。

    关于你在@Jeroen's answers' cmets 中提到的错误, 确保LuxuryCarRentalCarRental 在同一个package 中。简单来说, 确保两个文件在同一个文件夹中。

    编辑:

    您的代码不包含main 方法,这就是产生该错误的原因。您的程序中应该有一个 main 方法来执行它。那是所有java应用程序的起点。所以在里面定义一个带有main 方法的类,然后创建一个LuxuryCarRental 对象并在那里执行你的计算。例如,

    class Sample {
      public static void main(String[] args) { //this is how all main methods would look
    
        LuxuryCarRental luxuryCar = new LuxuryCarRental("A",62020,"SUV",10,true);
        //call the LuxuryCarRental methods as per your coding requirements
    
      }
    }
    

    看,很简单,

    class CarRental {
      //CarRental code
    }
    
    class LuxuryCarRental {
      //LuxuryCarRental code
    }
    
    class Test {
      public static void main(String[] args) {
        LuxuryCarRental luxuryCar = new LuxuryCarRental("A",62020,"SUV",10,true);
        luxuryCar.display();
      }
    }
    

    【讨论】:

    • 现在我在运行文件时收到此错误,现在编译工作正常:No Main Methods, applet, or MIDlets found in file?
    • (编辑 - 对这些错误感到抱歉)呃,现在它给了我一个 Illegal start of Expression CarRental.java:16 的错误:错误:表达式的非法开始受保护的 double totalRentalFee; ^
    • 您可能缺少{}。既然你说,它以前编译得很好,我假设你已经改变了你的代码。重新发布更改后的代码。
    • 你的代码现在是错误的。 LuxuryCarRental 类应该在一个单独的不同文件中。不在CarRental 类中。这个新课程UserRental是什么?
    • 三个文件相互连接,UseCarRentalLuxuryCarRentalCarRental顺便说一句,我贴出新代码​​
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-12
    • 1970-01-01
    • 2015-11-02
    • 2016-08-08
    • 2020-06-30
    相关资源
    最近更新 更多