汎用分類——汎用クラス、汎用メソッド、汎用インタフェース、汎用高度(ワイルドカード)

4660 ワード

汎用分類——汎用クラス、汎用メソッド、汎用インタフェース、汎用高度(ワイルドカード)
1、汎用型で安全問題を解決する:
Javaの初期には、Objectタイプを使用して任意のオブジェクトタイプを受信しました.しかし、実際の使用では、アップシフトには何の問題もないというタイプ変換の問題がありますが、ダウンシフト時には実はタイプ変換の問題が隠されています.これがプログラムが安全ではない原因です.つまり、このようなセキュリティ上の危険性があるため、JavaはJDK 5以降、このセキュリティ問題を解決するために汎用的なものを提供しています.
2、汎用クラス
(1)汎用クラス:汎用をクラスに定義する.
フォーマット:public classクラス名
注:汎用タイプは参照タイプでなければなりません.
(2)コードプレゼンテーション
package cn.itcast_04;
/*
 *    :        
 */
public class ObjectTool {
	private T obj;

	public T getObj() {
		return obj;
	}

	public void setObj(T obj) {
		this.obj = obj;
	}
}

*******************************
package cn.itcast_04;

/*
 *       
 */
public class ObjectToolDemo {
	public static void main(String[] args) {
		ObjectTool ot = new ObjectTool();
		// ot.setObj(new Integer(27)); 				//            !
		ot.setObj(new String("   "));
		String s = ot.getObj();
		System.out.println("   :" + s);

		ObjectTool ot2 = new ObjectTool();
		// ot2.setObj(new String("   "));		//            !
		ot2.setObj(new Integer(27));
		Integer i = ot2.getObj();
		System.out.println("   :" + i);
	}
}

3、汎用方法
(1)汎用メソッド:汎用をメソッドに定義する.
フォーマット:public戻りタイプメソッド名
(2)コードプレゼンテーション
/*
 *     :         
 */
package cn.itcast_05;

public class ObjectTool {
	public  void show(T t) {
		System.out.println(t);
	}
}

*******************************
package cn.itcast_05;

public class ObjectToolDemo {
	public static void main(String[] args) {

		//        
		ObjectTool ot = new ObjectTool();
		ot.show("hello");
		ot.show(100);
		ot.show(true);
	}
}

4、汎用インタフェース
(1)汎用インタフェース:汎用をインタフェースに定義する.
フォーマット:public iterfaceインタフェース名
(2)コードプレゼンテーション
A:インタフェースの定義
package cn.itcast_06;

/*
 *     :         
 */
public interface Inter {
	public abstract void show(T t);
}

B:インタフェースの実装クラスを定義する
package cn.itcast_06;

//           
//     :            (   )

//public class InterImpl implements Inter {
//
//	@Override
//	public void show(String t) {
//		System.out.println(t);
//	}
// }

//     :          (  )
public class InterImpl implements Inter {

	@Override
	public void show(T t) {
		System.out.println(t);
	}
}

C:テストクラス
package cn.itcast_06;

public class InterDemo {
	public static void main(String[] args) {
		//         (   )
		// Inter i = new InterImpl();
		// i.show("hello");

		// //         (  )
		Inter i = new InterImpl();
		i.show("hello");

		Inter ii = new InterImpl();
		ii.show(100);
	}
}

5、汎用高級(汎用ワイルドカード)
(1)汎用上級(ワイルドカード)
        A:        ?:任意のタイプで、明確でなければ、Objectと任意のJavaクラスです.        B:        ? extends E:下に限定され、EはE自体とそのサブクラスのみです.        C:        ? super E:上に限定され、EはE自体とその親のみです.
(2)コードプレゼンテーション
package cn.itcast_07;

import java.util.ArrayList;
import java.util.Collection;

/*
 *     (   )
 * ?:	    ,      ,    Object     Java  
 * ? extends E:	    ,E   E      
 * ? super E:	    ,E   E      
 */
public class GenericDemo {
	public static void main(String[] args) {
		//            ,      
		Collection c1 = new ArrayList();
		// Collection c2 = new ArrayList();	//  
		// Collection c3 = new ArrayList();	//  
		// Collection c4 = new ArrayList();	//  

		// ?	            
		Collection> c5 = new ArrayList();
		Collection> c6 = new ArrayList();
		Collection> c7 = new ArrayList();
		Collection> c8 = new ArrayList();

		// ? extends E:	    ,E   E      
		// Collection extends Animal> c9 = new ArrayList();	//  
		Collection extends Animal> c10 = new ArrayList();
		Collection extends Animal> c11 = new ArrayList();
		Collection extends Animal> c12 = new ArrayList();

		// ? super E:	    ,E   E      
		Collection super Animal> c13 = new ArrayList();
		Collection super Animal> c14 = new ArrayList();
		// Collection super Animal> c15 = new ArrayList();	//  
		// Collection super Animal> c16 = new ArrayList();	//  
	}
}

class Animal {
}

class Dog extends Animal {
}

class Cat extends Animal {
}