Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中私有成员变量的子类访问_Java_Oop_Inheritance - Fatal编程技术网

java中私有成员变量的子类访问

java中私有成员变量的子类访问,java,oop,inheritance,Java,Oop,Inheritance,古德里奇和塔玛西亚的《数据结构和算法》一书中的这个问题真的难住了我。本书分为以下两类: public class CreditCard { private String customer; private String bank; private String account; private int limit; protected double balance; public CreditCard(String customer, String bank, String account,

古德里奇和塔玛西亚的《数据结构和算法》一书中的这个问题真的难住了我。本书分为以下两类:

public class CreditCard {
private String customer;
private String bank;
private String account;
private int limit;
protected double balance;

public CreditCard(String customer, String bank, String account, int limit, double balance) {
    this.customer = customer;
    this.bank = bank;
    this.account = account;
    this.limit = limit;
    this.balance = balance;

}

public CreditCard(String customer, String bank, String account, int limit) {
    this(customer, bank, account, limit, 0.0);
}

public String getCustomer() { return this.customer; }
public String getBank() { return this.bank; }
public String getAccount() { return this.account; }
public int getLimit() { return this.limit; }
public double getBalance() { return this.balance; }

public boolean charge(double price) {
    if(price + this.balance > this.limit)
        return false;

    this.balance += price;
    return true;
}

public void makePayment(double amount) {
   if( amount < 0)
      System.out.println("Cannot process negative payment");
   else
      this.balance -= amount;
}

public void updateCreditLimit() {
    this.limit -= balance;
}

public static void printSummary(CreditCard card) {
    System.out.println("Customer = " + card.customer);
    System.out.println("Bank = " + card.bank);
    System.out.println("Account = " + card.account);
    System.out.println("Balance = " + card.balance);
    System.out.println("Limit = " + card.limit);
}
问题:

假设我们更改了CreditCard类,使实例变量
balance
具有私有可见性。为什么捕食者CreditCard.charge方法的以下两种实现存在缺陷

public boolean charge(double price) {
    boolean isSuccess = super.charge(price);
    if(!isSuccess)
       charge(5); //penalty for late fee
     return isSuccess;
 }
第二个:

public boolean charge(double price) {
    boolean isSuccess = super.charge(price);
    if(!isSuccess)
       super.charge(5); //penalty for late fee
     return isSuccess;
 }
我的理解是,子类不能直接操作它的超类的私有字段。该字段必须是受保护的或公共的。这允许我们在子类
中说This.balance+=5
。我对原则的理解不是问题所在,我的问题在于我被要求回答的问题。从实现的角度来看,我可以清楚地看到PredatoryCreditCard.charge方法没有任何缺陷,因为在问题提供的新实现的两种情况下,我们都在改变类的平衡字段,因为调用了super。我无法在新的实现中发现任何缺陷,除非我认为我对继承的了解存在漏洞


先谢谢你

在这两种情况下,如果您接近余额,且费用(价值5)将超过您的限额,则不能向您收取费用。在第一种情况下:

public boolean charge(double price) {
    boolean isSuccess = super.charge(price);
    if(!isSuccess)
       charge(5); //penalty for late fee
     return isSuccess;
 }

issucess
失败时,这里会发生什么?我们陷入了无限递归调用this.charge(…)这两种实现中都没有语法错误。两者都有(或可能有)逻辑错误

首次实施

public boolean charge(double price) {
    boolean isSuccess = super.charge(price);
    if(!isSuccess)
       charge(5);  //penalty for late fee
     return isSuccess;
 }
public boolean charge(double price) {
    boolean isSuccess = super.charge(price);
    if(!isSuccess)
       super.charge(5); //penalty for late fee
     return isSuccess;
 }
我们从一个层次开始

第一个语句是
super.charge(price)

如果该语句返回
false
,则我们调用
this.charge(5)

我们现在有两个层次的深度

第一个语句是
super.charge(5)

如果该语句返回
false
,则我们调用
this.charge(5)

我们现在有三个层次的深度

第一个语句是
super.charge(5)

如果该语句返回
false
,则我们调用
this.charge(5)

我们现在有四层深。

你明白了。第一个实现可能导致无限递归,这将导致堆栈溢出。看

第二次实施

public boolean charge(double price) {
    boolean isSuccess = super.charge(price);
    if(!isSuccess)
       charge(5);  //penalty for late fee
     return isSuccess;
 }
public boolean charge(double price) {
    boolean isSuccess = super.charge(price);
    if(!isSuccess)
       super.charge(5); //penalty for late fee
     return isSuccess;
 }
我们试图收取
价格
。我们记录交易是否成功。
如果交易失败,我们会收取滞纳金。我们不记录交易是否成功。
如果滞纳金不起作用,我们不在乎


我不确定这里的错误是什么。它当然不会模仿我的银行的运作方式,但这不一定是个问题。我认为避免滞纳金是一件坏事,但这实际上取决于您的要求。

如果5>限制余额,那么余额在您的版本中不会改变。在这个版本中,balance属性是直接操作的,因此它独立于limit和balance值增加了5。而第一个替代值可能导致无限递归。