Linux搭建青龍面板實現簡單功能
阿新 • • 發佈:2022-02-07
Java 泛型
泛型在Java中被稱之為”JAVA型別”,簡稱GJ。泛型是JavaSE平臺1.5版本增加的新特性。泛型在Java中不是一種資料型別,是一種在編譯時期的特殊語法,
它能夠讓JVM識別從而確定在應用泛型的操作中體現泛型特點,幫助程式設計師在開發中從泛型中獲得更高效和更安全資料的管理操作。
泛型由於不是一種Java的資料型別所以在執行時,JVM將使用擦除法將泛型描述還原成未應用泛型語法的規則進行執行。
泛型基本語法:<T>
泛型能夠在程式碼書寫過程中幫助程式設計師提高效率和資料型別安全。 泛型能夠在編譯階段確定資料型別是否符合要求,規避錯誤發生。 泛型能否避免資料物件強制型別轉換操作。 泛型也能夠支援在動態下確定資料型別。
import java.util.Date;
/**
* 員工實體類
*/
public class Employee {
private int id;
private String name;
private String longName;
private String password;
private Date birth;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLongName() {
return longName;
}
public void setLongName(String longName) {
this.longName = longName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
}
import com.GJ.entity.Employee; import java.util.ArrayList; import java.util.List; public class Test { public static void main(String[] args) { List<Employee> employeeList=new ArrayList<Employee>(); Employee emp=new Employee(); emp.setId(1); emp.setName("十三"); Employee emp2=new Employee(); emp2.setId(2); emp2.setName("十四"); Employee emp3=new Employee(); emp3.setId(3); emp3.setName("十五"); employeeList.add(emp); employeeList.add(emp2); employeeList.add(emp3); for (Employee e:employeeList){ System.out.println(e.getName()+"\t"+e.getId()); } } }
Java萬用字元泛型
List<?> numList = new ArrayList<>(); 使用萬用字元<?>的泛型,可以把任意型別的物件丟進去。
但是缺點是,呼叫的時候只能用Object接收,並通過強制型別轉換強轉,這樣就失去了泛型的一個省事兒的優勢。所以最好還是使用確定了型別的泛型比較好些。
public class Demo01 {
public static void main(String[] args) {
List<Integer> intList=new ArrayList<Integer>();
intList.add(new Integer(100));
intList.add(new Integer(200));
/* List<Number> numberList=intList; //會報錯*/
List<?> list=intList;
for (Object intObj:list){
System.out.println((Integer)intObj);
}
System.out.println("-----------------------------");
List<Number> numberList=new ArrayList<Number>();
numberList.add(new Double(6.66));
numberList.add(new Double(7.66));
numberList.add(new Double(16.66));
for (Number n:numberList){
System.out.println(n);
}
}
}
/**
* 使用Map集合應用泛型
*/
public class Demo02 {
public static void main(String[] args) {
Map<String, Set<Employee>> map=new HashMap<String, Set<Employee>>();
//建立員工物件
Employee emp=new Employee();
emp.setName("十三");
Employee emp2=new Employee();
emp2.setName("十四");
Set<Employee> set=new HashSet<Employee>();
set.add(emp);
set.add(emp2);
map.put("e",set);
Set<Employee> empSet=map.get("e");
for (Employee e:empSet){
System.out.println(e.getName());
}
}
}
帶泛型的類
public class GJClass<T>{ //這種屬於隱式泛型。顯示的一般有明確的資料型別如<Integer>
public String getClassName(T t){
return t.getClass().getName();
}
}
一般情況下隱式的用的比較多,顯示的用得少。
帶泛型的介面
public interface CountManager<T>{
double count(T t,double r);
double count(T t,double bottom,double height);
}