Created
May 19, 2017 02:39
-
-
Save kamekoopa/c2b9c4ab740799fd5398ec9962d8864d to your computer and use it in GitHub Desktop.
型クラス.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TypeClass { | |
interface Addable<T> { | |
T add(T a, T b); | |
} | |
public static class IntAdd implements Addable<Integer> { | |
@Override | |
public Integer add(Integer a, Integer b) { | |
return a + b; | |
} | |
} | |
public static class StringAdd implements Addable<String> { | |
@Override | |
public String add(String a, String b) { | |
return a + b; | |
} | |
} | |
public static void main(String[] args) { | |
List<Integer> integerList = Arrays.asList(1 ,2, 3); | |
List<String> stringList = Arrays.asList("a", "b", "c"); | |
Integer result1 = addAll(integerList, 0, new IntAdd()); | |
String result2 = addAll(stringList, "", new StringAdd()); | |
} | |
public static <T> T addAll(List<T> list, T init, Addable<T> addable) { | |
T result = init; | |
for(T x : list){ | |
result = addable.add(result, x); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment