我制作了一个程序来创建一个对象数组,该对象数组存储两个CheckingAccount对象和一个SavingAccount对象。我遇到了一个简单的问题,对于我的主要方法来说,我想维护一个CheckingAccount对象,但问题是构造函数似乎有错误。之前,我以相同的方式进行了不同的练习,因此我对构造函数的错误感到困惑。这是代码:
public class program4 {
public static void main(String[] args) {
BankAccount[] array = new BankAccount[3];
boolean y = true;
CheckingAccount no1 = new CheckingAccount("John","Smith","abc@aol.com",30.0,false,30.0);
}
public static class BankAccount{
private static int accountId;
private static final int TOTAL_ACCOUNT_NUMBER = accountId;
private static String firstName;
private String lastName;
private String custEmail;
public static double balance;
private Date dateCreated;
BankAccount(String firstName, String lastName, String email, double balance){
this.firstName = firstName;
this.lastName = lastName;
this.custEmail = email;
this.balance = balance;
this.dateCreated = new Date();
}
public void deposit(double amount) {
balance = balance + amount;
}
Date getDate() {
return this.dateCreated;
}
int getID() {
return accountId;
}
String getName() {
return firstName +" "+ lastName;
}
String getEmail(){
return custEmail;
}
double getBalance() {
return balance;
}
public void withdraw(double amount) {
balance =balance - amount;
}
public String toString() {
return "Account ID: " + accountId + " /nName: " + firstName+" "+lastName+"/nEmail: "+custEmail+"/nBalance: "+balance+ "\nDate Created: "+ dateCreated;
}
}
public class CheckingAccount extends BankAccount{
private boolean hasDirectDeposit;
private double minBal;
CheckingAccount(String firstName,String lastName,String email,double deposit, boolean hasDirectDeposit,double minBal){
super(firstName,lastName,email,balance);
this.hasDirectDeposit = hasDirectDeposit;
this.minBal = minBal;
}
public boolean getDirectDeposit() {
return hasDirectDeposit;
}
public void setDirectDeposit(boolean flag){
hasDirectDeposit = flag;
}
public double getMinBal() {
return minBal;
}
public void setMinBal(double bal) {
minBal = bal;
}
public String toString() {
return "Account ID: " + getID()+"/nName: "+ getName() + "/nEmail: "+ getEmail()+"/nBalance: "+ getBalance()+ "/nDate Created: "+ getDate() +
"/nDirect Deposit: " + getDirectDeposit() + "/nMinimum Balance: "+ getMinBal();
}
}
public class SavingsAccount extends BankAccount{
private double interestRate;
SavingsAccount(String firstName,String lastName,String email,double balance,double intRate){
super(firstName,lastName,email,balance);
this.interestRate = intRate;
}
public double getInterestRate() {
return interestRate;
}
public void setInterestRate(double intRate) {
interestRate =intRate;
}
public String toString() {
return "Account ID: " + getID()+"/nName: "+ getName() + "/nEmail: "+ getEmail()+"/nBalance: "+ getBalance()+ "/nDate Created: "+ getDate() +
"/nInterest Rate: "+ getInterestRate();
}
}
}
答案 0 :(得分:1)
内部类默认情况下为“实例”(与static
相反)。顶级类本质上是“静态”的(尽管试图将其标记为非法的,但实际上是,不能这样)。这些“实例内部类”可能是有用的,但是大多数情况下,它们令人困惑。我建议您忘记它们暂时存在*,并且总是用static
总是标记您的内部类,这时您的问题就会消失。
*)如果您必须知道,非static-inner的行为就像它们具有外部类类型的隐藏的final字段一样,而所有构造函数的行为就好像它们具有用于对其进行设置的隐藏参数一样。在存在正确类型的this
引用的地方对任何构造函数的任何调用都会静默插入该引用,但是,如果没有,则必须使用许多Java程序员不会使用的极其奇特的outerRef.new NonStaticInner()
语法甚至认识。换句话说:除非您真的,真的,知道自己在做什么,并且非常确定它们最适合这种情况,否则不要使用它们。