汎用分類——汎用クラス、汎用メソッド、汎用インタフェース、汎用高度(ワイルドカード)
4660 ワード
汎用分類——汎用クラス、汎用メソッド、汎用インタフェース、汎用高度(ワイルドカード)
1、汎用型で安全問題を解決する:
Javaの初期には、Objectタイプを使用して任意のオブジェクトタイプを受信しました.しかし、実際の使用では、アップシフトには何の問題もないというタイプ変換の問題がありますが、ダウンシフト時には実はタイプ変換の問題が隠されています.これがプログラムが安全ではない原因です.つまり、このようなセキュリティ上の危険性があるため、JavaはJDK 5以降、このセキュリティ問題を解決するために汎用的なものを提供しています.
2、汎用クラス
(1)汎用クラス:汎用をクラスに定義する.
フォーマット:public classクラス名
注:汎用タイプは参照タイプでなければなりません.
(2)コードプレゼンテーション
*******************************
3、汎用方法
(1)汎用メソッド:汎用をメソッドに定義する.
フォーマット:public戻りタイプメソッド名
(2)コードプレゼンテーション
*******************************
4、汎用インタフェース
(1)汎用インタフェース:汎用をインタフェースに定義する.
フォーマット:public iterfaceインタフェース名
(2)コードプレゼンテーション
A:インタフェースの定義
B:インタフェースの実装クラスを定義する
C:テストクラス
5、汎用高級(汎用ワイルドカード)
(1)汎用上級(ワイルドカード)
A: ?:任意のタイプで、明確でなければ、Objectと任意のJavaクラスです. B: ? extends E:下に限定され、EはE自体とそのサブクラスのみです. C: ? super E:上に限定され、EはE自体とその親のみです.
(2)コードプレゼンテーション
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