Create function without knowing class name

konart

Here is what I'm trying to do:

I have this piece of code:

    Object selectedCreditId = null;
    int i = 0;
    String selectedCreditName = leasing.getCredit().getDisplayName();
    Collection<DCredRating> selectCreditIds = creditData.getItemIds();
    for (DCredRating id: selectCreditIds) {
        String creditItem = id.getDisplayName();
        if (selectedCreditName.equals(creditItem)) {
            selectedCreditId = creditData.getIdByIndex(i);
        }
            i++;
    }

I want to create function using this code. The function will take leasing class and creditData BeanItemContainer as parameters and return selectedCreditId object.

In other words something like this:

private Object getSelectedCreditId(LeasingResP param, BeanItemContainer<...> param2) {
    Object selectedCreditId = null;
    int i = 0;
    LeasingResP leasing = param;
    BeanItemContainer<...> creditData = param2;
    String selectedCreditName = leasing.getCredit().getDisplayName();
    Collection<...> selectCreditIds = creditData.getItemIds();
    for (... id: selectCreditIds) {
        String creditItem = id.getDisplayName();
        if (selectedCreditName.equals(creditItem)) {
            selectedCreditId = creditData.getIdByIndex(i);
        }
            i++;
    }
}

The problem here is that creditData container can be of different class.

Is there any "pretty" way of doing this, or I'll have to write an if block(s) for different classes?

Seelenvirtuose

If you are really not interested on the type of beans that are collected in the container you could declare the method as following:

private Object getSelectedCreditId(LeasingResP param, BeanItemContainer<?> param2) {
    ...
}

The "?" means container of unknown type, so you only know about your beans that they are of type Object.

You could also make the method generic, so that it returns the correct type:

private <T extends DCredRating> T getSelectedCreditId(LeasingResP param, BeanItemContainer<? extends T> param2) {
    ...
}

A usage for that second variant would be (an example):

LeasingResP param = ...
BeanItemContainer<DCredRating> container = ...
DCredRating dcr = getSelectedCreditId(param, container);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

bind first argument of function without knowing its arity

来自分类Dev

Knowing the original class if casted to an object class

来自分类Dev

using function in name space without back-slash

来自分类Dev

是否未定义对class_name <template_argument> function_name(变量)的引用?

来自分类Dev

Sending the name of a function to a function

来自分类Dev

$(。'class-name')。click(function(){...})停止跨浏览器功能吗?

来自分类Dev

How do I get property value without knowing it's specific key in javascript using JSON

来自分类Dev

How to parse 'div' without name?

来自分类Dev

Create a method attribute in a class

来自分类Dev

Python/Selenium - find_elements_by_class_name with long class name

来自分类Dev

外键class_name

来自分类Dev

外键class_name

来自分类Dev

如果 __name__==function_name()

来自分类Dev

<function>不是<class>的成员

来自分类Dev

(新的Class())-> Function(); 意思是?

来自分类Dev

CREATE AGGREGATE FUNCTION的特权

来自分类Dev

function_name()() 中的语法

来自分类Dev

create an instance of class in python 2.7

来自分类Dev

Altering the definition of a function without an assignment

来自分类Dev

Domain Name without "www" does not work for CloudFront

来自分类Dev

Rails routing short urls without controller name

来自分类Dev

I need to create a Javascript Function to create custom push function

来自分类Dev

hide div where class name with jquery

来自分类Dev

Rails:获取belongs_to class_name

来自分类Dev

如何从按钮中找到td class = name?

来自分类Dev

Swift – "Cannot find object class with name"

来自分类Dev

Show / Hide divs individually with same class name

来自分类Dev

Java set string as class name to run

来自分类Dev

CheerioJS, looping through <ul> with same class name

Related 相关文章

热门标签

归档