JAVA里有很多这样的接口-able,我想模仿这个自己定义一个接口
public interface eatable{} ——此接口下没有任何方法定义
然后我希望实现这个功能:
public apple implement eatable{ ...... }
user.eat(New apple()); //correctorpublic pen{ ...... }
user.eat(New pen()); //not implement eatable, throw NoEatException.怎么实现不实现 eatable 的话,每次eat都抛异常?
public interface eatable{} ——此接口下没有任何方法定义
然后我希望实现这个功能:
public apple implement eatable{ ...... }
user.eat(New apple()); //correctorpublic pen{ ...... }
user.eat(New pen()); //not implement eatable, throw NoEatException.怎么实现不实现 eatable 的话,每次eat都抛异常?
不能吃你还喂它
Thread(Runnable target)
Allocates a new Thread object.
http://blog.csdn.net/Slugfest/archive/2008/01/10/2034359.aspx
如果需要在编译期告知,可以采用泛型参数。如果需要运行时抛出异常采用:
if(!(arg instanceof apple)) {
throw new NoEatException();
}
throw new NoEatException();
}需要在编译期报错,eat方法采用泛型方法:public static <M extends Eatable> void eat(M arg);不实现Eatable的话,做不到,因为eat并不知道哪些能吃哪些不能吃的。
和
C#泛型的where constraints一样 都是那么尴尬