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

手机网站设计嵌入式开发工程师

手机网站设计,嵌入式开发工程师,小程序商城模板下载,asp.net 建立网站吗我之前曾在Java Collections类的实用程序上进行过博客撰写#xff0c;并且特别地在使用Usings Collections Methods上的博客emptyList#xff08;#xff09;#xff0c;emptyMap#xff08;#xff09;和emptySet#xff08;#xff09;上进行了博客撰写。 在本文中并且特别地在使用Usings Collections Methods上的博客emptyListemptyMap和emptySet上进行了博客撰写。 在本文中我研究了使用Collections类的相关字段访问空集合与使用Collections类的相关方法访问空集合之间有时细微但重要的区别。 以下代码演示了直接访问Collections的字段以指定空集合。 将集合的字段用于空集合 /*** Instantiate my collections with empty versions using Collections fields.* This will result in javac compiler warnings stating warning: [unchecked]* unchecked conversion.*/public void instantiateWithEmptyCollectionsFieldsAssigment(){this.stringsList Collections.EMPTY_LIST;this.stringsSet Collections.EMPTY_SET;this.stringsMap Collections.EMPTY_MAP; } 上面的代码使用javac进行编译 但是导致出现警告消息在这种情况下该消息由NetBeans和Ant生成 -do-compile:[javac] Compiling 1 source file to C:\java\examples\typesafeEmptyCollections\build\classes[javac] Note: C:\java\examples\typesafeEmptyCollections\src\dustin\examples\Main.java uses unchecked or unsafe operations.[javac] Note: Recompile with -Xlint:unchecked for details. 将-Xlintunchecked指定为 javac的参数在这种情况下通过NetBeans project.properties文件中的javac.compilerargs-Xlint:unchecked 有助于获取更具体的警告消息用于前面列出的代码 [javac] Compiling 1 source file to C:\java\examples\typesafeEmptyCollections\build\classes[javac] C:\java\examples\typesafeEmptyCollections\src\dustin\examples\Main.java:27: warning: [unchecked] unchecked conversion[javac] this.stringsList Collections.EMPTY_LIST;[javac] ^[javac] required: ListString[javac] found: List[javac] C:\java\examples\typesafeEmptyCollections\src\dustin\examples\Main.java:28: warning: [unchecked] unchecked conversion[javac] this.stringsSet Collections.EMPTY_SET;[javac] ^[javac] required: SetString[javac] found: Set[javac] C:\java\examples\typesafeEmptyCollections\src\dustin\examples\Main.java:29: warning: [unchecked] unchecked conversion[javac] this.stringsMap Collections.EMPTY_MAP; [javac] ^[javac] required: MapString,String[javac] found: Map 如果在其选项中选中了适当的提示框则NetBeans还将显示这些警告。 接下来的三个图像演示如何确保设置适当的提示以查看NetBeans中的这些警告并提供一个示例说明NetBeans如何将上面显示的代码与警告一起呈现。 幸运的是很容易利用Collections类的实用程序并以类型安全的方式访问空集合而不会导致这些javac警告和相应的NetBeans提示。 这种方法是使用Collections的方法而不是其字段 。 下一个简单的代码清单对此进行了演示。 对空集合使用集合的方法 /*** Instantiate my collections with empty versions using Collections methods.* This will avoid the javac compiler warnings alluding to unchecked conversion.*/public void instantiateWithEmptyCollectionsMethodsTypeInferred(){this.stringsList Collections.emptyList();this.stringsSet Collections.emptySet();this.stringsMap Collections.emptyMap();} 上面的代码将编译而不会发出警告并且也不会显示任何NetBeans提示。 Collections类的每个字段的Javadoc文档都没有解决为什么这些字段会出现这些警告的问题但是每个类似方法的文档都对此进行了讨论。 具体来说有关Collections.emptyList Collections.emptySet和Collections.emptyMap的文档每个状态为“不同于此方法该字段不提供类型安全性。” 对最后一个代码清单中显示的空集合使用Collections方法可提供类型安全性而无需显式指定存储在该集合中的类型因为类型是通过在显式分配给已知和已经声明的实例属性时使用Collections方法来推断的指定的元素类型。 如果无法推断类型则使用没有显式指定类型的Collections方法时将导致编译器错误 。 下一个尝试在NetBeans中执行此操作的屏幕快照中显示了这一点。 具体的编译器错误消息是 [javac] C:\java\examples\typesafeEmptyCollections\src\dustin\examples\Main.java:62: error: method populateList in class Main cannot be applied to given types;[javac] populateList(Collections.emptyList());[javac] ^[javac] required: ListString[javac] found: ListObject[javac] reason: actual argument ListObject cannot be converted to ListString by method invocation conversion[javac] C:\java\examples\typesafeEmptyCollections\src\dustin\examples\Main.java:63: error: method populateSet in class Main cannot be applied to given types;[javac] populateSet(Collections.emptySet());[javac] ^[javac] required: SetString[javac] found: SetObject[javac] reason: actual argument SetObject cannot be converted to SetString by method invocation conversion[javac] C:\java\examples\typesafeEmptyCollections\src\dustin\examples\Main.java:64: error: method populateMap in class Main cannot be applied to given types;[javac] populateMap(Collections.emptyMap());[javac] ^[javac] required: MapString,String[javac] found: MapObject,Object[javac] reason: actual argument MapObject,Object cannot be converted to MapString,String by method invocation conversion[javac] 3 errors 通过在代码中显式指定集合元素的类型可以避免这些编译器错误并实现类型安全。 这显示在下一个代码清单中。 使用Collections的Empty方法显式指定元素类型 /*** Pass empty collections to another method for processing and specify those* empty methods using Collections methods. This will result in javac compiler* ERRORS unless the type is explicitly specified.*/public void instantiateWithEmptyCollectionsMethodsTypeSpecified(){populateList(Collections.StringemptyList());populateSet(Collections.StringemptySet());populateMap(Collections.String, StringemptyMap());} 出于相同的目的使用Collections类的用于获取空集合的方法比使用Collections的类似命名的字段更可取因为这些方法提供了类型安全性。 这样可以更好地利用Java的静态类型系统这是诸如Effective Java这类书籍的关键主题。 一个不错的副作用是消除了混乱的警告和标记的NetBeans提示但是更重要的结果是更好更安全的代码。 参考 JCG合作伙伴 Dustin Marx在Inspired by Actual Events博客中提供的Java类型安全的空集合 。 翻译自: https://www.javacodegeeks.com/2012/11/type-safe-empty-collections-in-java.html
http://www.zqtcl.cn/news/373112/

