繁体   English   中英

该方法不适用于Java中的arguments

[英]The method is no applicable to the arguments in Java

我对 Java 中的对象和类相当陌生,对于新手问题,我深表歉意。 在主要方法的打印语句中,我收到错误:

Account类型中的getId(int)方法不适用于arguments()

我不太确定这里到底发生了什么,但我只是想打印 user2 的 id。 先感谢您。

我也为试图将所有代码放入一个块中而道歉,但显然我遇到了一些问题。

package problems;

import java.time.LocalDate;

public class AccountTest {

    public static void main(String[] args) {
        //User 1 with no input values
        Account user1 = new Account();
        //User 2 with the specified id and balance values
        Account user2 = new Account(1122, 20000);
        
        //Test to see if the getters and setters work
        System.out.println("The id is + " + user2.getId());
    }
    

}

//Create the class

class Account {
    private int id;
    private double balance;
    private static double annualInterestRate;
    private LocalDate dateCreated;
    
    //Object for when no values are passed
    Account() {
        id = 0;
        balance = 0;
        annualInterestRate = 4.5;
        System.out.println("There are no parameters set for the object");
    }
    //Object for when values are passed
    Account(int id, int balance) {
        this.id = id;
        this.balance = balance;
        annualInterestRate = 4.5;
        dateCreated = LocalDate.now();
        
    }
    
    //Getters
    public int getId(int id) {
        return  this.id;
    }
    public  double getBalance(double balance) {
        return this.balance;
    }
    public double getMonthlyInterest(double annualInterestRate) {
        return (annualInterestRate / 100 /12 * balance);
    }
    
    //Setters
    public void setAnnualInterestRate(double annualInterestRate) {
        this.annualInterestRate = annualInterestRate;
    }
    
    
    //Deposit and withdraw methods
    public double withdraw(double balance) {
        this.balance = balance - 2500;
        return this.balance;
    }
    public double deposit(double balance) {
        this.balance = balance + 3000;
        return this.balance;
    }
    
    //Get the date for when the account was made
    public long getDate(long dateCreated) {
        return dateCreated;
    }
    
}

顾名思义,当你做一个 getter 时,你会得到一个值,所以当它所做的只是返回私有变量的值时,给它一个参数是没有意义的。

例子:

private String myVar;

public String getMyVar()
{
  return this.myVar;
}

public void setMyVar(String theValueIWantMyVar)
{
  this.myVar = theValueIWantMyVar;
}

我建议你读一点,看看 object 是如何工作的

https://dev.to/codegym_cc/18-best-java-books-for-beginners-in-2019-fme

暂无
暂无

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

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