Custom generic Classes

Source: Internet
Author: User

1 when instantiating an object of a generic class, indicate the type of the generic class. When specified, all locations in the corresponding class that use generics become the type of the generic that is specified in the instantiation

2 If we customize a generic class, but are not used when instantiating it, the default type is the object class.

 PackageLianxi3;Importjava.util.ArrayList;Importjava.util.List;Importorg.junit.Test;//Custom Generic Classes Public classOrder<t> {    Private intorderId; PrivateString Ordername; PrivateT T; List<T> list =NewArraylist<t>(); //methods for declaring generic classes     Public<E>e Gete (e e) {returne; }    //implementing array-to-collection replication     Public<e>list Fromarrtolist (e[]e,list<e>List1) {         for(E e1:e) {list1.add (E1); }        returnList1; }     Public voidAdd () {List.add (t); }     Public intGetorderid () {returnorderId; }     Public voidSetorderid (intorderId) {         This. OrderId =orderId; }     PublicString Getordername () {returnOrdername; }     Public voidsetordername (String ordername) { This. Ordername =Ordername; }     PublicT Gett () {returnT; }     Public voidsett (t t) { This. T =T; } @Override PublicString toString () {return"Order [orderid=" + OrderId + ", ordername=" +Ordername+ ", t=" + t + "]"; }}//1.class suborder<t> extends order<t>{classSuborderextendsOrder<boolean>{    }
@Test//Customizing the use of generic classes     Public voidtest3 () {Order<String> O1 =NewOrder<string>(); O1.setorderid (32); O1.setordername ("FAs"); O1.sett ("TT");        System.out.println (O1);        O1.add (); //list<string> list1 = o1.list;//The list in O1 holds data of type stringSystem.out.println (o1.list);//or System.out.println (List1); //methods for declaring generic classesInteger I1 = O1.gete (789); System.out.println ("I1=" +i1); //1.suborder<boolean> so = new suborder<boolean> ();Suborder so =Newsuborder (); So.setorderid (34); So.sett (true);        So.add (); So.sett (true);        So.add (); So.sett (false);        So.add ();    System.out.println (so.list); //implementing array-to-collection replicationInteger[] in =Newinteger[]{3,5,6}; List<Integer> List1 =NewArraylist<integer>();        O1.fromarrtolist (in, List1);            System.out.println (List1); }

Results:

Order [Orderid=32, Ordername=fas, T=tt]
[TT]
i1=789
[True, True, false]
[3, 5, 6]

Custom generic Classes

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.