Java Basic Learning-generics

Source: Internet
Author: User

One, generic method

1 /**2 3 Custom generics: A custom generic can be understood as a placeholder for a data type, or as a variable that is a data type. 4 5 Generic methods:6 custom formats for generic methods: modifiers < declaring custom generics > return value type function name (formal parameter list)7 The specific type of a generic on a custom generic method is the type of the passed-in parameter that is determined when the method is called .8 9  */Ten  Public classDemo1 { One      Public<T>t printf (T t) { A         returnT; -     } -      Public Static voidMain (string[] args) { theDemo1 demo1 =Newdemo1 (); -Integer integer = demo1.printf (100); -String string = demo1.printf ("wz"); - System.out.println (string); + System.out.println (integer); -     } +}

Second, generic type

1  PackageCn.itcast.generic;2 /**3 * Custom generics on a class if you create an object of that class, you do not specify a specific type of generic then the default is the object type4 * Generic specific data types that are customized on a class are specified when the object is created5  * 6  */7  Public classDemo2 {8      Public Static voidMain (string[] args) {9mylist<string > MyList =NewMyList ();TenMylist.add ("Add"); One //Mylist.add (123); A     } - } -  the classMylist<t>{ -     //You cannot create objects directly with generics directly -     //t[] arr = new T[10]; -object[] arr =NewObject[10]; +     intindex = 0; -      PublicMyList () { +          A     } at      Public voidAdd (T oT) { -arr[index++] =OT; -          -     } -}

Three, generic interface

1 /**2 3 generic interface:4 5 definition format for generic interface: interface interface name < DECLARE custom generic >{}6 custom generic Considerations on interfaces:7 1 A custom generic data type on an interface is specified when the interface is implemented. 8 2 If the interface is custom-defined when the interface is implemented without specifying a data type then the interface defaults to the object type9 Ten if One  */ A  - InterfaceGenericdao<t>{ -      Public voidAdd (t t); the      -      Public voidRemove (t t); - } -  +  Public classDemo3ImplementsGenericdao<string >{ -  + @Override A      Public voidAdd (String t) { at         //TODO auto-generated Method Stub -          -     } -  - @Override -      Public voidRemove (String t) { in         //TODO auto-generated Method Stub -          to     } + } - classDemo4Implementsgenericdao{ the  * @Override $      Public voidAdd (Object t) {Panax Notoginseng         //TODO auto-generated Method Stub -     } the  + @Override A      Public voidRemove (Object t) { the         //TODO auto-generated Method Stub +     } -}

Three, generic interface

1 /**2 3 generic interface:4 5 definition format for generic interface: interface interface name < DECLARE custom generic >{}6 custom generic Considerations on interfaces:7 1 A custom generic data type on an interface is specified when the interface is implemented. 8 2 If the interface is custom-defined when the interface is implemented without specifying a data type then the interface defaults to the object type9 Ten if One  */ A  - InterfaceGenericdao<t>{ -      Public voidAdd (t t); the      -      Public voidRemove (t t); - } -  +  Public classDemo3ImplementsGenericdao<string >{ -  + @Override A      Public voidAdd (String t) { at         //TODO auto-generated Method Stub -          -     } -  - @Override -      Public voidRemove (String t) { in         //TODO auto-generated Method Stub -          to     } + } - classDemo4Implementsgenericdao{ the  * @Override $      Public voidAdd (Object t) {Panax Notoginseng         //TODO auto-generated Method Stub -     } the  + @Override A      Public voidRemove (Object t) { the         //TODO auto-generated Method Stub +     } -}

Java Basic Learning-generics

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.