我有一个接口A,该类B实现.
以下通用方法有效
public static <T, U extends T> List<T> listFactory(Collection<U> source) {
return new ArrayList<T>(source);
}
Run Code Online (Sandbox Code Playgroud)
但
public static <T> List<T> listFactory(Collection<? extends T> source) {
return new ArrayList<T>(source);
}
Run Code Online (Sandbox Code Playgroud)
当我将输出定向到时,不会(编译错误,类型不匹配)
List<A> tester = listFactory(B.defaultCollectionFactory(3));
Run Code Online (Sandbox Code Playgroud)
defaultCollectionFactory(int count)静态地提供Bs 的集合,具有默认标签方案.
关于为什么会这样的任何见解?看起来普通的U和通配符正在做同样的事情.
我试图阅读和理解一些Java代码.这里是:
protected LoadTarget<? super PopulationLoadContext> createTarget(PopulationLoadContext context) {
return createTransactionalTargetGroup(RiskScoresTables.All_Tables);
}
Run Code Online (Sandbox Code Playgroud)
什么<? super PopulationLoadContext>意思?
我有类似的东西:
interface Foo<T> {
//... lines [0,45]...
/*line 46*/ <R, X super T&R> List<X> weave(R value);
//...
}
Run Code Online (Sandbox Code Playgroud)
但是IntelliJ正在报道:
有什么问题?我不允许将名字绑定到下限吗?或者我只允许R&X在上限中使用表达式?
把它改成
interface Foo<T> {
//... lines [0,45]...
/*line 46*/ <R> List<? super T&R> weave(R value);
//...
}
Run Code Online (Sandbox Code Playgroud)
产量
我有一个相对简单的问题,我试图解决.似乎没有直观的方法来做到这一点,或者我在这里遗漏了一些东西.
考虑使用此方法查找主图像,如果不存在,则返回第一张图像 -
public Image findMainImage(Collection<? extends Image> images) {
if (images == null || images.isEmpty()) return null
return images.stream()
.filter(Image::isMain)
.findFirst()
.orElse(images.iterator().next())
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误 - orElse(capture<? extends Image>) in Optional cannot be applied
任何方向都会很棒.
在Bar课堂上使用通配符类型的泛型完全跳过它们有什么好处吗?
public class Foo<T> {}
public interface Bar {
public void addFoo(Foo<?> foo);
public Foo<?> getFoo(String name);
}
Run Code Online (Sandbox Code Playgroud) 作为这个问题的后续,是否有可能编写一个方法来增加Dog一个合适的房间?(在这个例子中,它会接受Animal房间或Dog房间.)或者我是否被迫写下两种不同的方法如下?(因为类型擦除,我甚至不能依赖重载).
public class Rooms {
interface Animal {}
class Dog implements Animal {}
class Room<T> {
void add(T t) {}
}
void addDogToAnimalRoom(Room<Animal> room) {
room.add(new Dog());
}
void addDogToDogRoom(Room<Dog> room) {
room.add(new Dog());
}
}
Run Code Online (Sandbox Code Playgroud) Class<? super T> getSuperclass()
Run Code Online (Sandbox Code Playgroud)
Class类中的getSuperclass()返回一个类型为<?的Class.super T>,这意味着Super的Class类型参数可以是T或它的任何超类,现在为什么SuperClass的Class类型参数与子类的类型相同?
例如
Class<Manage>.class.getSuperclass()
Run Code Online (Sandbox Code Playgroud)
不回来
Class<Manager>
Run Code Online (Sandbox Code Playgroud)
从来没有
这有意义吗?或者我在这里遗失了什么?
我试图将一些使用(有界)通配符泛型的Java代码转换为C#.我的问题是,Java似乎允许泛型类型在与通配符一起使用时既是协变的又是逆变的.例如:
Java的:
interface IInterf { }
class Impl implements IInterf { }
interface IGeneric1<T extends Impl> {
void method1(IGeneric2<?> val);
void method1WithParam(T val);
}
interface IGeneric2<T extends Impl> {
void method2(IGeneric1<?> val);
}
abstract class Generic<T extends Impl> implements IGeneric1<T>, IGeneric2<T> {
public void method1(IGeneric2<?> val2) {
val2.method2(this);
}
}
Run Code Online (Sandbox Code Playgroud)
...作品.
C#等价(?)
interface IInterf { }
class Impl : IInterf { }
interface IGeneric1<T> where T:Impl {
//Java was:
//void method1(IGeneric2<?> val2);
void method1(IGeneric2<Impl> val);
void method1WithParam(T to);
}
interface …Run Code Online (Sandbox Code Playgroud) 我班上有两张地图(我是仿制药的新手)
private Map<Integer, Integer> aMap = new ConcurrentHashMap<Integer, Integer>();
private Map<Integer, Short> bMap = new HashMap<Integer, Short>();
Run Code Online (Sandbox Code Playgroud)
如果地图中不存在键我想获得零值.所以我制作了这个包装方法来减少输入containsKey(key)
@SuppressWarnings("unchecked")
private <T extends Number> T getValue (Map<Integer, T> map, Integer key) {
return (T) ((map.containsKey(key)) ? map.get(key) : 0);
}
Run Code Online (Sandbox Code Playgroud)
我称之为
Integer a = getValue(aMap, 15); //okay in any case
Short b = getValue(bMap, 15); //15 key does not exist
Run Code Online (Sandbox Code Playgroud)
对于第二种情况,它给了我:
ClassCastException: java.lang.Integer cannot be cast to java.lang.Short
Run Code Online (Sandbox Code Playgroud)
所以我可能需要做类似的事情: new Number(0),但是数字是抽象的.
我该如何解决?
编辑:
我的想法是在没有额外ifs的情况下进行算术运算:
Integer a = getValue(aMap, …Run Code Online (Sandbox Code Playgroud) List<? super IOException>表示列表可以包含IOException对象或任何属于super类的对象IOException。那么为什么下面的第2行不编译?
同样在第3行FileNotFoundException中也不是的super类IOException。那为什么编译呢?
List<? super IOException> myList = new ArrayList<Exception>();
myList.add(new Exception()); //Line 2 This line do not compile
myList.add(new FileNotFoundException()); //Line 3 This compiles
Run Code Online (Sandbox Code Playgroud) bounded-wildcard ×10
java ×10
generics ×9
.net ×1
casting ×1
collections ×1
covariance ×1
java-8 ×1
map ×1
nested ×1
optional ×1
pecs ×1
syntax-error ×1
wildcard ×1