提问者:小点点

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


我正在学习Java初学者课程,我正在做我的第一个面向对象的作业。我确实了解OOP的基础知识,但我不具备将其付诸实践所需的知识。如果有人能指出任何新手的错误或给我一些建议,我将非常感激。

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

我相信这些错误是由跨类引用的方法造成的,但我不知道正确的方法。


共1个答案

匿名用户

对不起,你的一切都搞砸了。

  • 使所有SavingsAccount方法和字段非静态。全部。
  • 不要将SavingsAccount变量传递给方法参数,因为这没有意义。相反,在SavingsAccount实例上调用方法,而不是相反。
  • 调用用于定义方法的参数类型时,传入正确的参数。
  • 读你的书,检查你的笔记,因为你不能猜测这些东西,尤其是在开始的时候。这是一个很高的山,你必须攀登时,开始编程,所以使用您的资源材料的帮助,你已经有。他们会帮你越过那些高峰的!