content = content;
}
public function getContent() : String
{
return content;
}
}
The BinaryNode class is a very simple container for hierarchically stored information. In this
example, the content of each node is of String type but it sounds quite obvious that exactly the same
node class can easily be adapted to store any kind of value. To do that and with the knowledge acquired
so far, the problem can be solved in one of the following two ways: declaring a new node class for each
type you want to store in it or using a content of Dynamic type. The first solution is tremendously
repetitive (remember DRY: Don ??™ t Repeat Yourself!) and a real pain when the implementation just
changes a little bit. The second solution is weak because it is necessary to continually cast to a proper
type each time the getContent() is invoked, and it is always a good practice to avoid unsafe casts if not
absolutely necessary.
What is needed is a generic approach ( generics is the term used in the same context in languages like C#
and Java); this is exactly what type parameters are used for.
Pages:
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265