繁体   English   中英

通过方法继承和传递参数

[英]inheritance and passing parameters through methods

import java.text.NumberFormat;

// 1. ***** indicate that SavingsAccount inherits
//          from BankAccount
public class SavingsAccount
{

public final double DEFAULT_RATE = .03;

// 2. ****** define the private interestRate instance variable
// interestRate, a double, represents an annual rate
// Part 2 student code starts here:
    private double interestRate;



// Part 2 student code ends here.

// 3 ***** write the default constructor
/** default constructor
*   explicitly calls the BankAccount default constructor
*   set interestRate to default value DEFAULT_RATE
*   print a message to System.out indicating that
*    constructor is called
*/
// Part 3 student code starts here:
    public void BankAccount()
    {
        interestRate = DEFAULT_RATE;
        System.out.println("Default constructor is called. ");
    }


// Part 3 student code ends here.

// 4 ***** write the overloaded constructor
/** overloaded constructor
*   explicitly call BankAccount overloaded constructor
*   call setInterestRate method, passing startInterestRate
*   print a message to System.out indicating that
*    constructor is called
*  @param  startBalance      starting balance
*  @param  startInterestRate starting interest rate
*/
// Part 4 student code starts here:

    public void BankAccount(double startBalance,double startInterestRate)
    {

        setInterestRate(startInterestRate);
        System.out.println("Overloaded constructor is called. ");
    }
        // Part 4 student code ends here.

// 5 ****** write this method:
/** applyInterest method, no parameters, void return value
*  call deposit method, passing a month's worth of interest
*  remember that interestRate instance variable is annual rate
*/
// Part 5 student code starts here:

    public void applyInterest()
    {
        deposit ( super.getBalance() * (interestRate / 12.0));
    }

// Part 5 student code ends here.

/** accessor method for interestRate
*  @return  interestRate
*/
public double getInterestRate()
{
  return interestRate;
}

/** mutator method for interestRate
*  @param  newInterestRate new value for interestRate
*          newInterestRate must be >= 0.0
*            if not, print an error message
*/
public void setInterestRate(double newInterestRate)
{
  if (newInterestRate >= 0.0)
  {
    interestRate = newInterestRate;
  }
  else
  {
    System.err.println("Interest rate cannot be negative");
  }
}

// 6 *****  write this method
/* toString method
*  @return String containing formatted balance and interestRate
*    invokes superclass toString to format balance
*    formats interestRate as percent using a NumberFormat object
*    To create a NumberFormat object for formatting percentages
*    use the getPercentInstance method in the NumberFormat class,
*    which has this API:
*       static NumberFormat getPercentInstance( )
*/
// Part 6 student code starts here:

    public String toString()
    {
      NumberFormat percent = NumberFormat.getPercentInstance();
      return super.toString()
        + "\n" + "interestRate is " + percent.format(interestRate);
    }

// Part 6 student code ends here.

}

到目前为止,我在第5部分中遇到了麻烦。对于初学者来说,这并不是独立的代码。 它是银行帐户计划的较大部分。 “讲述人”部分是实际运行的代码。

我的问题是我无法获取“ SavingsAccount”进行清理。 对于第5部分,我没有尝试任何过程。 它说它看不懂该符号,但是我不确定它到底有多错误。 我还编译了“ teller”文件,但没有很多错误就不会编译。 最大的优点是它找不到我一开始没有碰过的类和符号。 我不确定这是否是由于我没有SavingsAccount可以干净编译的事实。

编译器错误:Activity-10-1 \\ SavingsAccount.java:68:错误:找不到符号存款(getBalance()*(interestRate / 12.0)); ^符号:方法getBalance()位置:类SavingsAccount 1错误

工具已完成,退出代码为1

您需要先完成步骤1,然后才能执行步骤5。 就目前而言, SavingsAccount继承自Object而不是BankAccount 尝试改变

public class SavingsAccount

public class SavingsAccount extends BankAccount

到那时,应该可以访问getBalance()方法(我猜是在BankAccount类中定义)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM