java - Methods for an interface with a variable number of parameters -
i'd ask if it's possible in java have declaration of method in interface but, want methods defined have variable number of input parameters (for example, of same type). thinking in this:
public interface equalscriteria { public boolean isequal(string... paramstocheck); // not equals(object obj) !!! }
and 1 class implements equal criteria like, example:
public class commonequals implements equalscriteria { private string name; private string surname; .... @override public boolean isequal(string othername, string othersurname) { return name.equals(othername) && surname.equals(othersurname); } }
but maybe, want other criteria in part of code, this
public class specialequals implements equalscriteria { .... @override public boolean isequal(string othername, string othersurname, string passport) { return name.equals(othername) && surname.equals(othersurname) && passport.equals(passport); } }
ps: problem little bit more complicated, useful me.
a better way be:
public interface equalscriteria { public boolean isequal(equalscriteria other); public string[] getparam(); // not equals(object obj) !!! }
and then:
public class commonequals implements equalscriteria { private string name; private string surname; @override public boolean isequal(equalscriteria other) { if(other==null) return false; return arrays.aslist(getparam()).equals(arrays.aslist(other.getparam())); } }
this way, if number of strings changes, still work.
Comments
Post a Comment