Home > Mobile >  Use getter as parameter in method
Use getter as parameter in method

Time:03-15

I have multiple different classes Class1, Class2, Class3. Each class has different variables and getters for them.

public class Class1 {
 private String var1_1;
 private String var1_2;
 private String var1_3;

 public String getVar1_1() { return var1_1;}
 public String getVar1_2() { return var1_2;}
 public String getVar1_3() { return var1_3;}
}

public class Class2 {
 private String var2_1;
 private String var2_2;
 private String var2_3;

 public String getVar2_1() { return var2_1;}
 public String getVar2_2() { return var2_2;}
 public String getVar2_3() { return var2_3;}
}

public class Class3 {
 private String var3_1;
 private String var3_2;
 private String var3_3;

 public String getVar3_1() { return var3_1;}
 public String getVar3_2() { return var3_2;}
 public String getVar3_3() { return var3_3;}
}

How can I write method which takes List of objects and getters(functions) as a method parameters? For example:

List<Class1> list1 = //doesn't matter
List<Class2> list2 = //doesn't matter
List<Class3> list3 = //doesn't matter
generateRows(list1, Class1::getVar1_1, Class1::getVar1_3);
generateRows(list2, Class2::getVar2_1, Class2::getVar2_2, Class2::getVar2_3);
generateRows(list3, Class3::getVar3_1, Class3::getVar3_2);

Method

public void generateRows(List<T> list, /*???Something???*/... getters) {
 for(T object: list) {
  /*How to use getters to print?*/
  /*System.out.println(obj.firtsGetter())*/
 }
}

What I should write instead of ???Something???. Something like Function<T, String> or Consumer<T>? And how can I use getters in method?

CodePudding user response:

You need to wrap the method calls in a Function. A Consumer, or another FunctionalInterface might do the trick as well, depending on your needs.

Then you need to have your method accept varargs of this function.

  public static <T> void generateRows(List<T> list, Function<T, String>... getters) {
    for (T object : list) {
      for (Function<T, String> getter : getters) {
        System.out.println(getter.apply(object));
      }
    }
  }

Function will accept argument of type T and return a String. T can be anything - Class1, Class2, etc., as long you can return a string from this object - that means to have a getter method returning String in your case.

CodePudding user response:

First you can defined a interface eg:

 interface  Get{
    String getVar1();
     String getVar2();
     String getVar3();
 }

Second All of classes implemente it:

 class Class2 implements Get{
    private String var2_1;
    private String var2_2;
    private String var2_3;

    public String getVar1() { return var2_1;}
    public String getVar2() { return var2_2;}
    public String getVar3() { return var2_3;}
}

Third:

   public void generateRows(List<T> list, Get ...getters) {
        for(T object: list) {
            /*How to use getters to print?*/
            /*System.out.println(obj.firtsGetter())*/
        }
    }

CodePudding user response:

such as this?enter image description here

  • Related