admin管理员组文章数量:1279043
I want to create a class in Java from a classname and an variable number of arguments (i.e. an Object[] args with variable length). Is there any way to achieve that?
Basically, the method would look like this in Javascript
function createClass(classname, args) {
protoObject = Object.create(window[classname].prototype);
return window[classname].apply(protoObject,args) || protoObject;
}
// I want to be able to do this:
var resultClass = createClass("someClass", someArrayOfArgs);
A simpler function to only call a function would look like
function callFunction(functionName, args) {
return window[functionName].apply(null,args);
}
Thanks!
For clarification, this would be some example usage in Javascript:
function multiplyResult(var1,var2) {
return var1*var2;
}
var result = callFunction("multiplyResult", ["5", "2"]); // == 10
function multiplyObject(var1,var2) {
var result = var1 * var2;
this.getResult = function() { return result };
}
var result = createClass("multiplyObject", ["5", "2"]).getResult(); // == 10
I want to create a class in Java from a classname and an variable number of arguments (i.e. an Object[] args with variable length). Is there any way to achieve that?
Basically, the method would look like this in Javascript
function createClass(classname, args) {
protoObject = Object.create(window[classname].prototype);
return window[classname].apply(protoObject,args) || protoObject;
}
// I want to be able to do this:
var resultClass = createClass("someClass", someArrayOfArgs);
A simpler function to only call a function would look like
function callFunction(functionName, args) {
return window[functionName].apply(null,args);
}
Thanks!
For clarification, this would be some example usage in Javascript:
function multiplyResult(var1,var2) {
return var1*var2;
}
var result = callFunction("multiplyResult", ["5", "2"]); // == 10
function multiplyObject(var1,var2) {
var result = var1 * var2;
this.getResult = function() { return result };
}
var result = createClass("multiplyObject", ["5", "2"]).getResult(); // == 10
Share
Improve this question
edited Dec 15, 2011 at 13:54
Willem Mulder
asked Dec 15, 2011 at 13:27
Willem MulderWillem Mulder
14k4 gold badges41 silver badges64 bronze badges
4
- Which exactly are you looking for? 1) The ability to use varargs in Java, or 2) The ability to invoke a method via reflection given its name and an array of arguments? Your title and question body don't quite align. – Rob Hruska Commented Dec 15, 2011 at 13:39
- I am not looking for varargs. I want to apply an arbitrary list of parameters to a function. Edited my question as well :) – Willem Mulder Commented Dec 15, 2011 at 13:49
- Use a vararg with Object. Thats the only way to have Java accept any number of arguments of unknown type for a method. – Stefan Commented Dec 15, 2011 at 14:44
- Does this answer your question? Java spread operator – Krzysztof Atłasik Commented Jun 5, 2020 at 19:20
3 Answers
Reset to default 9It turns out that you can simply provide an Object[]
to the invoke()
function and that it will work exactly like .apply()
in Javascript. Take the following function.
public int multiply(int int1, int int2) {
return int1*int2;
}
From the same class, it works to call the function like
Object result = this.getClass().getDeclaredMethod("multiply",classes).invoke(this,ints);
with classes
and ints
being something like
Class[] classes = new Class[] {int.class, int.class};
Object[] ints = new Object[] {2,3};
I think you need something like this. You can use reflection the invoke a method.
Method method = Class.forName("className").getMethod("methodName", Parameter1.class, Parameter2.class);
MethodReturnType result= (MethodReturnType) method.invoke(Class.forName("className"), new Object[]{parameter1, parameter2});
It goes like this:
Class.forName("foo").clazz.getConstructor(<classes>).newInstance(... parameters)
but unlike javascript you have strong typing and have to say which constructor you like to have.
本文标签: Javascript apply() method equivalent in JavaStack Overflow
版权声明:本文标题:Javascript .apply() method equivalent in Java? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741298336a2370944.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论