【发布时间】:2014-05-09 06:53:26
【问题描述】:
我有接口可同步:
public interface Syncable{
public void sync(Syncable other);
}
还有两个实现的类:
public class TstA implements Syncable{
@Override
public void sync(Syncable other) {
// TODO Auto-generated method stub
}
}
public class TstB implements Syncable{
@Override
public void sync(Syncable other) {
// TODO Auto-generated method stub
}
}
所以这段代码是合法的:
TstA a = new TstA();
TstB b = new TstB();
a.sync(b);
这不是我想要的。我想允许 a.sync 接收 TstA 的实例,而 b.sync 接收 TstB 的实例。
可能我必须使用泛型,但是如何使用?
【问题讨论】:
标签: java generics methods interface restriction