Case code:
1 PackageCom.xt.thins_15_3;2 3 ImportJava.util.Iterator;4 5 /**6 * Generic interface7 * 8 * @authorXue9 * Ten * @param<T> One */ A InterfaceGeneric<t> { - PublicT Next (); - } the - /** - * Fibonacci (an integer sequence), common class implementations - * + * @authorXue - * + */ A classFibonacciImplementsGeneric<integer> { at - protected Static intCount = 0; - - @Override - PublicInteger Next () { - //TODO auto-generated Method Stub in returnFIB (count++); - } to + Public intFibintN) { - if(N < 2) the return1; * returnFIB (n-2) + fib (n-1); $ }Panax Notoginseng } - the /** + * Iterative Adapter Defepo Nachi (an integer sequence) A * the * @authorXue + * - */ $ classIterablefibonacciextendsFibonacciImplementsIterable<integer> { $ - Private intsize; - the PublicIterablefibonacci (intsize) { - This. Size =size;WuyiCount = 0; the } - Wu @Override - PublicIterator<integer>iterator () { About //TODO auto-generated Method Stub $ return NewIterator<integer>() { - - @Override - Public voidRemove () { A //TODO auto-generated Method Stub + the } - $ @Override the PublicInteger Next () { the //TODO auto-generated Method Stub thesize--; the returnIterablefibonacci. This. Next (); - } in the @Override the Public BooleanHasnext () { About //TODO auto-generated Method Stub the returnSize > 0; the } the }; + } - the }Bayi the Public classFibonaccitest { the Public Static voidMain (string[] args) { -Fibonacci fib =NewFibonacci (); - for(inti = 0; I < 20; i++) { theSystem.out.print (Fib.next () + ","); the } theSystem.out.println ("\ n------------------------------"); theIterablefibonacci IFIB =NewIterablefibonacci (20); - for(Integer integer:ifib) { theSystem.out.print (integer + ","); the } the }94}
Java generic interface