我维护了2个具有相同功能的项目,我正在将这个功能整合到一个公共项目中.我定义了一个接口:
public interface GraphData
{
public List<? extends ShapeData> getShapes();
public void setShapes( List<? extends ShapeData> shapes );
}
Run Code Online (Sandbox Code Playgroud)
我在两个项目中实现了这个接口:
public class Graph implements GraphData
{
public List<Shape> shapes = new ArrayList<Shape>();
public List<? extends ShapeData> getShapes()
{
return shapes;
}
public void setShapes( List<? extends ShapeData> shapes )
{
this.shapes = shapes;
}
}
Run Code Online (Sandbox Code Playgroud)
Shape是一个子类型ShapeData.当我编译这个类,我得到关于铸造的错误List<Shape>,以List<? of...我如何解决这个编译错误?也许更好的问题是,我应该使用有界通配符定义我的接口方法(即?extends)?
class Animal{}
class Dog extends Animal{}
class Cat extends Animal{}
public class Mixer<A extends Animal>{
public <C extends Cat> Mixer<? super Dog> useMe(A a, C c){
//return new Mixer<Object>();//KO
return new Mixer<Animal>(); //OK
}
}
Run Code Online (Sandbox Code Playgroud)
返回参数是Mixer<? super Dog>如此,如果是使用较低的有界通配符定义的
当我返回a Mixer<Object>并且没有编译器错误时,为什么我有编译器错误Mixer<Animal>?
我有一个BaseDao接口,使用hibernate将数据持久化到mysql
public interface BaseDao<T extends Serializable> {
public void saveAll(Collection<T> objects) throws PersistenceException;
}
Run Code Online (Sandbox Code Playgroud)
我有一个实体类hirechy,他们的基类是BaseActivity(所有扩展可序列化)其中之一:
public class Activity1 extends BaseACtivity{...}
Run Code Online (Sandbox Code Playgroud)
在某些班级我宣布ActivityDao:
protected abstract ActivityDao<Activity1> createActivityDao1();
Run Code Online (Sandbox Code Playgroud)
和方法:
private void persistData(ActivityDao<? extends BaseActivity> activityDao, Collection<? extends BaseActivity> data){
EntityTransaction transaction = activityDao.getEntityManager().getTransaction();
try {
transaction.begin();
activityDao.saveAll(data);
transaction.commit();
}
catch (HibernateException ex) {
Run Code Online (Sandbox Code Playgroud)
at activityDao.saveAll(data)我得到一个编译时异常:
Error:(62, 24) java: method saveAll in interface com.matomy.persistence.dao.BaseDao<T>
cannot be applied to given types;
required: java.util.Collection<capture#1 of ? extends
com.matomy.persistence.entity.activities.BaseActivity>
found: java.util.Collection<capture#2 of ? extends com.matomy.persistence.entity.activities.BaseActivity>
reason: actual …Run Code Online (Sandbox Code Playgroud) 我有一个对象定义如下:
protected Map<String, ? extends List<? extends LightGeometry>> geoms=new HashMap<String,List<LightGeometry>>();
Run Code Online (Sandbox Code Playgroud)
我尝试插入一个看起来符合通配符的对象
ArrayList<LightGeometry> points=new ArrayList<LightGeometry>();
points.add((LightGeometry)new LightPoint(pt));
geoms.put("point", points);
Run Code Online (Sandbox Code Playgroud)
编译器抛出一个错误:
put(String, capture#18-of ? extends List<? extends LightGeometry>类型中的方法)Map<String,capture#18-of ? extends List<? extends LightGeometry>>不适用于参数(String, ArrayList<LightGeometry>)
我错过了什么?
编辑:为什么我使用通配类型的通配符
它基本上归结为能够将列表(我通过服务获得)分配给geoms另一个类中的对象,而无需筛选列表以进行转换.
public void onSuccess(Map<String, ArrayList<LightPolygon>> result) {
// TODO Auto-generated method stub
GWT.log("" + result.size());
Log.debug("" + result.size());
long startTime = System.currentTimeMillis();
if (overlay != null) {
overlay.setData(result);
overlay.update();
Log.debug("time to add features: "
+ (System.currentTimeMillis() - startTime));
}
} …Run Code Online (Sandbox Code Playgroud) 我正在进行Java OCP考试,我遇到了这个问题
给出以下代码:
Transaction t1 = new Transaction<>(1, 2); //1
Transaction t2 = new Transaction<>(1, "2"); //2
Run Code Online (Sandbox Code Playgroud)
要求// 1必须编译,// 2必须不编译.类Transaction的以下哪个声明会满足请求?
答案是这样的:
public class Transaction<T, S extends T> {
public Transaction(T t, S s) {
}
}
Run Code Online (Sandbox Code Playgroud)
我明白了 但是,当我将此代码放入Eclipse项目时,它可以工作!这是我的代码:
class TransactionD<T, S extends T> {
public TransactionD(T t, S s){
System.out.println("D: t: " + t.getClass() + " s: " + s.getClass());
}
}
public class Test1
{
public static void main(String[] args) {
TransactionD d1 = new TransactionD<>(1, 2); //1
TransactionD …Run Code Online (Sandbox Code Playgroud) 拥有一组抽象对象: Set<Foo> foes;
我想要一个像这样的方法:
List<? extends Foo> getFoesByType(TypeEnum type);
Run Code Online (Sandbox Code Playgroud)
我试过了:
List<? extends Foo> result = new ArrayList<>();
for(Foo f : foes) {
if(f.getType() == type) {
switch(type) {
case TYPE1:
f = (FooType1) f;
break;
case TYPE2:
/*...*/
}
result.add(f);
/*The method add(capture#1-of ?) in the type
List<capture#1-of ?> is not applicable for the arguments (Foo)*/
}
}
return result;
Run Code Online (Sandbox Code Playgroud)
但是我收到了一个错误.
我希望能够做到这一点:List<FooType1> foesType1 = getFooesByType(TypeEnum.TYPE1);哪种方法是正确的?