ไม่สามารถใส่ T ใน Map <String,? ขยาย T>

Aug 27 2020
private static final Map<String, ? extends TrackingInterface<TrackResponseObject>> map = new HashMap<>();
    static {
        map.put("dunzo", new DunzoShipment());
        map.put("self", new SelfShipment());
    }
public class DunzoShipment implements TrackingInterface<TrackResponseObject>,java.util.function.Supplier<TrackingInterface<TrackResponseObject>> { 
//body
}

ขณะวางวัตถุลงในแผนที่ฉันได้รับข้อผิดพลาด:

วิธีการใส่ (String จับ # 1-of? ขยาย TrackingInterface) ในประเภท Map <String, capture # 1-of? ขยาย TrackingInterface> ไม่สามารถใช้ได้กับอาร์กิวเมนต์ (String, DunzoShipment)

คำตอบ

2 Michael Aug 27 2020 at 10:24
? extends TrackingInterface

หมายถึง " สิ่งที่ขยายออกไปTrackingInterface" เราไม่รู้ว่าสิ่งนั้นคืออะไรดังนั้นเราจึงไม่สามารถแน่ใจได้ว่าDunzoShipmentเป็นหนึ่งในนั้น นั่นเป็นเหตุผลที่คุณไม่สามารถแทรกลงในแผนที่ได้

เต้า: ผลิตขยายผู้บริโภคสุด นี่คือผู้บริโภค (ผ่านput) extendsดังนั้นคุณจะไม่สามารถใช้

เพียงแค่ลบ? extendsส่วนและมันจะคอมไพล์