은행 계좌를 오브젝트로 보유하는 프로그램을 작성 중입니다. 계정에는 금리, 잔액, ID 및 날짜 생성 데이터가 있습니다. 내가 만든 것에서 기본 균형, 이드 및이자는 내가 이해하는 것에서 0입니다. 이자율은 기본적으로 정의되지 않습니다. 내가 배우는 책은 인자가없는 생성자가 "Circle() {}"로 끝난다는 것을 보여준다. 나는 account 클래스에서 "account() {}"를 사용했다. jGRASP에서 프로그램을 실행할 때 두 생성자 모두에 대해 "잘못된 메서드 선언, 반환 유형 필요"오류가 발생합니다. 그것은 내가 생성자로 의도하는 것을 방법으로 인식하고 있습니다. 내 생성자를 메소드로 인식하지 못하게하려면 무엇을 이해해야합니까?제 생성자가 메소드로 인식되는 이유는 무엇입니까?
첫 번째 생성자를 실행할 때 나는 default 값을 가진 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;
}
}
편집 참조 자체의 형태를 돌려줍니다 : 생성자 수 없습니다 반환 형식을 – JClassic