2014-09-26 17 views
-5

我想用另一个类的方法时遇到了一些麻烦。带两个参数的构造函数Java

所以我必须从我的班级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; 
    } 

} 

我在做什么错在这里?

+4

你已经注意到你自己的构造函数有两个参数,但你已经通过了0. – FatalError 2014-09-26 17:01:44

+0

你有没有试过在类中定义一个无参构造函数? – IdusOrtus 2014-09-26 17:03:59

回答

1

如果要初始化一个空的AccountType对象,则y你需要添加一个没有参数的默认构造函数。

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); 
+0

这是我的任务:银行系统现在准备处理不同类型的账户 ,如储蓄账户,信用账户,工资账户等。为了处理关于不同账户类型的信息 ,需要添加新的账户类型AccountType到 项目。此类应具有两个属性:一个字符串,用于保存帐户类型和利率(所有给定类型的帐户具有相同的利率 费率)的名称。编写这个类,包括一个构造函数,其类型和利率为 参数和访问器方法的属性。 – user2984388 2014-09-26 17:14:53

+1

@ user2984388然后我敢打赌你确实需要两个参数的构造函数,而不是空的,并且类型和速率应该在外部提供。 – Claudio 2014-09-26 19:34:04

2

只需通过一个“typeName的”和“的InterestRate”通过参数构造函数

AccountType type = new AccountType("savings", 0.012f); 
+0

但是如果我想在interest类中设置interestRate而不是在这里呢? – user2984388 2014-09-26 17:03:14

+0

我的意思是说让accountType InterestRate是2.0,并且将永远是这个..我不想一次又一次地输入2.0? – user2984388 2014-09-26 17:04:23

+0

@ user2984388然后,不要提供两个参数的构造函数,其中由客户端来配置利率,但不变的AccountType。 – Claudio 2014-09-26 17:09:40

0

您需要的时候调用构造函数来设置参数:

AccountType acct = new AccountType("Type R", 1.0f); 
0

添加这一个你AccountType类

public AccountType(){ 
} 
+0

然后,我将能够显示,预定义的属性,而不是一次又一次地设置它们? – user2984388 2014-09-26 17:06:31