我正在处理包含CarRental.java、LuxuryCarRental.java和UseCarRental.jsava类的3个程序,在我的LuxuryCar Rental.jva上,我不断收到错误,error;找不到符号;符号:类的变量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;
}
}
首先,超级
不能像你使用的那样工作。当你扩展一个类时,在你的例子中,你继承了该类的所有公共
和protected
成员。所以要使用你的超级类的变量,你不必前缀超级
,你可以使用这个变量,就像子类持有它一样。所以而不是
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();
前提是,方法getRentalDay
在Carrent
类中是公共的。
关于您在@Jeroen的回答评论中提到的错误,请确保LuxuryCarRental
和CarRental
位于同一包
中。简单地说,确保两个文件都在同一文件夹中。
编辑:
您的代码不包含main
方法,这就是产生该错误的原因。您的程序中应该有一个main
方法让它执行。这是所有java应用程序的起点。因此,请定义一个内部带有main
方法的类,然后创建一个LuxuryCarrent
对象并在其中执行您的计算。例如,
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();
}
}
super.dailyRentalFee = 79.99;
这不管用。你用过的其他地方也一样。
我假设您的类有一个私有
字段dailyRentalFee
?改为protected
。或者使用公共/保护
getter和setter。
你在一个子类中,你应该把它看作是超类的一个扩展。只要不使用< code>private访问,而是使用< code>protected(在当前类和子类中可用)或< code>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
}
}
总结一下:
超级。X
, super()
用于调用超类的构造函数受保护
或公共
访问标识符而不是专用
标识符我正在用Android Studio制作我的第一个测试程序。 该程序有两个活动,您在第一个活动中插入一个文本,按下一个按钮,然后文本在另一个活动上显示。 我下面的YT教程,但我发现两个错误: 找不到符号变量,也找不到解决方案。 我在这里搜索了这么久,但没有找到有效的答案。我试过: > 清洁项目和重建项目。 我没有"导入Android。R". 我用了但它告诉我"未使用的导入语句"。 我已经更改了ID
在项目中,我有几个风格,几个构建类型和几个文件夹 构建组合: 文件夹结构 在每个文件夹中,我都有自定义的来注册这个 有时,但只是有时生成失败,出现“找不到符号”错误: 或与类无关但错误相同 但是下一个构建可能会成功,所以可能的流程是 构建(成功) 代码中的一些更改 构建(错误) 生成重试(成功) 注意事项 这不是同步问题,同步/无效缓存不是答案 免责声明:有些问题看起来像这一个,但它们是不同的,例
我遇到了一个奇怪的问题!我的xml文件中有每个布局的id名,并且在我的主要活动中创建了对它们的变量引用。java文件。 当我清理和重建我的项目时,我得到的是: “错误:找不到符号layoutMain=findViewById(r.id.layoutMain);” 我在用敏。SDK 21,目标29
错误是: 找不到符号
这里有一个类似的答案:如何在Java中将函数作为参数传递? 但提供的正确答案不起作用。我有一门课: 在函数内部我试图将传递到,但我得到的错误是: 找不到符号 符号:类Callable 我不知道为什么。 另外,我尝试使用返回类型字符串作为xMethod,您能传递一个返回类型不同的函数吗?
我是新人。每当我试图构建并运行这段代码时,都会出现这条消息。 下面是 XML 代码: 下面是Java代码: /***重要事项:在下面添加您的包裹。包名称可以在项目的AndroidManifest中找到。xml文件。*这是我们的示例使用的包名称:**packagecom.example.android。justjava;**/ 导入Android。R;导入Android捆绑包;import andr
嗨,我试着把谷歌分析在我的应用程序,我看到留档,但不为我工作,我有这个错误错误:(51,9)错误:找不到符号变量mTracker: 分析pplication.java 显示: 主要活动 ... 错误:错误:(51,9)错误:找不到符号变量mTracker 修复:问题是我需要将其放入主活动: 公共类主活动扩展活动{
问题内容: 我使用时代码工作正常 吗?在我看来这很奇怪。 (错误显示在终端上) 问题答案: 这是 Arrays 类的静态方法。 您应该像这样调用它: 请注意,您仍然必须像这样导入Arrays类: 或者像其他人提到的那样,如果您进行静态导入,则可以省略类名。 我认为这样做对可读性更好。