目录
- 泛型概念
- 泛型可设置多个类型参数
- 泛型继承
- 泛型接口
- 限制泛型可用类型
- 泛型通配声明
- 泛型方法
- 泛型方法限制泛型可用类型
泛型概念
- 问题引入:如果我们需要产生多个对象,每个对象的逻辑完全一样,只是对象内的成员啊变量的类型不同。那我们如何去做?两种解决办法:
方法一:
class Cls1
{int a;public Cls1(int a){//构造方法this.a = a;}public int getData(){return a;}
}class Cls2
{String b;//仅仅类型不同 小数、浮点数... 类的膨胀,重用性差public Cls2(String b){//构造方法this.b = b;}public String getData(){return b;}
}public class test {public static void main(String[] args) {Cls1 cls1 = new Cls1(10);System.out.println(cls1.getData());Cls2 cls2 = new Cls2("你好呀!");System.out.println(cls2.getData());}}
方法二:
class Cls1
{Object a;//Object是所有数据类型的父类public Cls1(Object a){//构造方法this.a = a;}public Object getData(){return a;}
}public class test {public static void main(String[] args) {Cls1 cls1 = new Cls1(10); //1.5版本以下运行出错,询问回复int不是类,是数据类型,String是类System.out.println(cls1.getData());//Integer才是类Cls1 cls2 = new Cls1("你好呀!");System.out.println(cls2.getData());//System.out.println((int)cls2.getData());这样强制转换 编译通过但运行出错!ppt有讲}}
这里可以使用泛型的解决方法:
创建工程时选择1.5版本JDK及以上
class Cls1<T>
{T a;//Object是所有数据类型的父类public Cls1(T a){//构造方法this.a = a;}public T getData(){return a;}
}public class test {public static void main(String[] args) {Cls1<Integer> cls1 = new Cls1<Integer>(10);System.out.println(cls1.getData());Cls1<String> cls2 = new Cls1<String>("你好呀!");System.out.println(cls2.getData());}}
泛型可设置多个类型参数
上述泛型类特点第二点的例子:
class Cls1<T>
{T a;public Cls1(T a){//构造方法this.a = a;}public T getData(){return a;}
}class Cls2<T,T2>
{T a;T2 b;public Cls2(T a,T2 b){this.a = a;this.b = b;}public T getData(){return a;}public T2 getData2(){return b;}
}public class test {public static void main(String[] args) {Cls1<Integer> cls1 = new Cls1<Integer>(10);System.out.println(cls1.getData());Cls1<String> cls2 = new Cls1<String>("你好呀!");System.out.println(cls2.getData());Cls2<Integer,String> cls3 = new Cls2<Integer,String>(100,"好热!");System.out.println(cls3.getData());System.out.println(cls3.getData2());}}
运行结果:
10
你好呀!
100
好热!
泛型继承
abstract class Cls1<T>//抽象类不能实例化(不能new)
{T a;public Cls1(T a){//构造方法this.a = a;}public T getData(){return a;}abstract void printInfo();
}class Cls2<T,T2> extends Cls1<T>
{T2 b;public Cls2(T a,T2 b){super(a);this.b = b;}public T2 getData2(){return b;}void printInfo(){System.out.println("输出");}
}public class test {public static void main(String[] args) {Cls2<Integer,String> cls = new Cls2<Integer,String>(100,"好热!");//抽象类不能进行实例化(Cls1不能new)cls.printInfo();}}
泛型接口
abstract class Cls1<T>//抽象类
{T a;public Cls1(T a){//构造方法this.a = a;}public T getData(){return a;}abstract void printInfo();
}interface cls3<T> //接口
{abstract void printInfoInCls3(T t);
}class Cls2<T,T2> extends Cls1<T> implements cls3<T>
{T2 b;public Cls2(T a,T2 b){super(a);this.b = b;}public T2 getData2(){return b;}void printInfo(){System.out.println("输出");}public void printInfoInCls3(T t) {// TODO Auto-generated method stubSystem.out.println(t);}
}public class test {public static void main(String[] args) {Cls2<Integer,String> cls = new Cls2<Integer,String>(100,"好热!");//抽象类不能进行初始化cls.printInfo();cls.printInfoInCls3(100);}}
运行结果:
输出
100
限制泛型可用类型
默认使用:
abstract class Cls1<T extends Object>
实现某个接口:
interface move //接口
{abstract void printInfoInCls3();
}abstract class Cls1<T extends move>//不用implements
{
...
}
泛型通配声明
泛型方法
例子:
class B
{public <T> void printInfo(T t) {//泛型方法System.out.println(t);}public void print1(int a){//比方法重载高效System.out.println(a);}public void print1(char a){System.out.println(a);}public void print1(String a){System.out.println(a);}
}public class test {public static void main(String[] args) {B b = new B();b.printInfo("哈哈");b.printInfo(1234);b.printInfo(0.5);b.printInfo('a');}}
运行结果:
哈哈
1234
0.5
a