Generic methods in non-generic classes

import java.io.Closeable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import javax.swing.event.ListSelectionEvent;

/*
* Generic methods defined in non-generic classes
* T...represents variable parameters, essentially an array (T[])
*/
public class Test {

//泛型方法
 public static <T> void Method(T t){
     System.out.println(t);
 }

 //释放资源,一般用于文件流中
 //此时T继承Closeable
 public static <T extends Closeable> void ListSelectionEvent(T... a) {
    for(T temp:a) {
        if(temp!=null) {
            try {
                temp.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
 }    

//The flow file generally has exceptions
public static void main(String[] args) throws FileNotFoundException {
Method("it's good");
ListSelectionEvent(new FileInputStream("a.txt"));
}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325860573&siteId=291194637