相关文章:

  • 武夷山住房和城乡建设局网站网站提权
  • 电 器建设网站目的及功能定位百度的网站域名
  • 个人备案网站类型网站制作 徐州
  • 北京网站建设推贵州能源网站 中企动力建设
  • 鲅鱼圈网站在哪做vs2013网站开发教程
  • 花艺企业网站建设规划wordpress首页文件
  • 东莞建站模板源码交易所网站开发
  • p2p理财网站开发流程新手怎么搭建网站
  • 阅读网站策划书网站模板建站教程视频
  • 计算机网站开发毕业设计论文开题报告吴中区网站建设技术
  • cdn能为网站上宁波北仑做公司网站
  • wap网站分享到微信福建漳州建设局网站
  • wordpress子站点解析浙江省特种作业证查询官网
  • 长春门户网站建设制作上门做网站哪里有
  • 提卡网站建设西安成品网站建设
  • 广州做餐饮的招聘网站买毕业设计的网站
  • 涡阳网站建设网站开发工程师项目经验
  • 手机网站建站系统成都如何做网站
  • 安徽省住房和建设执业资格注册中心网站优质公司网站
  • 深圳福田做网站公司cname解析对网站影响
  • 做个网站要多久网站制作文案
  • 用户搭建网站wordpress代码实现头像
  • 和平区网站建设app和手机网站
  • 腾讯科技微信小程序电商seo是什么意思啊
  • 手机网站模板更换方法新闻客户端网站开发
  • 湛江定制建站黄页推广app软件
  • 盈利型网站做安卓app用什么软件
  • wordpress优秀移动站点西宁公司网站建设
  • 浙江网站建设的要求建设网上商城网站的目的和意义
  • 西峰住房和城乡建设局网站关于校园网站升级建设的报告