Java中的跨类方法和面向对象逻辑的问题

时间:2014-04-07 22:24:31

标签: java oop methods

我正在初学者Java课程,我正在进行我的第一个面向对象的任务。我确实理解OOP的基础知识,但我没有足够的知识将其付诸实践。如果有人能指出任何新手的错误或给我一些指示,我将非常感激。

这是作业:

创建类SavingsAccount。使用私有静态类变量来存储每个保护程序的annualInterestRate。该类的每个对象都包含一个私有实例变量savingsBalance,表示存储器当前具有的存储量。提供方法calculateMonthlyInterest通过将balanceInterestRate乘以12来计算月利息;这种兴趣应该加到savingsBalance。提供静态方法modifyInterestRate,将annualInterestRate设置为新值。编写一个驱动程序来测试类SavingsAccount。实例化两个不同的savingsAccount对象,saver1和saver2,余额分别为$ 2,000.00和$ 4,000.00。将annualInterestRate设置为3%,然后计算每月利息并打印每个储户的新余额。然后将annualInterestRate设置为5%并计算下个月的兴趣并打印每个储户的新余额。

这是我目前的代码:

class savings{
    public static void main(String[] args){
        double annualInterestRate = 0;
        double monthlyInterest = 0;
        SavingsAccount saver1 = new SavingsAccount(2000, .03);
        SavingsAccount saver2 = new SavingsAccount(4000, .03);
        calculateMonthlyInterest(saver1);
        System.out.println("Monthly Interest at 3%: " + monthlyInterest);
        calculateMonthlyInterest(saver2);
        System.out.println("Monthly Interest at 3%: " + monthlyInterest);
        modifyInterestRate(saver1);
        modifyInterestRate(saver2);
        calculateMonthlyInterest(saver1);
        System.out.println("Monthly Interest at 5%: " + monthlyInterest);
        calculateMonthlyInterest(saver2);
        System.out.println("Monthly Interest at 5%: " + monthlyInterest);
    }
}

class SavingsAccount{
    static double annualInterestRate;
    static private double savingsBalance;
    public static double calculateMonthlyInterest(double annualInterestRate){
                double monthlyInterest = 0;
                monthlyInterest = savingsBalance * annualInterestRate / 12;
                return monthlyInterest;
        }
    public static void modifyInterestRate(double annualInterestRate){
        annualInterestRate = .05;
    }
    SavingsAccount(double savingsBalance, double annualInterestRate){
        this.savingsBalance = savingsBalance;
        this.annualInterestRate = annualInterestRate;
    }
}

以下是编译错误:

F:\Archive\Homework_Spring_2014\Java\Project 2\Savings\savings.java:7: error: cannot find symbol
        calculateMonthlyInterest(saver1);
        ^
  symbol:   method calculateMonthlyInterest(SavingsAccount)
  location: class savings
F:\Archive\Homework_Spring_2014\Java\Project 2\Savings\savings.java:9: error: cannot find symbol
        calculateMonthlyInterest(saver2);
        ^
  symbol:   method calculateMonthlyInterest(SavingsAccount)
  location: class savings
F:\Archive\Homework_Spring_2014\Java\Project 2\Savings\savings.java:11: error: cannot find symbol
        modifyInterestRate(saver1);
        ^
  symbol:   method modifyInterestRate(SavingsAccount)
  location: class savings
F:\Archive\Homework_Spring_2014\Java\Project 2\Savings\savings.java:12: error: cannot find symbol
        modifyInterestRate(saver2);
        ^
  symbol:   method modifyInterestRate(SavingsAccount)
  location: class savings
F:\Archive\Homework_Spring_2014\Java\Project 2\Savings\savings.java:13: error: cannot find symbol
        calculateMonthlyInterest(saver1);
        ^
  symbol:   method calculateMonthlyInterest(SavingsAccount)
  location: class savings
F:\Archive\Homework_Spring_2014\Java\Project 2\Savings\savings.java:15: error: cannot find symbol
        calculateMonthlyInterest(saver2);
        ^
  symbol:   method calculateMonthlyInterest(SavingsAccount)
  location: class savings
6 errors

Tool completed with exit code 1

我相信这些错误来自跨类引用的方法,但我不知道正确的方法。

2 个答案:

答案 0 :(得分:0)

您似乎对如何在对象上运行方法存在一些误解。

例如,当你这样做时:

    SavingsAccount saver1 = new SavingsAccount(2000, .03);
    calculateMonthlyInterest(saver1);

您没有告诉saver1执行calculateMonthlyInterest,而是尝试在savings类上调用方法,并将saver1作为参数。

如果您尝试这样做:

    SavingsAccount saver1 = new SavingsAccount(2000, .03);
    saver1.calculateMonthlyInterest();

你仍然会收到错误,因为出于某种原因你已经使calculateMonthlyInterest静态,并取annualInterestRate,即使你已经在对象的构造函数中传递了利率。

你真正想要做的是这样的事情:

    double annualInterestRate = 0;
    double monthlyInterest = 0;
    SavingsAccount saver1 = new SavingsAccount(2000, .03);
    SavingsAccount saver2 = new SavingsAccount(4000, .03);
    monthlyInterest = saver1.calculateMonthlyInterest();
    System.out.println("Monthly Interest at 3%: " + monthlyInterest);
    monthlyInterest = saver2.calculateMonthlyInterest();
    System.out.println("Monthly Interest at 3%: " + monthlyInterest);
    // etc

将方法签名更改为:

public double calculateMonthlyInterest(){

您的其他方法和对它们的调用存在同样的问题。

答案 1 :(得分:0)

抱歉,您的 一切 搞砸了。

  • 使所有SavingsAccount方法和字段非静态。所有
  • 不要将SavingsAccount变量传递给您的方法参数,因为这没有意义。相反,在SavingsAccount实例上调用方法,而不是相反。
  • 在调用方法时,使用与定义方法相同的参数类型,传递正确的参数。
  • 阅读您的书并检查您的笔记,因为您无法猜测这些内容,尤其是在开始时。在开始编程时,您必须爬上高山,因此请使用您所拥有的资源材料。他们会帮助你克服这些高峰!