0

我在返回键入的列表时遇到了困难。出于某种原因,它作为原始列表返回,尽管我真的很想得到键入的列表。

它与类抽象有关。通过在类上添加/删除 abstract 关键字,我的 List 从 raw 变为 typed,反之亦然。似乎因为抽象而贬低了我的泛型?

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();       
    }
}

我该如何解决这个问题,为什么会这样。我尝试了 Java 泛型常见问题解答等。

更新代码; FacebookMes​​senger服务

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())
        {
            // ..
        }       
    }
}

信使服务

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

信使服务

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

Facebook活动邀请通知

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

Facebook通知

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);
    }
}

注意

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;
    }
}

我注意到

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

活动邀请

public class EventInvitation extends Trigger {
    private Event event;


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


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

}

扳机

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 回答