interface as a method parameter in Java

前端 未结 8 1824
一个人的身影
一个人的身影 2020-11-30 20:34

I had an interview days ago and was thrown a question like this.

Q: Reverse a linked list. Following code is given:

public class ReverseList { 
    i         


        
相关标签:
8条回答
  • 2020-11-30 21:01

    This is one possible implementation:

    public class ReverseList { 
    interface NodeList {
        int getItem();
        NodeList nextNode();
    }
    
    static class Node implements NodeList {
        private int item;
        private Node next;
    
        @Override
        public int getItem() {
            return item;
        }
    
        public void setItem(int si) {
            item = si;
        }
    
        @Override
        public NodeList nextNode() {
            return this.next;
        }
    
        public void setNext(Node n) {this.next=n;}
    
    }
    
    Node reverse(NodeList head) {
        Node node = (Node) head;
        Node previous = null;
        while(node.nextNode() !=null) {
            Node tempNext = (Node) node.nextNode();
            node.setNext(previous);
            previous = node;
            node = tempNext;
        }
        node.setNext(previous);
        return node;
    
    }
    public static void main(String[] args) {
        //Initialization block
        ReverseList rl = new ReverseList();
        Node n1= new Node(); n1.setItem(1);
        Node n2=new Node(); n2.setItem(2);
        Node n3 =new Node(); n3.setItem(3);
        n1.setNext(n2); n2.setNext(n3); n3.setNext(null);
    
        //Reversing the list
        System.out.println("Before reversal");      
        System.out.println(n1.getItem() +"->" 
                        + n1.nextNode().getItem() + "->"
                        + n1.nextNode().nextNode().getItem() + "->"
                        +n1.nextNode().nextNode().nextNode());
    
    
        rl.reverse(n1);
    
        System.out.println("\nAfter reversal");
        System.out.println(n3.getItem() +"->" 
                + n3.nextNode().getItem() + "->"
                + n3.nextNode().nextNode().getItem() + "->"
                +n3.nextNode().nextNode().nextNode());
            }
    }
    

    Program output:

    Before reversal
    1->2->3->null
    
    After reversal
    3->2->1->null
    

    I am very curious to know if this problem can be solved by using an anonymous class. Any ideas?

    0 讨论(0)
  • 2020-11-30 21:07

    This is in fact one of the most common and useful ways to use an interface. The interface defines a contract, and your code can work with any class that implements the interface, without having to know the concrete class - it can even work with classes that didn't exist yet when the code was written.

    There are many examples in the Java standard API, especially in the collections framework. For example, Collections.sort() can sort anything that implements the List interface (not just ArrayList or LinkedList, though implementing your own List is uncommon) and whose contents implement the Comparable interface (not just String or the numerical wrapper classes - and having your own class implement Comparable for that purpose is quite common).

    0 讨论(0)
提交回复
热议问题