0

I'm having difficulties returning a typed List. For some reason it is returned as a raw List though I'd really like to get the typed one.

It has to do with the class abstraction. By adding/removing the abstract keyword on the class, my List changes from raw to typed and vice versa. Seems like it's downcasting my generic because of the abstraction?

public abstract class Notice<T> {
    public List<Contact> contacts;

    public List<Contact> getContacts()
    {
            return this.contacts;
    }
}


public class Contact {
}

public class Service {
    public Service(Notice notice) {
        // ! Would like to use List<Contact> here, as I defined in Notice !
        List contacts = notice.getContacts();       
    }
}

How can I solve this, and why does it happen. I tried Java Generics FAQ etc.

Updated code; FacebookMessengerService

public class FacebookMessengerService extends MessengerService<FacebookNotice> {
    public void send(FacebookNotice notice)
    {
        //
        // ERROR occurs here:
        // "Type mismatch: cannot convert from element type Object to Contact"
        //
        // Also, hovering over it tells me to be just a rawtyped List instead of List<Contact>
        //

        for (Contact contact : notice.getContacts())
        {
            // ..
        }       
    }
}

MessengerService

public abstract class MessengerService<T extends Notice> implements IMessengerService<T> {
}

IMessengerService

public interface IMessengerService<T extends Notice> {
    public void send(T notice);
}

FacebookEventInvitationNotice

public class FacebookEventInvitationNotice extends FacebookNotice<EventInvitation> {    
    public FacebookEventInvitationNotice(EventInvitation trigger) {
        super(trigger);
    }
}

FacebookNotice

public abstract class FacebookNotice<T extends Trigger> extends Notice<T> {
    public static FacebookMessengerService service = new FacebookMessengerService();

    public FacebookNotice(T trigger) {
        super(trigger);
    }


    public void send()
    {
        service.send(this);
    }
}

Notice

public abstract class Notice<T extends Trigger> implements INotice<T> 
{
    public List<Contact> contacts = new ArrayList<Contact>();
    public T trigger;


    public Notice(T trigger)
    {
        this.trigger = trigger;
    }


    public void addContact(Contact contact)
    {
        this.contacts.add(contact);
    }


    public List<Contact> getContacts()
    {
        return this.contacts;
    }
}

INotice

public interface INotice<T extends Trigger> {
    public void send();
}

EventInvitation

public class EventInvitation extends Trigger {
    private Event event;


    public EventInvitation(Event event)
    {
        this.event = event;
    }


    public Event getEvent()
    {
        return this.event;
    }

}

Trigger

public class Trigger {
    public List<Notice> notices = new ArrayList<Notice>();
}
4

2 に答える 2

3

あなたは完全に一般的である必要があります:

public class Service {
    public Service(Notice<?> notice) {
        List<Contact> contacts = notice.getContacts();       
    }
}
于 2012-05-20T06:44:12.840 に答える
0

提案されているようにコードを変更することにより:

public class FacebookMessengerService extends MessengerService<FacebookNotice<?>> {
    public void send(FacebookNotice<?> notice)
    {
        for (Contact contact : notice.getContacts())
        {

        }       
    }
}

私の問題は解決されたようです!FacebookNoticeそれでも、どのように違いがわかりませんFacebookNotice<?>List<Contact>?

于 2012-05-20T08:31:22.653 に答える