-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAboutInnerClasses.java
More file actions
157 lines (131 loc) · 4.39 KB
/
AboutInnerClasses.java
File metadata and controls
157 lines (131 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package intermediate;
import com.sandwich.koan.Koan;
import static com.sandwich.koan.constant.KoanConstants.__;
import static com.sandwich.util.Assert.assertEquals;
public class AboutInnerClasses {
interface Ignoreable {
String ignoreAll();
}
class Inner {
public String doStuff() {
return "stuff";
}
public int returnOuter() {
return x;
}
}
@Koan
public void creatingInnerClassInstance() {
Inner someObject = new Inner();
assertEquals(someObject.doStuff(),"stuff");
}
@Koan
public void creatingInnerClassInstanceWithOtherSyntax() {
AboutInnerClasses.Inner someObject = this.new Inner();
assertEquals(someObject.doStuff(), "stuff");
}
private int x = 10;
@Koan
public void accessingOuterClassMembers() {
Inner someObject = new Inner();
assertEquals(someObject.returnOuter(),10);
}
@Koan
public void innerClassesInMethods() {
class MethodInnerClass {
int oneHundred() {
return 100;
}
}
assertEquals(new MethodInnerClass().oneHundred(), 100);
// Where can you use this class?
}
class AnotherInnerClass {
int thousand() {
return 1000;
}
AnotherInnerClass crazyReturn() {
class SpecialInnerClass extends AnotherInnerClass {
int thousand() {
return 2000;
}
}
;
return new SpecialInnerClass();
}
}
@Koan
public void innerClassesInMethodsThatEscape() {
AnotherInnerClass ic = new AnotherInnerClass();
assertEquals(ic.thousand(), 1000);
AnotherInnerClass theCrazyIC = ic.crazyReturn();
assertEquals(theCrazyIC.thousand(), 2000);
}
int theAnswer() {
return 42;
}
@Koan
public void creatingAnonymousInnerClasses() {
AboutInnerClasses anonymous = new AboutInnerClasses() {
int theAnswer() {
return 23;
}
};// <- Why do you need a semicolon here?
assertEquals(anonymous.theAnswer(), 23);
}
@Koan
public void creatingAnonymousInnerClassesToImplementInterface() {
class bd implements Ignoreable {
public String ignoreAll() {
return "SomeInterestingString";
}
}
Ignoreable ignoreable = new bd();
// public String ignoreAll() {
// return null;
// }
// }; // Complete the code so that the statement below is correct.
// Look at the koan above for inspiration
assertEquals(ignoreable.ignoreAll(), "SomeInterestingString");
// Did you just created an object of an interface type?
// Or did you create a class that implemented this interface and
// an object of that type?
}
@Koan
public void innerClassAndInheritance() {
Inner someObject = new Inner();
// The statement below is obvious...
// Try to change the 'Inner' below to "AboutInnerClasses'
// Why do you get an error?
// What does that imply for inner classes and inheritance?
assertEquals(someObject instanceof Inner, true);
}
class OtherInner extends AboutInnerClasses {
}
@Koan
public void innerClassAndInheritanceOther() {
OtherInner someObject = new OtherInner();
// What do you expect here?
// Compare this result with the last koan. What does that mean?
assertEquals(someObject instanceof AboutInnerClasses, true);
}
static class StaticInnerClass {
public int importantNumber() {
return 3;
}
}
@Koan
public void staticInnerClass() {
StaticInnerClass someObject = new StaticInnerClass();
assertEquals(someObject.importantNumber(), 3);
// What happens if you try to access 'x' or 'theAnswer' from the outer class?
// What does this mean for static inner classes?
// Try to create a sub package of this package which is named 'StaticInnerClass'
// Does it work? Why not?
}
@Koan
public void staticInnerClassFullyQualified() {
AboutInnerClasses.StaticInnerClass someObject = new AboutInnerClasses.StaticInnerClass();
assertEquals(someObject.importantNumber(), 3);
}
}