英文:
How can I solve the problem using super keyword in Java
问题
public class CommissionCompensationModel
{
private double grossSales;
private double commissionRate;
public CommissionCompensationModel(double grossSales, double commissionRate)
{
this.grossSales = grossSales;
this.commissionRate = commissionRate;
}
}
This is my CommissionCompensationModel class
public class BasePlusCommissionCompensationModel extends CommissionCompensationModel
{
protected double baseSalary;
public void BasePlusCommssionCompenationModel(double grossSales, double commissionRate, double bs)
{
super(grossSales, commissionRate);
this.baseSalary = bs;
}
}
This is a subclass of the previous one. It intends to inherit its superclass and add the base salary property. However, the IDE indicates an issue with the `super` statement. How to resolve this problem?
英文:
public class CommissionCompensationModel
{
private double grossSales;
private double commissionRate;
public CommissionCompensationModel(double grossSales, double commissionRate)
{
this.grossSales=grossSales;
this.commissionRate=commissionRate;
}
}
This is my CommissionCompensationModel class
public class BasePlusCommissionCompensationModel extends CommissionCompensationModel
{
protected double baseSalary;
public void BasePlusCommssionCompenationModel(double grossSales, double commissionRate, double bs)
{
**super(grossSales, commissionRate);**
this.baseSalary=bs;
}
}
This is a subclass of the previous one. It intends to inherit its superclass and add the base salary property. However, the IDE tells me there's some problem with strong text. How to solve the problem?
答案1
得分: 1
在子类中构造函数的书写方式有误。
构造函数不能有返回类型,甚至不能是 void
。
更新如下:
class CommissionCompensationModel {
private double grossSales;
private double commissionRate;
public CommissionCompensationModel(double grossSales, double commissionRate) {
this.grossSales = grossSales;
this.commissionRate = commissionRate;
}
}
class BasePlusCommissionCompensationModel extends CommissionCompensationModel {
protected double baseSalary;
public BasePlusCommissionCompensationModel(double grossSales, double commissionRate, double bs) {
super(grossSales, commissionRate);
this.baseSalary = bs;
}
}
英文:
Constructor is written wrong in the subclass.
Constructors can't have a return type not even void
.
Update to this :
class CommissionCompensationModel {
private double grossSales;
private double commissionRate;
public CommissionCompensationModel(double grossSales, double commissionRate) {
this.grossSales = grossSales;
this.commissionRate = commissionRate;
}
}
class BasePlusCommissionCompensationModel extends CommissionCompensationModel {
protected double baseSalary;
public BasePlusCommissionCompensationModel(double grossSales, double commissionRate, double bs) {
super(grossSales, commissionRate);
this.baseSalary = bs;
}
}
答案2
得分: 0
构造函数没有返回类型,甚至没有 void
。此外,您在那里有一个拼写错误 - BasePlusCommssionCompenationModel
而不是 BasePlusCommissionCompensationModel
(在两个 "m" 之后缺少一个 "i")。
修正拼写错误,去掉返回类型,然后您就可以了:
public BasePlusCommissionCompensationModel(double grossSales, double commissionRate, double bs)
{
super(grossSales, commissionRate);
this.baseSalary = bs;
}
英文:
Constructors don't have a return type, not even void
. Additionally, you have a typo there - BasePlusCommssionCompenationModel
instead of BasePlusCommissionCompensationModel
(missing an "i" afer the "m"s).
Fix the typo and remove the return type and you should be fine:
public BasePlusCommissionCompensationModel(double grossSales, double commissionRate, double bs)
{
super(grossSales, commissionRate);
this.baseSalary=bs;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论