【问题标题】:Accesing getId from an ArrayList from within a HashMap从 HashMap 中的 ArrayList 访问 getId
【发布时间】:2018-05-15 07:37:11
【问题描述】:

我创建了一个 HashMap,其中键作为类,值作为抽象类的 ArrayList,我希望从该抽象类元素中获取 ID 或名称。 但是,当从 ABSTRACT CLASS 访问元素时,它只返回抽象类中的内容,而不是扩展抽象类的类中的方法。 有没有办法在不更改我的抽象类以包含这些字段的情况下获得扩展抽象类的方法的入口。

private HashMap<Persoana,ArrayList<Account>>Unu; 
    for (Entry<Persoana, ArrayList<Account>> entry : Unu.entrySet()) {
        if (entry.getValue().get(i).getClass().???) {
            System.out.println("found"+entry.getKey() + "/" + entry.getValue());
            return entry.getValue();
        }
        i++;
        System.out.println(entry.getValue()); 
    }
    return null;
}

我的抽象类:

public abstract class Account  extends Observable{
    public abstract void retragere(int sumaDeRetras);
    public abstract void depunere(int sumaDeDepunere);

}

一个扩展我的抽象类的类和我希望在我的 main 中访问的方法。

   public class SavingsAccount extends Account {
        private long suma;
        private Date dataRetragere ;
        private Date dataDepunere;
        private int interest=5;
        private int IBAN;
        private int idClient;
        private long watchedSum;
public int getIBAN() {
        return IBAN;
    }}

【问题讨论】:

  • 解决方案可以取决于您需要做什么?为什么 Account 首先没有 IBAN?
  • 因为我使用账户类型作为指导。(储蓄或支出)并且 IBAN 在其中一种类型中是唯一的,而不是在这两种类型中(例如,我可以有一个支出账户IBAN 1212 和具有相同 IBAN 的储蓄账户,这将是两件不同的事情)或者至少我是这么想的,我不确定自己是否会实施。

标签: java arraylist hashmap abstract-class


【解决方案1】:

您基本上可以使用instanceof 检查具体类,然后强制转换实例。所以在你的情况下看起来像:

private HashMap<Persoana,ArrayList<Account>>Unu; 

for (Entry<Persoana, ArrayList<Account>> entry : Unu.entrySet()) {
    for (Account account : entry.getValue() {
        if (account instanceof SavingsAccount) {
            SavingsAccount savingsAccount = (SavingsAccount) account;

            System.out.println("found "+savingsAccount.getIBAN());
        }
    }
}

【讨论】:

  • 我会尝试 is.But,我不是在寻找 SavingsAccount 的实例。一个人可以拥有多个具有不同 IBAN 的储蓄账户。我正在搜索具有特定 IBAN 的帐户(来自输入)。
  • 但是您可以将输入的 IBAN 与 SavingAccount 的 IBAN 进行比较,还是我错过了问题?
【解决方案2】:

你可以向下转型,然后像这样调用方法:

Account accountValue = entry.getValue();
if(accountValue instanceOf SavingsAccount) {
    SavingsAccount savingAccountValue = (SavingsAccount)accountValue;
    System.out.println(savingAccountValue.getIBAN());
}

【讨论】:

  • 你不能强制转换然后检查实例,没有意义,你必须以其他方式进行
【解决方案3】:

如果您知道具体类,请使用上述答案中的强制转换。如果你只知道具体的方法,你可以使用下面的反射:

for (Entry<Persoana, ArrayList<Account>> entry : Unu.entrySet()) {
 for (Account a : entry.getValue()) {
  System.out.println(a.getClass().getSimpleName()); // Prints the name of the concrete class.
  try {
    System.out.println(a.getClass().getMethod("getIBAN").invoke(a));
  } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
        e.printStackTrace();
  }
 }
}

如果要列出类的所有方法,也可以使用反射:

...
a.getClass().getMethods();
...

【讨论】:

  • 大家好,谁能告诉我,为什么答案是错误的?
  • 在这里使用反射来解决设计问题似乎有点矫枉过正。
  • 感谢您的回答。我只想添加一个“通用方式”,如何从“未知”类中获取/调用方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-30
  • 1970-01-01
相关资源
最近更新 更多