java - Who to put methods in a array to use? -
i wonder if there way create methods array make call array in place of method in example below create array stores user objects in class there getname method, call method array, follows example example:
public class javaapplication48 { public static void main(string[] args) { user[] u = new user[] {new user ("1", "john"), new user ("2", "tereza"), new user ("3", "tobias")}; //there put methods .getid() , .getname in array, dont know //... //and concatenating users array methods array system.out.println(u[0]ac[1]); //to print on console "john" } } class user { private string id; private string name; public user(string id, string name) { this.id = id; this.name = name; } public string getid() { return id; } public string getname() { return name; } }
you can pull off using lambda expressions:
public static void main(string[] args) { user[] u = new user[] {new user ("1", "john"), new user ("2", "tereza"), new user ("3", "tobias")}; function<user, string> f1 = user -> user.getname(); function<user, string> f2 = user -> user.getid(); function<user, string>[] f = new function[] { f1, f2 }; system.out.println(f[0].apply(u[0])); }
(i don't understand want achieve this, technically way go.)
the functions declared have method "apply" declared in interface, final call see in system.out-line is
functionreference.apply(inputobject)
Comments
Post a Comment