【问题标题】:How can I use methods from a different class in Java?如何在 Java 中使用来自不同类的方法?
【发布时间】:2015-04-30 04:12:32
【问题描述】:

我尝试搜索这个问题,但找不到任何真正帮助我的东西..

我有一个 Java 文件,其中包含一个银行帐户类,其中包含存款、取款、更改名称、收取服务费和打印帐户摘要的方法。这个文件叫做 Account.java

当我尝试运行该程序时,我收到一条消息,提示在文件中找不到主要方法。

那么我还有另一个名为 ManageAccount.java 的文件,它应该使用 Account 类来创建和管理 2 个不同的银行账户。该文件只有说明(以评论形式)和我的教授包括的只有 3 行代码:

    public class ManageAccounts { 
    public static void main(String[] args){ 
    Account acct1, acct2; 

我对如何将这两个文件链接在一起感到困惑。在 ManageAccount 文件中,我在开头添加了这两行:

    package Account; 
    import Account.*; 

我该怎么办?如何在 ManageAccounts 类中使用 Account 类的提款、存款、changeName、serviceFee 和 printSummary 方法?

【问题讨论】:

  • 如果是静态方法,通过类调用:OtherClass.callMethod()',如果是实例方法,通过实例调用:OtherClass o = new OtherClass(); o.callMethod();'
  • 请发布您的相关代码。
  • no main methods found in the file:我假设,你尝试开始类Account没有main方法。
  • 是的,Account 没有 main 方法。

标签: java class methods banking


【解决方案1】:

首先看一下oop的基础知识。在你的 main 方法中,通过 new 创建两个新的 account 实例。

  public static void main(String[] args){ 
    Account acct1 = new Account(1000,"Sally",1111); 
    Account acct2 = new Account(1000,"Barry",1112);
    acct1.depositTo(2000);

}

在您的变量 acct1 和 acct2 中,您有两个 Account 类实例。帐户类是您的实例的某种形状。在实例上,您可以调用您定义的方法。如果你想运行你的程序,你必须在定义了 main 方法的类上运行它。

【讨论】:

  • 我用我应该做的所有存款和取款修复了我的 ManageAccounts 文件。但我收到此错误:ManageAccounts.java:12: error: cannot find symbol Account acct1 = new Account(1000, "Sally", 1111); ^ 符号:类帐户位置:类 ManageAccounts
【解决方案2】:

为了能够启动 Java 程序,您需要一些代码来启动。这是主要的方法。这是一个外观示例。

public static void main(String[] args) {

}

当然,这个 main 方法什么都不做。所以你需要在方法中插入你的代码。您还需要记住,为了能够调用您需要创建要使用的类的实例的方法(除非该方法是静态的)查看http://docs.oracle.com/javase/tutorial/java/concepts/ 以了解基础知识。

【讨论】:

    【解决方案3】:

    由于您没有发布所有代码和错误消息,因此很难回答这个问题。 为了让A类能够使用B类的方法,需要在A类项目的构建路径中设置B类。对您来说最简单的方法是将它们放在同一个包中,这样它们就对彼此可见(假设该方法不是私有的。

    【讨论】:

      【解决方案4】:

      我认为你对基础知识本身并不了解,所以首先阅读 OPPS 的概念并开始使用,这样你就可以避免将来出错。要访问其他类中的方法,创建实例并可以使用它访问相应的方法.请检查以下示例以帮助您理解..

      public class Account {
      int id;
      Date dateCreated;
      double balance, annualInteretRate;
      // Other fields
      
      public Account() {
      // Here is where you create a default account.
      }
      
      public void setID(int i) {
      id = i;
      }
      
      public int getID() {
      return id;
      }
      
      // Method that checks to see if balance is sufficient for withdrawal.
      // If so, reduces balance by amount; if not, prints message.
      public void withdraw(double amount)
      {
      if (balance >= amount)
      {
      balance -= amount;
      }
      else
      {
      System.out.println("Insufficient funds");
      }
      }
      
      // Method that adds deposit amount to balance.
      public void deposit(double amount)
      {
      balance += amount;
      }
      //-----------------------------------…
      // Returns balance.
      //-----------------------------------…
      public double getBalance()
      {
      return balance;
      }
      // Adds interest to the account and returns the new balance.
      /
      public double addInterest ()
      {
      balance += (balance * RATE);
      return balance;
      }
      

      /// 主类

      import java.util.Scanner;
      
      public class BankProgram {
      public static void main(String args[]) {
      Account acct1 = new Account();
      acct1.setID(1122);
      acct1.setBalance(20000);
      acct1.setAnnualInterestRate(4.5);
      System.out.print("\nDepositing $3000 into account, balance is now ");
      acct1.deposit(3000);
      System.out.println(acct.getBalance());
      System.out.print("\nWithdrawing $2500, balance is now ");
      acct1.withdraw(2500);
      System.out.println(acct.getBalance());
      }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多