当前位置: 首页 > news >正文

美容平台网站建设外包公司做网站

美容平台网站建设,外包公司做网站,百度站长网站规则改版,长沙网络推广营销文章目录前言泛型接口定义实现类时指定泛型具体的数据类型定义实现类时#xff0c;泛型与接口相同泛型接口的示例代码泛型类演示代码泛型方法演示代码泛型的通配符泛型的区间限定泛型应用泛型性能注意事项前言 泛型的本质就是将数据类型参数化, 普通方法的参数值可以变#… 文章目录前言泛型接口定义实现类时指定泛型具体的数据类型定义实现类时泛型与接口相同泛型接口的示例代码泛型类演示代码泛型方法演示代码泛型的通配符泛型的区间限定泛型应用泛型性能注意事项前言 泛型的本质就是将数据类型参数化, 普通方法的参数值可以变但是参数的数据类型是不能变的那么需要使用不同数据类型参数时我们通常的做法就是重载方法而泛型则可以将数据类型也搞成跟参数的值一样可以变的。 所以说泛型表示具体的数据类型未知泛型其实就是一个参数变量用来接收数据类型的名称。当我们在定义接口、类、方法时不知道使用什么数据类型的时候可以使用泛型。 泛型参数名称可以任何字符甚至单词不过通常使用比较多了就是 E 和 TE 表示 ElementT 表示 Type。 泛型分为泛型接口、泛型类和泛型方法。泛型接口、泛型类大家都比较熟悉例如 List 就是泛型接口而 ArrayList 就是泛型类, 我们经常看到 ListE 的声明new ArrayListE() 的定义尖括号里面的 E 就是泛型参数名称参数值就是你在创建对象的时候指定的具体数据类型例如你创建集合对象的时候可以指定 E 是 String或者 Integer当然也可以指定自己定义的类(例如CarBean)。在实际的工作中泛型方法用的比较多特别是解析自定义数据结构非常有用类似于 toString 这种场景是百试不爽。 泛型接口 泛型接口中的泛型参数的值是在写实现类的时候指定具体的值或者在引用实现子类的示例对象时指定具体的值但是引用实现子类对象时实现子类也必须是泛型类才行。 定义实现类时指定泛型具体的数据类型 例如定义一个泛型接口 Interator 和实现类 Scanner 的泛型实现如下 public interface interatorE {E next(); }Scanner 类实现了 Iterator 接口并指定接口的泛型参数值为 String所以重写的 next() 方法中的泛型参数值就是 String public final class Scanner implements IteratorString {public String next(){} }定义实现类时泛型与接口相同 在定义实现类时接口使用什么泛型参数名实现类就使用什么泛型参数名。其实就是定义一个泛型类只不过这个泛型的泛型参数名称需要和泛型接口的泛型参数名相同。创建对象的时候确定泛型的具体类型。 注在创建对象时指定泛型具体的数据类型那么具体的数据类型应该是保存在所创建的对象中当访问某个含有泛型参数的变量或者调用某个含有泛型参数的方法时会把这个数据类型传递过去。 例如List 接口和 ArrayList 实现类的泛型实现如下 public interface ListE {boolean add(E e);E get(int index); }pubic class ArrayListE implements ListE {public boolean add(E e);public E get(int index); }泛型接口的示例代码 泛型接口 package priv.lwx.javaprac.genericinterface;/*** 泛型接口示例** author liaowenxiong* date 2021/11/22 15:02*/public interface GenericInterfaceE {void toString(E e); }泛型接口的第一种使用方式定义实现类时指定泛型具体的数据类型 package priv.lwx.javaprac.genericinterface;/*** 泛型接口的第一种使用方式* 定义实现类时指定泛型具体的数据类型** author liaowenxiong* date 2021/11/22 15:42*/public class GenericInterfaceImpl01 implements GenericInterfaceDouble{Overridepublic void toString(Double aDouble) {System.out.println(aDouble);} }泛型接口的第二种使用方式在定义实现类时接口使用什么泛型参数名实现类就使用什么泛型参数名。其实就是定义一个泛型类只不过这个泛型的泛型参数名称需要和泛型接口的泛型参数名相同。创建对象的时候确定泛型的具体类型。 package priv.lwx.javaprac.genericinterface;/*** 泛型接口的第二种使用方式* 在定义实现类时接口使用什么泛型参数名实现类就* 使用什么泛型参数名。其实就是定义一个泛型类只不过这个泛型的泛型参数名称需* 要和泛型接口的泛型参数名相同。创建对象的时候确定泛型的具体类型。** author liaowenxiong* date 2021/11/22 15:04*/public class GenericInterfaceImpl02E implements GenericInterfaceE {Overridepublic void toString(E e) {System.out.println(e);} }测试类代码 package priv.lwx.javaprac.genericinterface;/*** 泛型接口测试** author liaowenxiong* date 2021/11/22 15:31*/public class GenericInterfaceDemo01 {public static void main(String[] args) {/*泛型接口的第一种使用方式测试定义实现类时指定了具体的泛型类型那么使用接口类型的变量接收实现类对象在调用方法时会有风险*/GenericInterface gi1 new GenericInterfaceImpl01();gi1.toString(123); // 参数类型是Object,编译通过运行时报错Integer cannot be cast to Double/*只能使用实现类变量接收对象才能避免风险参数类型是确定的*/GenericInterfaceImpl01 gi2 new GenericInterfaceImpl01();gi2.toString(23.99);/*泛型接口第二种使用方式测试定义实现类时跟随接口的泛型那么使用接口类型的变量接收实现类对象时接口指定什么数据类型那么创建的对象中有关变量或方法的泛型参数就自动使用什么数据类型*/GenericInterfaceString gi3 new GenericInterfaceImpl02();gi3.toString(liaowenxiong);GenericInterfaceImpl02Double gi4 new GenericInterfaceImpl02();gi4.toString(2.9);} }泛型类 声明定义泛型类那么在创建泛型类对象时指定具体的泛型参数值即具体的数据类型。如果创建对象的时候不指定具体的数据类型默认数据类型是 Object。 例如ArrayList 类在声明定义的时候不知道集合中会存储什么类型的数据所以类型使用泛型 注在创建对象时指定泛型具体的数据类型那么具体的数据类型应该是保存在所创建的对象中当访问某个含有泛型参数的变量或者调用某个含有泛型参数的方法时会把这个数据类型传递过去。 演示代码 先声明定义两个 JavaBean 类型 1.CarBean public class CarBean {private String brand;private String name;private String price;public CarBean() {}public CarBean(String brand, String name, String price) {this.brand brand;this.name name;this.price price;}Overridepublic boolean equals(Object o) {if (this o) return true;if (o null || getClass() ! o.getClass()) return false;CarBean carBean (CarBean) o;return Objects.equals(brand, carBean.brand) Objects.equals(name, carBean.name) Objects.equals(price, carBean.price);}Overridepublic int hashCode() {return Objects.hash(brand, name, price);}public String getBrand() {return brand;}public void setBrand(String brand) {this.brand brand;}public String getName() {return name;}public void setName(String name) {this.name name;}public String getPrice() {return price;}public void setPrice(String price) {this.price price;} }2.HouseBean public class HouseBean {private String brand;private String name;private String price;public HouseBean() {}public HouseBean(String brand, String name, String price) {this.brand brand;this.name name;this.price price;}public String getBrand() {return brand;}public void setBrand(String brand) {this.brand brand;}public String getName() {return name;}public void setName(String name) {this.name name;}public String getPrice() {return price;}public void setPrice(String price) {this.price price;}Overridepublic String toString() {return HouseBean{ brand brand \ , name name \ , prive price \ };}Overridepublic boolean equals(Object o) {if (this o) return true;if (o null || getClass() ! o.getClass()) return false;HouseBean houseBean (HouseBean) o;return brand.equals(houseBean.brand) name.equals(houseBean.name) price.equals(houseBean.price);}Overridepublic int hashCode() {return Objects.hash(brand, name, price);} }再声明定义一个泛型类 public class GenericGoodsT { // 尖括号表示声明泛型参数名或泛型变量名为 Tprivate T goods;private String info_goods;public GenericGoods(T goods) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {this.goods goods;this.info_goods setData(goods);}public GenericGoods(T goods, String info_goods) {this.goods goods;this.info_goods info_goods;}/*** 声明定义一个方法将指定的goods对象中的所有成员变量的值取出来并以“变量名变量值”这样的格式进行拼接后返回该字符处。* 这里使用泛型声明参数类型这样就可以接收任意类型的参数然后利用反射技术获取对象所属类型的变量名和变量值再拼接成字符串返回*/private String setData(T goods) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {StringBuffer sb new StringBuffer();String getterMethodName ;String fieldName ;Method mObject null;String info_goods ;// 获取对象所属类型的全部字段字段是指类所声明的静态变量和成员变量Field[] fields goods.getClass().getDeclaredFields();if (fields.length 0) {for (Field f : fields) {// 设置允许访问私有域f.setAccessible(true);// 获取字段名称fieldName f.getName();// 将字段名称按符号“#”进行拼接sb.append(fieldName #);// 拼接getter方面名称if (fieldName.length() 1) {// 组装getter方法名// substring(0,1)表示截取索引值从0到1的字符串但是不含索引值为1的字符其实就是截取第一个字符并且转换成大写// substring(1)表示截取索引值为1后面的所有字符串包含索引值为1的字符getterMethodName get fieldName.substring(0, 1).toUpperCase() fieldName.substring(1);// 将拼接的getter方法名打印出来System.out.println(getterMethodName);} else {// 否则说明字段名称仅有一个字符直接转换成大写再拼接 get 即可getterMethodName get fieldName.toUpperCase();// 将拼接的getter方法名打印出来System.out.println(getterMethodName);}// 获取goods所属类型中指定名称的方法对象mObject goods.getClass().getMethod(getterMethodName);// 获取对象goods的成员变量的值并且使用“变量名变量值”这样的格式拼接起来// mObject.invoke(goods)是指调用对象goods的方法mObject其实就是调用getter方法获取相关成员变量的值info_goods fieldName mObject.invoke(goods) ;// 截取info_goods中索引值从0到info_goods最后一个索引值的子字符串不包含最后一个索引值的字符其实就是将最后一个字符“”过滤掉info_goods info_goods.substring(0, info_goods.length() - 1);}// 打印输出所有字段名称System.out.println(sb);}return info_goods;}public T getGoods() {return goods;}public void setGoods(T goods) {this.goods goods;}public String getInfo_goods() {return info_goods;}public void setInfo_goods(String info_goods) {this.info_goods info_goods;}Overridepublic boolean equals(Object o) {if (this o) return true;if (o null || getClass() ! o.getClass()) return false;GenericGoods? that (GenericGoods?) o;return Objects.equals(goods, that.goods) Objects.equals(info_goods, that.info_goods);}Overridepublic int hashCode() {return Objects.hash(goods, info_goods);} }main 方法测试 public class Demo04Generic {public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {// 创建一个汽车产品CarBean cb new CarBean();cb.setBrand(奔驰);cb.setName(S60);cb.setPrice(100000.00);// 创建一个通用商品对象指定泛型参数具体的值即指定具体的泛型类型即指定具体的数据类型那么创建的对象中有使用泛型参数的地方具体的数据类型就已经明确了GenericGoodsCarBean gg new GenericGoods(cb); String info_goods gg.getInfo_goods();System.out.println(info_goods);} } 泛型方法 泛型方法的定义语法格式修饰符 泛型变量名 返回值类型 方法名(泛型变量名 参数名) {方法体}。 调用方法的时候确定泛型参数的值即具体的数据类型即调用方法的时候传递什么类型的参数泛型就是什么类型。 演示代码 public T String toString(T obj) { // 方法所声明的泛型参数名和类的泛型参数名相同不会造成冲突StringBuffer info_obj new StringBuffer();StringBuffer fieldNames new StringBuffer();Method m null;String getMethodName ;String fieldName ;Field[] fields obj.getClass().getDeclaredFields();System.out.println(对象所属类型是 obj.getClass().getName());if (fields.length 0) for (int i 0; i fields.length; i) {fieldName fields[i].getName();if (i fields.length - 1) {fieldNames.append(fieldName);} else {fieldNames.append(fieldName #);}if (fieldName.length() 1) {getMethodName get fieldName.toUpperCase();} else {getMethodName get fieldName.substring(0, 1).toUpperCase() fieldName.substring(1);}try {m obj.getClass().getMethod(getMethodName);if (i fields.length - 1) {info_obj.append(fieldName m.invoke(obj));} else {info_obj.append(fieldName m.invoke(obj) );}} catch (NoSuchMethodException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();}}}return info_obj.toString();}main 方法测试代码 public class Demo05Generic {public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {HouseBean hb new HouseBean();hb.setBrand(万科);hb.setName(皇帝宫廷);hb.setPrice(200000.00);GenericGoodsCarBean gg new GenericGoods(); // 泛型参数指定了类型 CarBeanSystem.out.println(gg.toString(hb)); // 调方法的时候依旧根据传参类型来确定泛型参数的值System.out.println();CarBean cb new CarBean();cb.setBrand(奔驰);cb.setName(S60);cb.setPrice(200000.00);System.out.println(gg.toString(cb));} }泛型的通配符 下面的例子演示了如何使用泛型的通配符接收不同数据类型的 ArrayList 参数 public class Demo02Generic {public static void main(String[] args) {ArrayListInteger list1 new ArrayList();list1.add(15);list1.add(25);ArrayListString list2 new ArrayList();list2.add(迪丽热巴);list2.add(古力娜扎);printArrayList(list1);printArrayList(list2);}// 定义一个方法可以遍历所有数据类型的 Arraylistpublic static void printArrayList(ArrayList? list) {//使用迭代器遍历集合Iterator? it list.iterator();while (it.hasNext()) {Object obj it.next();System.out.println(obj);}} }泛型的区间限定 1.泛型的上限限定? extends E 代表使用的泛型只能是类型 E 的子类或者本身 2.泛型的下限限定? super E 代表使用的泛型只能是类型 E 的父类或者本身 下面的例子就是演示了如何使用泛型的区间限定 public class Demo03Generic {public static void main(String[] args) {CollectionInteger list1 new ArrayList();CollectionString list2 new ArrayList();CollectionNumber list3 new ArrayList();CollectionObject list4 new ArrayList();getElement1(list1);getElement1(list2); // String 不是 Number 的子类所以报错getElement1(list3);getElement1(list4); // Object 不是 Number 的类所以报错getElement2(list1); // Integer 不是 Number 的父类所以报错getElement2(list2); // String 不是 Number 的父类所以报错getElement2(list3); getElement2(list4);}private static void getElement1(Collection? extends Number list1) {}private static void getElement2(Collection? super Number list1) {}// 定义一个方法可以遍历所有数据类型的 Arraylistpublic static void printArrayList(ArrayList? list) {//使用迭代器遍历集合Iterator? it list.iterator();while (it.hasNext()) {Object obj it.next();System.out.println(obj);}} } 泛型应用 泛型性能 可以参考这篇文章: http://blog.csdn.net/hejiangtao/article/details/7173838 注意事项 1.泛型参数或叫泛型变量的值只能是引用类型不能是基本数据类型 2.泛型参数或叫泛型变量可以有多个
http://www.pierceye.com/news/496246/

