错误;找不到标志;符号:变量超级

时间:2013-11-23 02:20:28

标签: java class variables symbols

我正在研究包含类CarRental.java,LuxuryCarRental.java和UseCarRental.java的3个程序,在我的LuxuryCarRental.java上,我不断收到错误,错误;找不到标志;符号:类的变量超级,这是我的程序,我对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;
}

}

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

首先,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();

已提供,方法getRentalDaysCarRental类中是公开的。

关于你在@Jeroen的答案中提到的错误'评论, 确保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();
  }
}