如何在Java中使用super关键字解决问题。

huangapple go评论70阅读模式
英文:

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;
}

huangapple
  • 本文由 发表于 2020年9月26日 01:37:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64068931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定