|
| 1 | +package beginner; |
| 2 | + |
| 3 | +import com.sandwich.koan.Koan; |
| 4 | +import static com.sandwich.koan.constant.KoanConstants.__; |
| 5 | +import static com.sandwich.util.Assert.assertEquals; |
| 6 | + |
| 7 | +public class AboutMethodPreference { |
| 8 | + |
| 9 | + class A { |
| 10 | + public String doStuff(int i) { return "int"; } |
| 11 | + public String doStuff(Integer i) { return "Integer"; } |
| 12 | + public String doStuff(Object i) { return "Object"; } |
| 13 | + public String doStuff(int...i) { return "int vararg"; } |
| 14 | + } |
| 15 | + |
| 16 | + @Koan |
| 17 | + public void methodPreferenceInt() { |
| 18 | + assertEquals(new A().doStuff(1), __); |
| 19 | + } |
| 20 | + |
| 21 | + @Koan |
| 22 | + public void methodPreferenceInteger() { |
| 23 | + assertEquals(new A().doStuff(Integer.valueOf(1)), __); |
| 24 | + } |
| 25 | + |
| 26 | + @Koan |
| 27 | + public void methodPreferenceLong() { |
| 28 | + long l = 1; |
| 29 | + assertEquals(new A().doStuff(l), __); |
| 30 | + } |
| 31 | + |
| 32 | + @Koan |
| 33 | + public void methodPreferenceBoxedLong() { |
| 34 | + Long l = Long.valueOf(1); |
| 35 | + assertEquals(new A().doStuff(l), __); |
| 36 | + } |
| 37 | + |
| 38 | + @Koan |
| 39 | + public void methodPreferenceDouble() { |
| 40 | + Double l = Double.valueOf(1); |
| 41 | + assertEquals(new A().doStuff(l), __); |
| 42 | + } |
| 43 | + |
| 44 | + @Koan |
| 45 | + public void methodPreferenceMore() { |
| 46 | + // What happens if you change 'Integer' to 'Double' |
| 47 | + // Does this explain 'methodPreferenceDouble'? |
| 48 | + // Think about why this happens? |
| 49 | + assertEquals(new A().doStuff(1,Integer.valueOf(2)), __); |
| 50 | + } |
| 51 | +} |
0 commit comments