Java Generic Instances

Source: Internet
Author: User

/** * @authorRollen-holt using generics*/ classHello<t, v>{hello () {} PublicT GetName () {returnname; }      Public voidsetName (T name) { This. Name =name; }      PublicV getage () {returnAge ; }      Public voidsetage (V age) { This. Age =Age ; }     PrivateT name; PrivateV age;  Public Static voidMain (string[] args) {Hello<string, integer> he =NewHello<string, integer>(); He.setage (10); He.setname ("Rollen Holt"); System.out.println (He.getname ()+ "   " +he.getage ()); }} /** * @authorRollen-holt Generic class construction method definition*/ classHello<t, v>{Hello (T name, V age) { This. Age =Age ;  This. Name =name; }      PublicT GetName () {returnname; }      PublicV getage () {returnAge ; }     PrivateT name; PrivateV age;  Public Static voidMain (string[] args) {Hello<String,Integer> he=NewHello<string,integer> ("Rollen", 12); System.out.println (He.getname ()+"  "+he.getage ()); }} /** * @authorRollen-holt using wildcard characters*/ classInfo<t>{info (T name) { This. Name =name; }    PrivateT name;} classhello{ Public Static voidfunction (info<?>temp) {System.out.println ("Content:" +temp); }          Public Static voidMain (string[] args) {info<String> demo=NewInfo<string> ("Rollen");    function (demo); }} /** * @authorrollen-holt Generic Limit*/ classInfo<t>{info (T age) { This. age=Age ; }    PrivateT age;} classhello{ Public Static voidfunction (INFO&LT;?extendsNumber>temp) {System.out.println ("Content" +temp); }          Public Static voidMain (string[] args) {info<Integer> demo=NewInfo<integer> (1);    function (demo); }} /** * @authorrollen-holt Generic Lower limit*/ classInfo<t>{info (T age) { This. age=Age ; }    PrivateT age;} classhello{ Public Static voidfunction (INFO&LT;?SuperString>temp) {System.out.println ("Content" +temp); }          Public Static voidMain (string[] args) {//only string or object can be used hereInfo<string> demo=NewInfo<string> ("Rollen");    function (demo); }}/** * @authorRollen-holt limitations of generics and subclass inheritance*/ classInfo<t>{} classhello{ Public Static voidMain (string[] args) {info<String> demo1=NewInfo<string>(); Info<Object> demo2=NewInfo<object>(); //Demo2=demo1; Error here          }} /*** The above example shows that a subclass of a class can be instantiated by its parent class by polymorphism * But in a generic operation, a generic type of a subclass cannot be instantiated by its parent class's generic type. */if the above bar statement is allowed, it will appear: Exception in thread"Main"java.lang.Error:Unresolved compilation Problem:type Mismatch:cannot convert from Info<String> to Info<object>At Hello.main (Hello.java:12) Two implementations of generic interfaces:/** * @authorRollen-holt implementation of generic interfaces 1*/ InterfaceInfo<t> {     Public voidsay ();} //declaring generics directly after a subclassclassHello<t>ImplementsInfo<t>{      Public Static voidMain (string[] args) {info<String> demo =NewHello<string>();     Demo.say (); }      Public voidsay () {System.out.println ("Hello"); }} /** * @authorRollen-holt implementation of generic interfaces 2*/ InterfaceInfo<t> {     Public voidsay ();} //explicitly given a generic type in the interface implemented by the subclassclassHelloImplementsInfo<string>{      Public Static voidMain (string[] args) {info<String> demo =Newhello ();     Demo.say (); }      Public voidsay () {System.out.println ("Hello"); }}/** * @authorRollen-holt The type of an incoming parameter using a generic pass-through*/ classInfo<t> {    PrivateT var;  PublicT GetVar () {returnvar; }      Public voidSetVar (T var) { This. var =var; }     PublicString toString () {return  This. var.tostring (); }     }classhello{ Public Static voidMain (string[] args) {info<String> demo1=NewInfo<string>(); Info<String> demo2=NewInfo<string>(); Demo1.setvar ("Rollen"); Demo2.setvar ("Holt");    Printadd (Demo1, Demo2); }    //all of the same string types are passed here .     Public Static<T>voidPrintadd (info<t> demo1, info<t>Demo2) {System.out.println (Demo1.getvar ()+" "+Demo2.getvar ()); Otherwise, it looks like this: An error has occurred:/** * @authorRollen-holt The type of an incoming parameter using a generic pass-through*/ classInfo<t> {    PrivateT var;  PublicT GetVar () {returnvar; }      Public voidSetVar (T var) { This. var =var; }     PublicString toString () {return  This. var.tostring (); }     }classhello{ Public Static voidMain (string[] args) {info<String> demo1=NewInfo<string>(); Info<Integer> demo2=NewInfo<integer>(); Demo1.setvar ("Rollen"); Demo2.setvar (30); //Error called herePrintadd (Demo1, Demo2); }    //all of the same string types are passed here .     Public Static<T>voidPrintadd (info<t> demo1, info<t>Demo2) {System.out.println (Demo1.getvar ()+" "+Demo2.getvar ()); }} Exception in thread"Main"java.lang.Error:Unresolved Compilation Problem:the method Printadd (Info<t>, info<t>) in the type Hello was not applicable forThe arguments (INFO&LT;STRING&GT;, info<integer>) at Hello.main (Hello.java:26)

Java Generic Instances

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.