第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使泛型函數(shù)根據(jù) Java 中的輸入獲取對象屬性

如何使泛型函數(shù)根據(jù) Java 中的輸入獲取對象屬性

繁華開滿天機 2023-05-17 14:41:45
我有如下的 Java 實體@Entity@Table(name = "user", schema = "xxxx")public class User extends AuditableBase<Long> {  /** The Constant serialVersionUID. */  private static final long serialVersionUID = -5827659402853250569L;  public static final String FTL = "FTL";  /** The refer number. */  @Column(name = "reference_number")  private String referenceNumber;  /** The customer id. */  @OneToOne(fetch = FetchType.LAZY)  @JoinColumn(name = "customer_id")  @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)  private Customer customer;  /** The load Notes. */  @OneToMany(cascade = CascadeType.ALL, mappedBy = "load")  @JsonBackReference  private Set<Notes> notes = new HashSet<>();  /** The customer location. */  @OneToOne(fetch = FetchType.LAZY)  @JoinColumn(name = "customer_location_id")  @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)  private CustomerLocation customerLocation;  /** The invoice number. */  @Column(name = "invoice_number")  private String invoiceNumber;  // Lot going  /**   * Instantiates a new User.   */  public User() {    super();  }  /**   * Instantiates a new User.   *   * @param id   *          the id   */  public User(Long id) {    super(id);  }}我在實體內(nèi)部有很多孩子,我從 DAO 調(diào)用中獲取對象User user = userRepository.findById(userId);現(xiàn)在我想根據(jù)一些引用 Map 的 if 條件獲取一些用戶詳細(xì)信息。Map<Integer, String> cc = new HashMap<Integer, String>();cc.put(1, "getCustomer()");cc.put(2, "getNotes()");cc.put(3, "getCustomerLocation()");cc.put(4, "getReferenceNumber()");for (Entry<Integer, String> map : cc.entrySet()) {    user.map.getvalue();}我需要創(chuàng)建一個通用方法來根據(jù)地圖獲取用戶對象。我怎樣才能做到這一點。
查看完整描述

1 回答

?
牧羊人nacy

TA貢獻(xiàn)1862條經(jīng)驗 獲得超7個贊

不確定我是否完全理解你的問題,但你可以這樣做:


        Map<Integer,Method> cc = new HashMap<>();

        cc.put(1, User.class.getDeclaredMethod("getCustomer"));

        cc.put(2, User.class.getDeclaredMethod("getNotes"));

        cc.put(3, User.class.getDeclaredMethod("getCustomerLocation"));

        cc.put(4, User.class.getDeclaredMethod("getReferenceNumber"));


        for (Entry<Integer, Method> map : cc.entrySet()) {

            Integer index = map.getKey();

            Method getter = map.getValue();

            Object value = getter.invoke(user);

            doSomethingUsefulWith(index, value);

        }

更新:


你可以Getter像這樣聲明一個接口:


public interface Getter<T,R> {

    public R get(T obj);

}

然后做這樣的事情:


    Map<Integer,Getter<User,?>> cc = new HashMap<>();

    cc.put(1, (u) -> u.getCustomer().getName());

    cc.put(2, (u) -> u.getNotes());

    cc.put(3, (u) -> u.getCustomerLocation());

    cc.put(4, (u) -> u.getReferenceNumber());

    for (Entry<Integer,Getter<User,?>> map : cc.entrySet()) {

        Integer index = map.getKey();

        Getter<User,?> getter = map.getValue();

        Object value = getter.get(user);

        doSomethingUsefulWith(index, value);

    }


查看完整回答
反對 回復(fù) 2023-05-17
  • 1 回答
  • 0 關(guān)注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號