相关文章:

  • 网站的在线支付怎么做用别人的二级域名做网站
  • 做宣传册从哪个网站找素材吉林省吉林市是几线城市
  • 互助盘网站怎么做的北京做兼职网站有哪些
  • jsp 网站开发教程多语言外贸网站建设
  • 免费销售网站模板下载安装济南网站制作推广
  • 青岛专业网站制作设计vs2008网站开发
  • 电子商务网站功能介绍0基础做网站用什么语言
  • 企业展示网站建设wordpress 优酷免广告
  • 濮阳做网站引导企业做网站
  • 卧龙区网站建设找做牙工作上哪个网站
  • 做网站的会计分录广州公司注册需要什么条件
  • 小米4路由器可以做网站嘛服务类网站建设服务公司
  • 电子商务网站建设规划书实例注册微信公众平台
  • 网站设计课程总结线上营销方案
  • 谷城网站建设网站集群建设实施方案
  • 怎么做外贸生意谷歌seo服务
  • 做网站好还是做淘宝好市场调研模板
  • 网站做换肤做一个平台费用是多少
  • 省内注销二建 建设部网站更新慢网站建设合同书封皮
  • 天津城市建设网站营销型网站的好处
  • 网站建设维护文档好看的页面
  • 网站搜索优化官网网站建设在电子商务中的作用的看法
  • 网站推广费用价格html大屏展示模板
  • 编辑网站用什么软件网站开发人员职责
  • 网站搜索引擎提交wordpress发布站点
  • 阳春网站建设天水网站seo
  • 仙桃市建设局网站wordpress链接前面的图标
  • 温州市城乡建设建档案馆网站公司装修费用可以一次性入账吗
  • 房地产开发公司网站宠物网页设计模板
  • 网站备案信息可以改吗中国做二手房最大的网站