Пересылка переменных параметров в другую функцию в Java, как в C ++ - PullRequest
0 голосов
/ 29 июня 2018

Возможно ли выполнить пересылку параметров в Java, как в C ++:

template <typename ...Params>
void f(Params&&... params)
{
    y(std::forward<Params>(params)...);
}

например. в ситуации, когда я хочу иметь одну заводскую функцию:

public static MyClass create( < parameter pack > )
{
    return new MyClass(< forward >);
}

с несколькими конструкторами?

Стоит отметить, что пакет параметров может содержать параметры разных типов.

1 Ответ

0 голосов
/ 29 июня 2018

Повторное редактирование:

Стоит отметить, что пакет параметров может содержать параметры разных типов.

Это полностью меняет вопрос. Отражение - единственный способ сделать это. Вот пример примера отражения снизу, но получающий типы параметров из фактических классов переданных значений параметров ( живой пример ):

import java.lang.reflect.Constructor;
import java.util.Arrays;

class Example
{
    public Example(String a) {
        System.out.println("(String): " + a);
        // ...
    }

    public Example(int a, String b) {
        System.out.println("(int, String): " + a + ", " + b);
        // ...
    }

    public Example(String a, String b) {
        System.out.println("(String, String): " + a + ", " + b);
        // ...
    }

    static Example create(Object... params) {
        try {
            Class[] paramTypes = new Class[params.length];
            for (int n = 0; n < params.length; ++n) {
                Class cls = params[n].getClass();
                if (cls.equals(Integer.class)) { // You may need this (for int and other primitives)
                    paramTypes[n] = Integer.TYPE;
                } else {
                    paramTypes[n] = cls;
                }
            }
            Constructor ctor = Example.class.getConstructor(paramTypes);
            return (Example)ctor.newInstance(params);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace(System.out);
            throw new IllegalArgumentException("parameters not supported");
        }
    }

    public static void main(String[] args)
    {
        Example e1 = Example.create("foo");
        Example e2 = Example.create(1, "foo");
        Example e3 = Example.create("foo", "bar");
    }
}

Очевидно, это очень простой, неполный пример, но, надеюсь, он поможет вам идти по правильному пути. Подробнее:


Оригинальный ответ:

Вариантные аргументы в Java являются синтаксическим сахаром для массивов. Везде, где вы можете предоставить список переменных аргументов, вы можете вместо этого предоставить массив:

public static MyClass create(SomeType... args)
{
    return new MyClass(args);
}

Вот полный пример ( работоспособная копия ):

import java.util.Arrays;

class Example
{
    Example(String... strings) {
        System.out.println("strings = " + Arrays.toString(strings));
        // Obviously, in the normal case, a constructor actually does something
        // with the parameters...
    }

    static Example create(String... strings) {
        return new Example(strings);
    }

    public static void main(String[] args)
    {
        Example e = Example.create("a", "b", "c");
    }
}

с несколькими конструкторами

Если вы имеете в виду несколько конструкторов без переменных параметров, вам придется ветвиться или использовать отражение; В Java нет встроенного механизма, кроме рефлексии, который может это сделать. (Я понимаю, что механизм C ++ для этого делает, что круто.) Что может послужить аргументом для их объединения и создания одного вариадического конструктора inadad.

Ветвление:

class Example
{
    Example(int a) {
        System.out.println("one param: " + a);
        // ...
    }

    Example(int a, int b) {
        System.out.println("two params: " + a + ", " + b);
        // ...
    }

    Example(int a, int b, int c) {
        System.out.println("three params: " + a + ", " + b + ", " + c);
        // ...
    }

    static Example create(int... ints) {
        switch (ints.length) {
            case 1:
                return new Example(ints[0]);
            case 2:
                return new Example(ints[0], ints[1]);
            case 3:
                return new Example(ints[0], ints[1], ints[2]);
            default:
                throw new IllegalArgumentException("Only 1-3 ints allowed");
        }
    }

    public static void main (String[] args)
    {
        Example e = Example.create(1, 2, 3);
    }
}

Отражение ( живой пример ):

import java.lang.reflect.Constructor;
import java.util.Arrays;

class Example
{
    public Example(String a) {
        System.out.println("one param: " + a);
        // ...
    }

    public Example(String a, String b) {
        System.out.println("two params: " + a + ", " + b);
        // ...
    }

    public Example(String a, String b, String c) {
        System.out.println("three params: " + a + ", " + b + ", " + c);
        // ...
    }

    static Example create(String... strings) {
        try {
            Class[] paramTypes = new Class[strings.length];
            Arrays.fill(paramTypes, String.class);
            Constructor ctor = Example.class.getConstructor(paramTypes);
            return (Example)ctor.newInstance((Object[])strings);
        } catch (Exception e) {
            throw new IllegalArgumentException(strings.length + " strings not supported");
        }
    }

    public static void main (String[] args)
    {
        Example e = Example.create("a", "b", "c");
    }
}
...