简体   繁体   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.

}

So far, I am having trouble with part 5. For starters, this is not stand alone code. 到目前为止,我在第5部分中遇到了麻烦。对于初学者来说,这并不是独立的代码。 It is a larger part of a bank account program. 它是银行帐户计划的较大部分。 The 'teller' part is the code that is actually run. “讲述人”部分是实际运行的代码。

My problem is I cannot get 'SavingsAccount' to clean compile. 我的问题是我无法获取“ SavingsAccount”进行清理。 No process I try for part 5 works. 对于第5部分,我没有尝试任何过程。 It says it cannot read the symbol, but I'm not sure how exactly it can be wrong. 它说它看不懂该符号,但是我不确定它到底有多错误。 I also compiled the 'teller' file, but it won't compile without quite a few errors. 我还编译了“ teller”文件,但没有很多错误就不会编译。 The biggest being that it can't find classes and symbols that I haven't touched in the first place. 最大的优点是它找不到我一开始没有碰过的类和符号。 I'm not sure if that is due to the fact I haven't got SavingsAccount to compile cleanly or not. 我不确定这是否是由于我没有SavingsAccount可以干净编译的事实。

COMPILER ERROR: Activity-10-1\\SavingsAccount.java:68: error: cannot find symbol deposit (getBalance() * (interestRate / 12.0)); 编译器错误:Activity-10-1 \\ SavingsAccount.java:68:错误:找不到符号存款(getBalance()*(interestRate / 12.0)); ^ symbol: method getBalance() location: class SavingsAccount 1 error ^符号:方法getBalance()位置:类SavingsAccount 1错误

Tool completed with exit code 1 工具已完成,退出代码为1

You need to get step 1 done before step 5 will work. 您需要先完成步骤1,然后才能执行步骤5。 As it stands, SavingsAccount inherits from Object , not from BankAccount . 就目前而言, SavingsAccount继承自Object而不是BankAccount Try changing 尝试改变

public class SavingsAccount

to

public class SavingsAccount extends BankAccount

At that point, the getBalance() method (which I'm guessing is defined in the BankAccount class) should become accessible. 到那时,应该可以访问getBalance()方法(我猜是在BankAccount类中定义)。

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

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