【问题标题】:Why are my constructors recognized as methods?为什么我的构造函数被识别为方法?
【发布时间】:2014-11-07 22:36:26
【问题描述】:

我正在开发一个将银行账户作为对象保存的程序。这些帐户有一个利率、余额、ID 和创建日期数据。在我所做的事情中,据我了解,默认余额、id 和利息为 0。默认情况下,利率未定义。我正在学习的这本书显示了一个无参数构造函数是用“Circle() {}”完成的。我在帐户类中使用了“account() {}”。当我在 jGRASP 中运行程序时,我的两个构造函数都收到错误“方法声明无效;需要返回类型”。它认识到我打算将构造函数作为方法。我需要了解什么才能使我的构造函数不被识别为方法?

在运行第一个构造函数时,我知道我们使用默认值创建了一个名为 account 的 Account 对象。当我们运行第二个构造函数时,我们将 account 对象的值更改为指定的值

public class Bank{

public static void main(String[] args){

Account account = new Account(1122, 20000);

account.setAnnualInterestRate(4.5);

account.withdraw(2500);

account.deposit(3000);


   System.out.println("Balance is " + account.getBalance());

   System.out.println("Monthly interest is " + account.getMonthlyInterest());

   System.out.println("This account was created at " + account.getDateCreated());

                                     }

                             }


  class Account {

             private int id = 0;

             private double balance = 0;

             private double annualInterestRate = 0;

             private String dateCreated;



              account(){

              }

             account(int newID, double newBalance){

             id = newID;

             balance = newBalance;

             }

        //accessor for ID
        public int getID(){
        return id;
        }
        //acessor for balance
        public double getBalance(){
        return balance;
        }
        //accessor for interest rate
        public double getAnnualInterest(){
        return annualInterestRate;
        }
        //mutator for ID
        public void setID(int IDset){
        id = IDset;
        }
        //mutator for balance
        public void setBalance(int BalanceSet){
        balance = BalanceSet;
        }
        //mutator for annual interest
        public void setAnnualInterestRate(double InterestSet){
        annualInterestRate = InterestSet;
          }
        //accessor for date created
        public String getDateCreated(){
        return dateCreated;
        }
        //method that converts annual interest into monthly interest and returns the value
        public double getMonthlyInterest(){
        double x =  annualInterestRate / 12;
        return x;
          }
       //method that witdraws from account
       public double withdraw(double w){
       balance -= w;
       return balance;
       }
       //method that deposits into account
       public double deposite(double d){
       balance += d;
       return balance;
       }




  }

【问题讨论】:

    标签: java class object constructor


    【解决方案1】:

    构造函数名称必须以区分大小写的方式匹配类名称。在Account 类中,更改

    account(){
    

    Account(){
    

    对于你的其他构造函数也是如此。

    【讨论】:

      【解决方案2】:

      您需要在两个构造函数中将 a 大写。 Java 区分大小写。

                Account(){
      
                }
      
               Account(int newID, double newBalance){
      
                   id = newID;
      
                   balance = newBalance;
      
               }
      

      否则,Java 将其视为没有返回类型的方法。请记住,构造函数没有或不需要返回类型。

      【讨论】:

      • 编辑:构造函数不能有返回类型
      【解决方案3】:

      构造函数应该是驼峰化的(作为约定,与类名相同)并且它只返回自身的类型 看我的例子:)

      Account() {
       return;
      }
      

      【讨论】:

        猜你喜欢
        • 2021-07-23
        • 1970-01-01
        • 2021-08-12
        • 1970-01-01
        • 2019-10-23
        • 2018-02-28
        • 1970-01-01
        • 2020-11-03
        • 2013-08-27
        相关资源
        最近更新 更多