質問編集履歴

3

誤字編集

2017/05/23 01:47

投稿

pancy
pancy

スコア11

test CHANGED
@@ -1 +1 @@
1
- thisとは何をしてくれるものですか。
1
+ thisとは何をしてくれるものですか。
test CHANGED
File without changes

2

thisのある部分に//←←←を付け加え

2017/05/23 01:47

投稿

pancy
pancy

スコア11

test CHANGED
File without changes
test CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
  public ClassA(int a) {
14
14
 
15
- this.a = a;
15
+ this.a = a;//←←←1
16
16
 
17
17
  }
18
18
 
@@ -20,7 +20,7 @@
20
20
 
21
21
  public int getA() {
22
22
 
23
- return this.a;
23
+ return this.a;//←←←2
24
24
 
25
25
  }
26
26
 
@@ -34,7 +34,7 @@
34
34
 
35
35
  public ClassB(int b) {
36
36
 
37
- this.b = b;
37
+ this.b = b;//←←←3
38
38
 
39
39
  }
40
40
 
@@ -42,7 +42,7 @@
42
42
 
43
43
  public int getB() {
44
44
 
45
- return this.b;
45
+ return this.b;//←←←4
46
46
 
47
47
  }
48
48
 
@@ -98,7 +98,7 @@
98
98
 
99
99
  public ClassB(int b) {
100
100
 
101
- this.b = b;
101
+ this.b = b;//←←←5
102
102
 
103
103
  }
104
104
 
@@ -106,7 +106,7 @@
106
106
 
107
107
  public int getB() {
108
108
 
109
- return this.b;
109
+ return this.b;//←←←6
110
110
 
111
111
  }
112
112
 
@@ -138,7 +138,7 @@
138
138
 
139
139
  // ClassBのインスタンスを、与えられた引数で生成。
140
140
 
141
- this.instanceB = new ClassB(b);
141
+ this.instanceB = new ClassB(b);//←←←7
142
142
 
143
143
  }
144
144
 
@@ -150,7 +150,7 @@
150
150
 
151
151
  public int getB() {
152
152
 
153
- return this.instanceB.getB();
153
+ return this.instanceB.getB();//←←←8
154
154
 
155
155
  }
156
156
 
@@ -212,7 +212,7 @@
212
212
 
213
213
  public int getB() {
214
214
 
215
- return this.instanceB.getB();
215
+ return this.instanceB.getB();//←←←9
216
216
 
217
217
  }
218
218
 

1

コードを付け足し&編集

2017/05/23 01:40

投稿

pancy
pancy

スコア11

test CHANGED
File without changes
test CHANGED
@@ -4,13 +4,13 @@
4
4
 
