Re: Generics and subclasses
- From: "andreas kinell" <news@xxxxxxxxx>
- Date: Mon, 31 Oct 2005 18:17:26 +0100
"EdwardH" <edwardh@N:O:S:P:A:M:edward.dyndns.org> schrieb im Newsbeitrag
news:pmP8f.149348$dP1.507244@xxxxxxxxxxxxxxxxxx
>I have two classes, Manager and Data. I use them as follows...
>
> Class Manager
> {
> private Vector<Data1> dataVector;
>
> add()
> {
> dataVector.add( new Data1(blabla) );
> }
> }
>
>
> What I want to do is use the same Manager class use several Data-typed
> classes, Data1, Data2 and Data3. They all have the same Interface but work
> differently.
>
> Unfortunately Manager doesn't want to work with Interfaces (DataInterface,
> for example) because of the "new".
>
> Does anyone have any suggestions as to how to make Manager more flexible?
I think what you want is this:
Class Manager
{
private Vector<DataInterface> dataVector;
add(){
dataVector.add( new Data1(blabla) );
}
}
class Data1 implements DataInterface{
}
notice: the dataVector accepts objects of type DataInterface. When you
actually add data, you will create a Data1 object (with "new") but it will
be of type DataInterface as well.
You seem to have overlooked that you can use Interface types like this:
DataInterface myData = new Data1();
HTH,
andreas
.
- Prev by Date: Re: Java Object Persistence (JSP & Java)
- Next by Date: Re: Generics - good, bad or indifferent?
- Previous by thread: Re: Generics and subclasses
- Next by thread: display a grid with struts/tiles?
- Index(es):
Relevant Pages
|
Loading