|
| 1 | +package intermediate; |
| 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 | +interface Ignorable { |
| 9 | + String ignoreAll(); |
| 10 | +} |
| 11 | + |
| 12 | +public class AboutInnerClasses { |
| 13 | + |
| 14 | + class Inner { |
| 15 | + public String doStuff() { return "stuff"; } |
| 16 | + public int returnOuter() { return x; } |
| 17 | + } |
| 18 | + |
| 19 | + @Koan |
| 20 | + public void creatingInnerClassInstance() { |
| 21 | + Inner someObject = new Inner(); |
| 22 | + assertEquals(someObject.doStuff(),__); |
| 23 | + } |
| 24 | + |
| 25 | + @Koan |
| 26 | + public void creatingInnerClassInstanceWithOtherSyntax() { |
| 27 | + AboutInnerClasses.Inner someObject = this.new Inner(); |
| 28 | + assertEquals(someObject.doStuff(),__); |
| 29 | + } |
| 30 | + |
| 31 | + private int x = 10; |
| 32 | + |
| 33 | + @Koan |
| 34 | + public void accessingOuterClassMembers() { |
| 35 | + Inner someObject = new Inner(); |
| 36 | + assertEquals(someObject.returnOuter(),__); |
| 37 | + } |
| 38 | + |
| 39 | + @Koan |
| 40 | + public void innerClassesInMethods() { |
| 41 | + class MethodInnerClass { |
| 42 | + int oneHundred() { return 100; } |
| 43 | + }; // <- Why do you need a semicolon here? |
| 44 | + assertEquals(new MethodInnerClass().oneHundred(), __); |
| 45 | + // Where can you use this class? |
| 46 | + } |
| 47 | + |
| 48 | + class AnotherInnerClass { |
| 49 | + int thousand() { return 1000; } |
| 50 | + |
| 51 | + AnotherInnerClass crazyReturn() { |
| 52 | + class SpecialInnerClass extends AnotherInnerClass { |
| 53 | + int thousand() { |
| 54 | + return 2000; |
| 55 | + } |
| 56 | + }; |
| 57 | + return new SpecialInnerClass(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Koan |
| 62 | + public void innerClassesInMethodsThatEscape() { |
| 63 | + AnotherInnerClass ic = new AnotherInnerClass(); |
| 64 | + assertEquals(ic.thousand(), __); |
| 65 | + AnotherInnerClass theCrazyIC = ic.crazyReturn(); |
| 66 | + assertEquals(theCrazyIC.thousand(), __); |
| 67 | + } |
| 68 | + |
| 69 | + int theAnswer() { return 42; } |
| 70 | + |
| 71 | + @Koan |
| 72 | + public void creatingAnonymousInnerClasses() { |
| 73 | + AboutInnerClasses anonymous = new AboutInnerClasses() { |
| 74 | + int theAnswer() { return 23; } |
| 75 | + }; |
| 76 | + assertEquals(anonymous.theAnswer(), __); |
| 77 | + } |
| 78 | + |
| 79 | + @Koan |
| 80 | + public void creatingAnonymousInnerClassesToImplementIntercae() { |
| 81 | + Ignorable ignorable = null; // Complete the code so that the statement below is correct. |
| 82 | + // Look at the koan above for inspiration |
| 83 | + assertEquals(ignorable.ignoreAll(), "SomeInterestingString"); |
| 84 | + // Did you just created an object of an iterface type? |
| 85 | + // Or did you create a class that implemented this interface and |
| 86 | + // an object of that type? |
| 87 | + } |
| 88 | + |
| 89 | + @Koan |
| 90 | + public void innerClassAndIneritance() { |
| 91 | + Inner someObject = new Inner(); |
| 92 | + // The statement below is obvious... |
| 93 | + // Try to change the 'Inner' below to "AboutInnerClasses' |
| 94 | + // Why do you get an error? |
| 95 | + // What does that imply for inner classes and inheritance? |
| 96 | + assertEquals(someObject instanceof Inner, __); |
| 97 | + } |
| 98 | + |
| 99 | + class OtherInner extends AboutInnerClasses { } |
| 100 | + |
| 101 | + @Koan |
| 102 | + public void innerClassAndIneritanceOther() { |
| 103 | + OtherInner someObject = new OtherInner(); |
| 104 | + // What do you expect here? |
| 105 | + // Complare this result with the last koan. What does that mean? |
| 106 | + assertEquals(someObject instanceof AboutInnerClasses, __); |
| 107 | + } |
| 108 | + |
| 109 | + static class StaticInnerClass { |
| 110 | + public int importantNumber() { return 3; } |
| 111 | + } |
| 112 | + |
| 113 | + @Koan |
| 114 | + public void staticInnerClass() { |
| 115 | + StaticInnerClass someObject = new StaticInnerClass(); |
| 116 | + assertEquals(someObject.importantNumber(), __); |
| 117 | + // What happens if you try to access 'x' or 'theAnswer' from the outer class? |
| 118 | + // What does this mean for static inner classes? |
| 119 | + // Try to create a sub package of this package which is named 'StaticInnerClass' |
| 120 | + // Does it work? Why not? |
| 121 | + } |
| 122 | + |
| 123 | + @Koan |
| 124 | + public void staticInnerClassFullyQualified() { |
| 125 | + AboutInnerClasses.StaticInnerClass someObject = new AboutInnerClasses.StaticInnerClass(); |
| 126 | + assertEquals(someObject.importantNumber(), __); |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments