I have somewhat new to java and am trying to make a class/ mehtod that returns an instance type I have made the class in obj-c to demonstrate what I am trying to do, as I am more familiar with obj-c. And now I am tryin to make something similar in java, but am having some trouble I am not asking for a translation, just some guidance in a common mistake I might be making. Here is my obj-c code and then I post what I have tried in java.
- (instancetype)time:(NSString)weekday hour:(NSInteger)hour minute:(NSInteger)minute {
self = [super init];
if (self != nil) {
timeFromWeek = weekday * 24 * 60 + hour * 60 + minute;
}
return self;
}
And here is what I am trying in java
public static class SomeContainer<E>
{
E createContents(Class<E> clazz) throws IllegalAccessException, InstantiationException {
return clazz.newInstance();
}
}
I am not sure how to pass in my parameters?
I have tried a bunch of things and I don't want to clog up this post with all of them, but will post if you think will help.
So I am just wondering how I pass the parameters in like in the obj-c code.
Thanks for the help :)
Something like this
public static class SomeContainer<E>
{
E createContents(Class<E> clazz, Object[] parameters) throws IllegalAccessException, InstantiationException {
Class[] clzes = new Class[parameters.length];
for (int i = 0; i < parameters.length; i++) {
clzes[i] = parameters[i].getClass();
}
Constructor<E> con = clazz.getConstructor(clzes);
return con.newInstance(parameters);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.