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

深圳提供网站建设服务平台无锡网站推广优化

深圳提供网站建设服务平台,无锡网站推广优化,印刷报价下单网站开发,手机网站建设方案书有时您可能想限制可以在参数化类型中用作类型参数的类型。 例如#xff0c;对数字进行操作的方法可能只希望接受Number或其子类的实例。 这就是有界类型参数的用途。受限制参数类型的方法示例要声明有界类型参数#xff0c;请列出类型参数的名称#xff0c;后跟extends关键字…有时您可能想限制可以在参数化类型中用作类型参数的类型。 例如对数字进行操作的方法可能只希望接受Number或其子类的实例。 这就是有界类型参数的用途。受限制参数类型的方法示例要声明有界类型参数请列出类型参数的名称后跟extends关键字然后是其上限在本例中为Number请注意在这种情况下extends通常用于表示“扩展”(如在类中)或“实现”(如在接口中)。package generics;/*** 定义受限制的方法** author psdxdgK1DT**/public class Box{private T t;public void set(T t) {this.t t;}public T get() {return t;}/*** 通过修改我们的通用泛型方法以包含此有界类型参数现在编译将失败因为我们对inspect的调用仍包含String* By modifying our generic method to include this bounded type parameter* compilation will now fail, since our invocation of inspect still includes a String:* inspect:单词检查* param * param u*/public void inspect(U u) {System.out.println(T: t.getClass().getName());System.out.println(U: u.getClass().getName());}public static void main(String[] args) {BoxintegerBox new Box();integerBox.set(new Integer(some text));integerBox.inspect(some test);这里会出现预编译错误integerBox.inspect(10);}}在显示器上会出现红色的波浪线表示编译错误如果强行编译则会报错program run result:Exception in thread “main” java.lang.Error: Unresolved compilation problem: The method inspect(U) in the type Box is not applicable for the arguments (String)at generics.Box.main(Box.java:36)译文未解决的编译错误Box类的inspect(U)方法不可应用于(String)类型参数\使用受限类型参的类可调用受限边界方法除了限制可用于实例化泛型类型的类型外有界类型参数还允许您调用在边界中定义的方法//使用受限类型参数的类public class NaturalNumber{private T n;public NaturalNumber(T n) { this.n n; }public boolean isEven() {return n.intValue() % 2 0;}// ...}isEven方法通过n调用Integer类中定义的intValue方法。多重受限边界(Multiple Bounds)The preceding example illustrates the use of a type parameter with a single bound, but a type parameter can have multiple bounds:A type variable with multiple bounds is a subtype of all the types listed in the bound. If one of the bounds is a class, it must be specified first. For example:Class A { /* … / } interface B { / … / } interface C { / … */ }class D { /* … */ } If bound A is not specified first, you get a compile-time error:class D { /* … */ } // compile-time error泛型算法有界类型参数是实现泛型算法的关键。考虑下面的方法该方法计算数组T[]中大于指定元素elem的元素数。public static int countGreaterThan(T[] anArray, T elem) {int count 0;for (T e : anArray)if (e elem) // compiler errorcount;return count;}The implementation of the method is straightforward,but it does not compile because the greater than operator () applies only to primitive typessuch as short, int, double, long, float, byte, and char.You cannot use the operator to compare objects. To fix the problem, use a type parameterbounded by the Comparableinterface:public interface Comparable{public int compareTo(T o);}The resulting code will be:public static int countGreaterThan(T[] anArray, T elem) {int count 0;for (T e : anArray)//因为这里的T是受限制的类型参数实现了Comparable接口于是可以使用接口的方法compareToif (e.compareTo(elem) 0)count;return count;}以上这篇java定义受限制的类型参数操作就是小编分享给大家的全部内容了希望能给大家一个参考也希望大家多多支持脚本之家。
http://www.huolong8.cn/news/65321/

相关文章:

  • 延吉网站优化html网页框架代码
  • 找做网站的朋友服装网站建设公司哪家好
  • 有域名就可以做网站么网站手机验证码怎么做
  • php网站建设学习娄底网站建设是什么
  • 电商网站制作价格兰州市城乡建设局网站
  • 怎么更换网站图片学校网站建设调查报告
  • 网站开发最新技术湖南省城乡建设勘测院 网站
  • 如何建一个网站教程建设好网站为什么读取不到文件
  • 生物商城网站建设自己可以模拟装修app
  • 重庆网站建设制作wordpress 树形主题
  • 自己做网站名电子章违法吗课程网站建设ppt模板
  • 山东网站建设价格实惠搭建网站的空间哪里买
  • 别墅效果图网站网络如何推广
  • 网站设计风格确认书电子商务专业就业方向及前景分析
  • 镇江网站建设优化网站asp代码
  • 网站建设 展滔科技大厦wordpress 自动水印
  • 在微信怎样搞做微视频网站一千元做网站
  • 通辽网站开发三文鱼电商代运营
  • 北京网站定制跨境电商app有哪些
  • 制作网站比较大的几家公司百度投稿平台
  • 成都网站建设g冠辰dede网站建设的个人总结
  • 网站建设运用软件石家庄是几线城市
  • 怎么做套版网站广平企业做网站推广
  • 学校部门网站建设总结企业 北京 响应式网站
  • 北京哪里有做网站的廊坊智能模板建站
  • 优秀的学校网站欣赏电子商城网站开发文档
  • 老外做的汉语网站创建全国文明城市主题班会教案
  • 南昌网站建设和推广网站改版 大量旧页面
  • 九江专业制作网站小程序电商网络推广培训
  • 池州建行网站建设部网站注册人员