如何在创建实例时检查两个子类中哪个被调用了?

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

How to check between two subclasses which one has been called when creating an instance?

问题

我正在尝试创建一个具有操作的“银行账户”。其中一些操作是:

  1. 银行存款:类名为Ajout
  2. 银行取款:类名为Retrait

有一个名为solde的银行余额变量和一个名为montant的金额变量。

AjoutOuRetrait类是父类(意味着添加或取款)。

现在我在主类中的预期行为如下:

c1.operation(new Ajout(750, new Date(01,01,2017)));
c1.operation(new Retrait(50, new Date(05,03,2017)));
System.out.println(c1.getSolde());          // 结果 -> 700
public class AjoutOuRetrait {
	public int montant;
	public Date date;

	public AjoutOuRetrait(int montant, Date d) {
		this.montant = montant;
		this.date = d;
	}
}
public class CompteBancaire {
	
	private String id;
	private Banque banque;
	private int solde;	
	
	public CompteBancaire(String id, Banque b) {
		this.id = id;
		this.banque = b;
	}
	
	public void operation(AjoutOuRetrait aor){
		this.solde = aor.montant;
	}
}

还有我省略的获取器和设置器。

public class Retrait extends AjoutOuRetrait {

	public Retrait(int montant, Date d) {
		super(montant, d);
	}
}
public class Ajout extends AjoutOuRetrait {
		
	public Ajout(int montant, Date d) {
		super(montant, d);
	}
}

我正在考虑一种方式来区分(使用条件语句)在**operation()**方法的参数中调用哪个子类,无论是Retrait(取款)还是Ajout(存款)++。

英文:

I'm trying to create a "bank account" with its operations. Some of them are:

  1. bank addition : class Ajout
  2. bank withdrawal : class Retrait

There are the bank balance variable named solde and amount named montant

The AjoutOuRetrait class is the mother class (means AdditionOrWithdrawal)

Now what I expect in my main class is the following:

c1.operation(new Ajout(750, new Date(01,01,2017)));
c1.operation(new Retrait(50, new Date(05,03,2017)));
System.out.println(c1.getSolde());          // result -> 700
public class AjoutOuRetrait {
	public int montant;
	public Date date;

	public AjoutOuRetrait(int montant, Date d) {
		this.montant = montant;
		this.date = d;
	}
public class CompteBancaire {
	
	private String id;
	private Banque banque;
	private int solde;	
	
	public CompteBancaire(String id, Banque b) {
		this.id = id;
		this.banque = b;
	}
	
	public void operation(AjoutOuRetrait aor){
		this.solde = aor.montant;
}

more the getters and setters that I omitted.

public class Retrait extends AjoutOuRetrait {

	public Retrait(int montant, Date d) {
		super(montant, d);
	}
}
public class Ajout extends AjoutOuRetrait{
		
	public Ajout(int montant, Date d) {
		super(montant, d);
	}

I was thinking on a way to differentiate (with conditional statement) which child class I call in argument of operation() method , whether it is Retrait (WithDrawal) -- or Ajout(Additional) ++

答案1

得分: 0

你可以在 operation 中检查 operation 中的子类型,如下所示。

public void operation(AjoutOuRetrait aor){
    if(aor instanceof Ajout) {
        this.solde += aor.montant;
    } else if (aor instanceof Retrait) {
        this.solde -= aor.montant;
    }
}

或者将它们拆分开来。

public void operation(Ajout a){
     this.solde += a.montant;
}
public void operation(Retrait r){
     this.solde -= r.montant;
}

此外,你可以获取作为操作的右操作数的值,如下所示。

public class AjoutOuRetrait {
    public int getValue() {
        return this.montant;
    }
}

public class Retrait {
    @Override
    public int getValue() {
        return -this.montant;
    }
}

public void operation(AjoutOuRetrait aor) {
    this.solde += aor.getValue();
}
英文:

You might check the child type in operation like.

public void operation(AjoutOuRetrait aor){
    if(aor instanceof Ajout) {
        this.solde += aor.montant;
    } else if (aor instanceof Retrait) {
        this.solde -= aor.montant;
    }
}

or split them

public void operation(Ajout a){
     this.solde += a.montant;
}
public void operation(Retrait r){
     this.solde -= r.montant;
}

Also you might get the value used as right operand of your operation like

public class AjoutOuRetrait {
    public int getValue() {
        return this.montant;
    }
}

public class Retrait {
    @Override
    public int getValue() {
        return -this.montant;
    }
}

public void operation(AjoutOrRetrait aor) {
    this.solde += aor.getValue();
}

huangapple
  • 本文由 发表于 2020年9月23日 23:25:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64031255.html
匿名

发表评论

匿名网友

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

确定