Learn from teacher Wang Generics (iv): Define generic interfaces and implementations

Source: Internet
Author: User

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 publicinterface Generator<T> {    publicT 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 publicclass Main {     public static void main(String[] args) {        FruitGenerator generator = newFruitGenerator();        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 publicclass 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 publicclass FruitGenerator implements Generator{     @Override    public Object next() {        // TODO Auto-generated method stub        returnnull;    }}
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.