当我想使用另一个类的方法时,我遇到了一些麻烦。
所以我必须在我的类AccountType中向控制台打印interestRate和帐户类型, 在我的班级客户和我已经用我称之为BankAccount类的余额做得很好。
BankAccount balance = new BankAccount();
并且从只有一个参数的构造函数调用,但现在我想调用带有两个参数的构造函数,我得到一个错误。
public void printAccountInformation()
{
BankAccount balance = new BankAccount(); // This work fine
AccountType rate = new AccountType(); // but here i get error because the constructor in the account type takes two arguments
AccountType type = new AccountType(); // but here i get error because the constructor in the account type takes two arguments
System.out.println("Greetings dear: " + name);
System.out.println("Your street: " + street);
System.out.println("Your town " + town);
System.out.println("Your postalCode " + postalCode);
System.out.println("Your PhoneNumber " + telephoneNumber);
System.out.println("User balance: " + balance.getBalance());
System.out.println("User Account type: " + type.getAccountType());
System.out.println("Your InterestRate Is: " + rate.getInterestRate());
}
这是我的AccountType类
public class AccountType
{
// Attributes
private String accountNameType;
private float interestRate = 0.0f;
// Constructor
public AccountType(String types, float rate)
{
accountNameType = types;
interestRate = rate;
}
// accessor
public String getAccountType()
{
return accountNameType;
}
public float getInterestRate()
{
return interestRate;
}
}
我在这里做错了什么?
答案 0 :(得分:2)
只需传递" typeName"和" interestRate" by参数到你的构造函数
AccountType type = new AccountType("savings", 0.012f);
答案 1 :(得分:1)
如果要初始化一个空的AccountType对象,则需要添加一个无参数的默认构造函数。
public AccountType() { }
如果需要在初始化对象后设置类变量,则需要添加适当的setter方法。
public void setAccountType(String accountNameType){
this.accountNameType = accountNameType;
}
public void setInterestRate(float interestRate){
this.interestRate = interestRate;
}
所以在你的主要班级。
AccountType accountType = new AccountType();
accountType.setAccountType("savings");
accountType.setInterestRate(10.0);
答案 2 :(得分:0)
调用构造函数时需要设置参数:
AccountType acct = new AccountType("Type R", 1.0f);
答案 3 :(得分:0)
将此项添加到您的AccountType类
public AccountType(){
}