|
| 1 | +package beginner; |
| 2 | + |
| 3 | +import static com.sandwich.koan.constant.KoanConstants.__; |
| 4 | +import static com.sandwich.util.Assert.assertEquals; |
| 5 | + |
| 6 | +import com.sandwich.koan.Koan; |
| 7 | + |
| 8 | +@SuppressWarnings("unused") |
| 9 | +public class AboutCasting { |
| 10 | + |
| 11 | + @Koan |
| 12 | + public void longPlusInt() { |
| 13 | + int a = 6; |
| 14 | + long b = 10; |
| 15 | + Object c = a + b; |
| 16 | + assertEquals(c, __); |
| 17 | + assertEquals(c instanceof Integer, __); |
| 18 | + assertEquals(c instanceof Long, __); |
| 19 | + } |
| 20 | + |
| 21 | + @Koan |
| 22 | + public void forceIntTypecast() { |
| 23 | + long a = 2147483648L; |
| 24 | + // What happens if we force a long value into an int? |
| 25 | + int b = (int)a; |
| 26 | + assertEquals(b, __); |
| 27 | + } |
| 28 | + |
| 29 | + @Koan |
| 30 | + public void implicitTypecast() { |
| 31 | + int a = 1; |
| 32 | + int b = Integer.MAX_VALUE; |
| 33 | + long c = a + b; |
| 34 | + assertEquals(c, __); |
| 35 | + } |
| 36 | + |
| 37 | + class Parent { public String doThing() { return "parent style"; } } |
| 38 | + class Child extends Parent { public String doThing() { return "child style"; } } |
| 39 | + |
| 40 | + @Koan |
| 41 | + public void downCastWithInerhitance() { |
| 42 | + Child child = new Child(); |
| 43 | + Parent parent = child; // Why isn't there an explicit cast? |
| 44 | + assertEquals(parent instanceof Parent,__); |
| 45 | + assertEquals(child instanceof Parent,__); |
| 46 | + assertEquals(child instanceof Child,__); |
| 47 | + } |
| 48 | + |
| 49 | + @Koan |
| 50 | + public void downCastAndPolymophism() { |
| 51 | + Child child = new Child(); |
| 52 | + Parent parent = child; |
| 53 | + // Think about the result. Did you expect that? Why? |
| 54 | + // Think about inheritance, objects, classes and instances. |
| 55 | + assertEquals(parent.doThing(),__); |
| 56 | + assertEquals(child.doThing(),__); |
| 57 | + } |
| 58 | + |
| 59 | + @Koan |
| 60 | + public void upCastWithInheritance() { |
| 61 | + Parent parent = new Child(); |
| 62 | + Child child = (Child)parent; // Why do we need an explicit cast here? |
| 63 | + assertEquals(parent instanceof Parent,__); |
| 64 | + assertEquals(child instanceof Parent,__); |
| 65 | + assertEquals(child instanceof Child,__); |
| 66 | + } |
| 67 | + |
| 68 | + @Koan |
| 69 | + public void upCastAndPolymophism() { |
| 70 | + Parent parent = new Child(); |
| 71 | + Child child = (Child)parent; |
| 72 | + // Think about the result. Did you expect that? Why? |
| 73 | + // How is that different from above? |
| 74 | + assertEquals(parent.doThing(),__); |
| 75 | + assertEquals(child.doThing(),__); |
| 76 | + } |
| 77 | + |
| 78 | + class SuperChild extends Child { |
| 79 | + public String doSuperChildThing() { return "Superchild"; } |
| 80 | + } |
| 81 | + |
| 82 | + @Koan |
| 83 | + public void complicatedCast() { |
| 84 | + Parent parent = new SuperChild(); |
| 85 | + // What do you need to do in order to call "doSuperChildThing"? |
| 86 | + assertEquals("Superchild", __); |
| 87 | + } |
| 88 | + |
| 89 | + interface Sleepable { |
| 90 | + String sleep(); |
| 91 | + } |
| 92 | + |
| 93 | + class SleepChild extends Child implements Sleepable { |
| 94 | + public String sleep() { return "zzzz"; } |
| 95 | + } |
| 96 | + |
| 97 | + @Koan |
| 98 | + public void complicatedCastWithInterface() { |
| 99 | + Parent parent = new SleepChild(); |
| 100 | + // What do you need to do in order to call "sleep"? |
| 101 | + assertEquals("zzzz", __); |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | +} |
0 commit comments