Defining generic interfaces and their implementations
Keynote Teacher: Wang Shaohua QQ Group: 483773664 Learning Goals
1. Generic interface
2, the implementation of the three types of generic interface
First, define the interface
123 |
public interface Generator<T> { public T next(); } |
Second, the realization of this interface
12345678910 |
public class fruitgenerator implements generator<string> { Private string[] Fruits = New string[]{ "Apple" , "Banana" , "Pear" }; @Override Public String next () { Random rand = new random (); return Fruits[rand.nextint ( 3 )]; } } |
Third, call
12345678910 |
public class Main { public static void main(String[] args) { FruitGenerator generator = new FruitGenerator(); System.out.println(generator.next()); System.out.println(generator.next()); System.out.println(generator.next()); System.out.println(generator.next()); } } |
Iv. three ways to implement the interface
123456789 |
public class FruitGenerator<T> implements Generator<T>{ @Override public T next() { // TODO Auto-generated method stub return null ; } } |
123456789 |
public Class fruitgenerator implements generator<string>{ @Override Public String Next () { //TODO auto-generated method Stub return null ; } } |
12345678 |
public class FruitGenerator implements Generator{ @Override public Object next() { // TODO Auto-generated method stub return null ; } } |
Five, learning video URL:
Http://edu.51cto.com/course/course_id-6083.html
From for notes (Wiz)
Learn from teacher Wang Generics (iv): Define generic interfaces and implementations