java之泛型(Generic)
来源:互联网 发布:网络视频传输套接字 编辑:程序博客网 时间:2024/06/05 22:56
泛型的复习
一、认识泛型
public class GenericText{ //泛型的简单实用, public static void main(String[] args) { //根据你自己的需求可以变换你需要的类型,我下面用String类型 Genericdemo<String> gd = new Genericdemo<>(); gd.setX("10"); gd.setY("20"); System.out.println(gd.getX()+"---"+gd.getY()); } static class Genericdemo<T>{ public T x; public T Y; //写一个构造方法,来接通主类 public T getX() { return x; } public void setX(T x) { this.x = x; } public T getY() { return Y; } public void setY(T y) { Y = y; } }}
打印结果:
二、如何在构造方法中使用泛型
public class GenericText01<T>{ public static void main(String[] args) { //根据你自己的需求可以变换你需要的类型,我下面用String类型 Genericdemo<String> gd = new Genericdemo<>("我在构造方法中使用了泛型"); System.out.println(gd.getValue()); } //我们在公共类使用单个泛型 static class Genericdemo<T>{ public T value; //构造方法中直接用T来代替即可 public Genericdemo(T value) { super(); this.value = value; } public T getValue() { return value; } public void setValue(T value) { this.value = value; } }}
打印结果:
三、如何使用多个泛型
public class GenericText01{ public static void main(String[] args) { //根据你自己的需求可以变换你需要的类型,我下面用String类型 Genericdemo<String,Integer> gd = new Genericdemo<>("我在构造方法中使用了泛型",1024); System.out.println(gd.getValue()+"----"+gd.getKey()); } //我们在公共类使用多个泛型 static class Genericdemo<T,k>{ public T value; public k key; //构造方法中直接用T来代替即可 public Genericdemo(T value) { super(); this.value = value; } public T getValue() { return value; } public Genericdemo(T value, K key) { super(); this.value = value; this.key = key; } public K getKey() { return key; } public void setKey(K key) { this.key = key; } }}
打印结果:
四、泛型中的通配符,用于接受各种类型
public class GenericText02<T>{ public static void main(String[] args) { //根据你自己的需求可以变换你需要的类型,我下面用String类型 Genericdemo<String> gd = new Genericdemo<>(); gd.setValue("我在测试通配符"); tell(gd); //然后我们把gd.getValue()传递给一个方法 } /* * 这里面<?>,里面的“?”就是通配符,不管你上面传递过来的什么类型,他都可以接受 * */ public static void tell(Genericdemo<?> gd){ System.out.println("我是传递过来的值,可以是任何类型: "+gd); } //我们在公共类使用单个泛型 static class Genericdemo<T>{ public T value; public T getValue() { return value; } public void setValue(T value) { this.value = value; } @Override public String toString() { return this.getValue().toString(); } }}
打印结果:
五、在接口中使用泛型
//1、我们先写一个接口,使用泛型,定义一个方法 interface style<T>{ public void getstyle(); } //我们写一个接口的子类,实现我们定义的接口,里面的类型自己去定义 class TTT implements style<String>{ public String valueString; public String getValueString() { return valueString; } public void setValueString(String valueString) { this.valueString = valueString; } public TTT(String valueString) { super(); this.valueString = valueString; } //没有用到这个方法 @Override public void getstyle() { } }public class GenericText04<T>{ //我们写一个接口的子类,实现我们定义的接口 //泛型的简单实用, public static void main(String[] args) { //创建我们子类对象 TTT gd = new TTT("我在这里用了接口泛型"); System.out.println(gd.getValueString()); }}
打印结果:
六、泛型方法的使用
public class GenericText03<T>{ //泛型方法 public static void main(String[] args) { //根据你自己的需求可以变换你需要的类型,我下面用String类型 Genericdemo<String> gd = new Genericdemo<>(); String s = gd.tell("A"); System.out.println("我传的是字符"+s); int num = gd.tell(1024); System.out.println("我传的是数字"+num); } static class Genericdemo<T>{ public <T> T tell(T t){ return t; } }}
打印结果:
六、数组使用泛型,就几句
0 0
- java之泛型(Generic)
- java之泛型(Generic)
- Java特性之泛型(Generic Types)
- 黑马程序员Java学习笔记之泛型Generic
- Java中的泛型(Generic)
- Java中的泛型(Generic)
- 泛型爪哇(Generic Java)
- Java 的泛型程序设计( Generic Programing)
- java 中的泛型 generic(未完成)
- Java中的泛型编程(generic programming)和泛型类(generic class)
- 泛型参数Generic
- java generic super/extends (java 泛型中的super/extends)
- Core Java (十九) 泛型程序设计(Generic parogramming)
- Java generic
- Java Generic
- java Generic
- Java Generic
- 泛型方法Generic Method
- KoaHub.js是基于 Koa.js 平台的 Node.js web 快速开发框架
- Go语言安装与liteIDE配置
- [C#]DataTable和DataSet有什么区别
- python中os.system调用exe文件问题
- C文件读写函数介绍(转)
- java之泛型(Generic)
- Microsoft .NET Framework 各个版本之间的关系?如何安装2.0,3.0,4.0?向下兼容?
- android开发中TabHost使用方法
- java基础的综合应用(类的应用,文本拷贝,IO流,数组,集合,文件文件夹的创建)
- an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe
- I2C知识总结
- Unity3D 之 OnTriggerEnter和OnCollisionEnter的区别
- 给编程生涯充电的 10 本书
- Python神图