Generics, JDK1,5 after new features, security mechanisms

benefit:

1, will appear runtime problems ClassCastException, moved to compile time, ahead of time to determine the type, to ensure the safety issues.

2, to avoid the cast

Generic format: to define the data type of the operation referenced by <>.  

When using JAVA objects provided, when it wrote Generics

class Demo 
{ 
    public <T> void Show (T T) 
    { 
        System.out.println ( "Show:" + T); 
    } 
    public <T> void Print (T T) 
    { 
        System.out.println ( "Print:" + T); 
    } 
    public <Q> void method (Q Q) 
    { 
        System.out.println ( "method:" + Q); 
    } / * define a generic class can be defined in the class, the method can also be defined in on. * / 
} 
Class Demo2 <T>  
{ 
    public   void Show (T T) 
    {
        System.out.println("show:"+t);
    }
    public void print(T t)
    {
        System.out.println("print:"+t);
    }
    public <Q> void method(Q q)
    {
        System.out.println("method:"+q);
    }
}


接口

interface Inter<T>
{
void show(T t);
}
/*
class InterImpl implements Inter<String>
{
public void show(String t)
{
System.out.println("show:"+t);
}

}
*/
class InterImpl<T> implements Inter<T>
{
public void show(T t)
{
System.out.println("show:"+t);
}
}

 

 

? Usually set in the common frame, whenever you see <> generic definition

When a set time, the set of data types to be stored as an argument to the <> which can

 

  Generic classes: When do I need a generic class

When uncertainty reference data type to be operated type, defined Object to the early completion of development, is now defined generic

Avoid strong turn, will be transferred to a compile-time error.

 

/ * Special features: static methods can not be defined on a generic access classes. If the application data type static methods uncertain operation, may be defined on a generic method * /

Guess you like

Origin www.cnblogs.com/zxl1010/p/11429497.html