5
5
  ```java
6
6
 
7
- コードclass KeishoA {
7
+ コードclass ClassA {
8
8
 
9
9
  private int a;
10
10
 
11
11
 
12
12
 
13
- public KeishoA(int a) {
13
+ public ClassA(int a) {
14
14
 
15
15
  this.a = a;
16
16
 
@@ -26,4 +26,232 @@
26
26
 
27
27
  }
28
28
 
29
+ class ClassB {
30
+
31
+ private int b;
32
+
33
+
34
+
35
+ public ClassB(int b) {
36
+
37
+ this.b = b;
38
+
39
+ }
40
+
41
+
42
+
43
+ public int getB() {
44
+
45
+ return this.b;
46
+
47
+ }
48
+
49
+ }
50
+
51
+
52
+
53
+ この二つのクラスを継承したClassABを作りたい場合を考えてみましょう。↓のようにできれば良いのですが、これはできません。
54
+
55
+
56
+
57
+ class ClassAB extends ClassA, ClassB {
58
+
59
+ ...
60
+
61
+ }
62
+
63
+
64
+
65
+ ClassBのinterfaceを作り、実装する
66
+
67
+
68
+
69
+ 次に、擬似的な多重継承のためにClassBのインタフェースを作ります。インタフェースとは、抽象メソッドしか持たない抽象クラスのようなものです(違いについては後記)。
70
+
71
+
72
+
73
+ interface InterfaceB {
74
+
75
+ public int getB();
76
+
77
+ }
78
+
79
+ 「クラスを継承する」のに対して「インタフェースを実装する」と言います。クラスを多重継承することはできませんが、インタフェースは多重実装することができます(抽象メソッドしか持たない抽象クラスを多重継承することはできませんが、インタフェースは多重実装できるという意味で抽象メソッドしか持たない抽象クラスと異なります)。
80
+
81
+
82
+
83
+ インタフェースは抽象メソッドしか持たないことが保障されているので、メソッド名の重複が起こっても、その処理の方法を必ずプログラマが記述することになります。
84
+
85
+
86
+
87
+
88
+
89
+ ClassBはInterfaceBを実装して次のようにします(implements InterfaceBが増えただけ)。
90
+
91
+
92
+
93
+ class ClassB implements InterfaceB {
94
+
95
+ private int b;
96
+
97
+
98
+
99
+ public ClassB(int b) {
100
+
101
+ this.b = b;
102
+
103
+ }
104
+
105
+
106
+
107
+ public int getB() {
108
+
109
+ return this.b;
110
+
111
+ }
112
+
113
+ }
114
+
115
+
116
+
117
+ ClassAB(ClassAとClassBを多重継承したクラス)
118
+
119
+
120
+
121
+ まず解説より先に多重継承のソースを示します。
122
+
123
+
124
+
125
+ class ClassAB extends ClassA implements InterfaceB {
126
+
127
+ // 多重継承したいClassBのインスタンスを保持する。
128
+
129
+ private ClassB instanceB;
130
+
131
+
132
+
133
+ public ClassAB(int a, int b) {
134
+
135
+ super(a);
136
+
137
+
138
+
139
+ // ClassBのインスタンスを、与えられた引数で生成。
140
+
141
+ this.instanceB = new ClassB(b);
142
+
143
+ }
144
+
145
+
146
+
147
+ // InterfaceB#getBメソッドを実装する必要がある。
148
+
149
+ // 動作の中身はinstanceB(ClassB)に完全に任せてしまう。
150
+
151
+ public int getB() {
152
+
153
+ return this.instanceB.getB();
154
+
155
+ }
156
+
157
+ }
158
+
159
+ やっていることは、
160
+
161
+
162
+
163
+ InterfaceBを実装
164
+
165
+ ClassBのインスタンスをprivateフィールド(instanceB)に保持
166
+
167
+ メソッドの中身はinstanceBに丸投げ
168
+
169
+ です。
170
+
171
+
172
+
173
+ 以下、順に解説します。
174
+
175
+
176
+
177
+
178
+
179
+ InterfaceBを実装
180
+
181
+
182
+
183
+ ClassABにはInterfaceBを実装させます。インタフェースはいくつでも実装できるので、ClassAを継承していようがInterfaceBを実装することができます。
184
+
185
+
186
+
187
+ class ClassAB extends ClassA implements InterfaceB {
188
+
189
+
190
+
191
+ ClassBのインスタンスをprivateフィールド(instanceB)に保持
192
+
193
+
194
+
195
+ 次に、多重継承したいクラス(この場合ClassB)のインスタンスを保持するprivateフィールドを作成します。
196
+
197
+
198
+
199
+ // 多重継承したいClassBのインスタンスを保持する。
200
+
201
+ private ClassB instanceB;
202
+
203
+
204
+
205
+ メソッドの中身はinstanceBに丸投げ
206
+
207
+
208
+
209
+ あとは、すべてをinstanceBに丸投げしてしまいます。例えば、ClassABで実装しなければならないgetBメソッドは
210
+
211
+
212
+
213
+ public int getB() {
214
+
215
+ return this.instanceB.getB();
216
+
217
+ }
218
+
219
+ です。instanceBに仕事をさせて、その結果を返しているだけです。この例ではInterfaceBが持っているメソッドが一つしかありませんが、もし仮に10個のメソッドがあれば、これと同じような丸投げの処理を10個書きます。
220
+
221
+
222
+
223
+
224
+
225
+ このようにすれば、InterfaceBのメソッドをすべて持つことを強制された、しかし、その動作はClassBによって決められたClassABを作ることができます。見かけ上は、ClassBを継承しているのと同じです*1。
226
+
227
+
228
+
229
+
230
+
231
+ 実行例
232
+
233
+
234
+
235
+ ClassABは次のようにして使えます。外から見ると、まるでClassBも継承されているようです。
236
+
237
+
238
+
239
+ public class MultipleInheritance {
240
+
241
+ public static void main(String[] args) {
242
+
243
+ ClassAB instanceAB = new ClassAB(100, 200);
244
+
245
+
246
+
247
+ System.out.println(instanceAB.getA()); // 100と表示
248
+
249
+ System.out.println(instanceAB.getB()); // 200と表示
250
+
251
+ }
252
+
253
+ }
254
+
255
+
256
+
29
257
  ```[参考にしたサイト](http://d.hatena.ne.jp/Kappuccino/20080812/1218513214)