今天碰到了这么个问题:
把一个链表反向。已经给了如下代码:public class ReverseList {
interface NodeList {
int getItem();
NodeList nextNode();
}
void reverse(NodeList node) {
} public static void main(String[] args) {
}}我没懂一个interface能作为一个方法(reverse)的参数吗?在这个方法里如何实现这些interface定好的contract?谢谢。
把一个链表反向。已经给了如下代码:public class ReverseList {
interface NodeList {
int getItem();
NodeList nextNode();
}
void reverse(NodeList node) {
} public static void main(String[] args) {
}}我没懂一个interface能作为一个方法(reverse)的参数吗?在这个方法里如何实现这些interface定好的contract?谢谢。
接口类型的参数 当然是它子类的示例
这里可以使用匿名类 在匿名类内实现两个方法
reverse(new NodeList(){ public int getItem() {
// TODO Auto-generated method stub
return 0;
} public NodeList nextNode() {
// TODO Auto-generated method stub
return null;
} });不过这段代码如果要写在 main方法中 reverse方法必须定义为static
原来还有内部interface的概念的.