简体   繁体   English

获取原始类型值的方法

[英]Method for getting value for primitive types

II am having so much trouble getting this method: A method in SmallBank named printBankDetailsthat prints out the details of the stored BankAccounts.我在获取此方法时遇到了很多麻烦:SmallBank 中名为 printBankDetails 的方法打印出存储的 BankAccounts 的详细信息。 This method should work in the case that there is only one BankAccount as well as in the case that there are two BankAccounts.此方法适用于只有一个 BankAccount 的情况以及有两个 BankAccounts 的情况。 You can check whether there is a value in the account2 instance variable as follows.您可以通过如下方式检查 account2 实例变量中是否有值。 I have no idea how to get the value of the primitive types I created, account1 and account2 then print that value.我不知道如何获取我创建的原始类型 account1 和 account2 的值,然后打印该值。 Any suggestions?有什么建议?

public class SmallBank
{
    private BankAccount account1;
    private BankAccount account2;

    public SmallBank(String name, int balance)
    {
        account1 = new BankAccount(name, balance);
        account2 = null;
    }

    public void addSecondAccount(BankAccount newAccount)
    {
        account2 = newAccount;
    }

    public void printBlankDetails()
    {
        account1.getDeclaredFields();
        String name = field.getName();
        Int balance = field.getBalance();
        System.out.println(b);

    }
}

First of all, account1 and account2 are NOT primitive types.首先, account1account2不是原始类型。

Also, you can access both quite simply as below.此外,您可以非常简单地访问两者,如下所示。

public void printBlankDetails()
{
    String name1 =  account1.getName();


    String name2 =  account2.getName();

}

ps there are many issues with your printBlankDetails function, so I didnt attempt to replicate it. ps 你的printBlankDetails函数有很多问题,所以我没有尝试复制它。

You could declare BankAccount as:您可以将 BankAccount 声明为:

class BankAccount {

    private int balance;
    private String name;

    public BankAccount (String name, int balance) {
        this.name = name;
        this.balance = balance;
    }

    public String getName () {
        return name;
    }

    public int getBalance () {
        return balance;
    }

}

Then you call the methods in SmallBank class:然后调用 SmallBank 类中的方法:

public void printBankDetails () {
    System.out.println("Account 1:");
    System.out.println("Name: " + account1.getName());
    System.out.println("Balance: " + account1.getBalance());
}

An alternate design is to have say printDetails() in the BankAccount class itself.另一种设计是在BankAccount类本身中说printDetails() Something like:就像是:

public class BankAccount {
  String name;
  int balance;
  ...
  ...

  public String printDetails() {
      return (name + "/" + balance);
  }
}

and then use it in your SmallBank::printBlankDetails() method as follows:然后在您的SmallBank::printBlankDetails()方法中使用它,如下所示:

printBlankDetails() {
   if(account1 != null) system.out.println(account1.printDetails());
   if(account2 != null) system.out.println(account2.printDetails());
}

IMO, this approach provides better encapsulation, hides the details of BankAccount from SmallBank class. IMO,这种方法提供了更好的封装,从SmallBank类中隐藏了BankAccount的详细信息。

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

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