Java泛型使用總結案例

  1. 泛型類使用

package com.denganzhi.fangxing;


/***
 * 泛型類: 申明時使用泛型
 * 字母:
 *  T   key 表示類型
 *  E,V   分別代表鍵值中的key value
 *  E: 代表元素
 */
 class Student<T1,T2> {
	
	private T1 username;
	private T2 age;
	public T1 getUsername() {
		return username;
	}
	public void setUsername(T1 username) {
		this.username = username;
	}
	public T2 getAge() {
		return age;
	}
	public void setAge(T2 age) {
		this.age = age;
	}
}

public class Main {
	public static void main(String[] args) {
		 // 使用的時候指定類型(引用)
	/**  注意
	 *  1.泛型在使用的時候,不能使用基本類型
	 *  2.泛型在使用的時候不能使用 靜態屬性|靜態方法上
	 */
	Student<String, Integer> stu=new Student<>();
    stu.setAge(100);
	}
}

 2. 泛型接口使用

package com.denganzhi.fangxing;

/**
 * 
 *  泛型接口使用
 */
public interface Comparator1<T> {

   void compare(T t);
}

3. 泛型方法

package com.denganzhi.fangxing;

public class TestMethod {

	 public static void main(String[] args) {
		 // 調用方法
		test(33);
	}
	// 泛型方法
	 // 只能訪問對象的信息,不能修改,對象不確定   <= 
	public static<T extends Integer> void test(T a){
		System.out.println(a);
	}
}

 4.   泛型與繼承| 接口使用使用

package com.denganzhi.fangxing;


/**
 *   1. 實際使用中 第一種方式 
 *   2. 第二種方式
 */
public abstract class Father<T> {
  T name;
  public abstract void test(T t);
}

//1. 子類使用時候  父類確定具體的類型
class child1 extends Father<String>{

	@Override
	public void test(String t) {
		// TODO Auto-generated method stub
	}
}
// 2. 子類爲泛型類,繼承父類泛型
class child2<T> extends Father<T>{

	@Override
	public void test(T t) {
		// TODO Auto-generated method stub
		
	}
}
// 3. 子類爲泛型,父類不指定類型,泛型擦除,使用object替代
class child3<T> extends Father{

	@Override
	public void test(Object t) {
		// TODO Auto-generated method stub
		
	}
	
}
// 子類和同時擦除
class child4 extends Father{

	@Override
	public void test(Object t) {
		// TODO Auto-generated method stub
		
	}
	
}
//錯誤, 子類擦除,父類使用泛型
//class child5 extends Father<T>{
//	
//}

5. 泛型的嵌套:

package com.denganzhi.fangxing;

class Student2<T>{
	T score;
}

 class Bjsxt<T> {
  T stu;
}

public class TypeNest{
	public static void main(String[] args) {
		// 泛型嵌套 
		Bjsxt<Student2<Integer>> bjsxt=new Bjsxt<Student2<Integer>>();
		// 使用的時候從內到外拆分
		bjsxt.stu=new Student2<Integer>();
		Student2<Integer> stu2= bjsxt.stu;
		stu2.score=100;
		Integer score = stu2.score;
		
		System.out.println(score);
		
		Object str="xiaoming";
		if(str instanceof String){
			System.out.println(str);
		}
		
	}
}

6. 泛型與多態

package com.denganzhi.fangxing;

/**
 * 泛型與多態
 */
abstract class  Animal{
  abstract void showAnimal();	
}
class Dog extends Animal{

	@Override
	void showAnimal() {
	  System.out.println("dog");
	}
}
class Cat extends Animal{

	@Override
	void showAnimal() {
	 System.out.println("cat");
	}
}

class Student1<T>{
	T t1;
}

public class MultiTypeMain {
	
 // ? 不確定類型
  public static void showDog1(Student1<?> stu){

  }
  // ? <=  Animal 
  public static void showDog2(Student1<? extends Animal> stu){

  }
  // ? >= Animal 
  public static void showDog3(Student1<? super Animal> stu){

  }
  
  // 使用的時候
  public static void main(String[] args) {
	showDog1(new Student1<Object>());
    showDog2(new Student1<Dog>());
    showDog3(new Student1<Animal>());
  }
  
	
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章