草庐IT

protected-resource

全部标签

java - 在没有新对象实例的情况下使用 try-with-resources 是错误的形式吗?

一般来说,我总是看到try-with-resources用于分配一个新对象实例,其close()方法被调用为它超出了范围。据我所知,创建一个新对象不是必需的,try-with-resources语法只需要一个局部变量来在超出范围时调用close()。因此,您可以使用它来控制“配对操作”,例如从池中分配一些东西并确保它被返回。例如,下面的MyHandle显示了如何在不再需要时释放池实例://initclassMyHandleimplementsAutoCloseable{booleaninUse=false;publicMyHandleallocate(){inUse=true;retu

java - 在 IntelliJ IDEA 中,如何使用 try-with-resources 包围?

在IntelliJIDEA中,我可以按“Surroundwith”快捷键CTRL-ALT-T用try/catchblock包围一个代码块,等等东西。我想将资源部分包围在一个try-with-resourcesblock中:Writerout=Files.newBufferedWriter(destination,StandardCharsets.UTF_8);temp.process(model,out);对此:try(Writerout=Files.newBufferedWriter(destination,StandardCharsets.UTF_8)){temp.process(

java - 为什么Joda Time中LocalTime的getLocalMillis()是 protected 方法?

我想将joda时间LocalTime转换为毫秒或毫秒。我看到getLocalMillis是一种protected方法。看起来没有办法获取LocalTime的毫秒值。那么,我是否必须以毫秒为单位获取每个字段的值,然后将它们相加以获得总毫秒值?为什么JodaTime没有获取LocalMillis的公共(public)方法? 最佳答案 LocalTime不代表绝对的即时时间,而是描述任意时区的任何一天的时间。通过LocalTime#toDateTimeToday()将您的LocalTime渲染为DateTime或LocalTime#toD

java - 为什么另一个包中的子类无法访问 protected 方法?

我在两个不同的包中有两个类:packagepackage1;publicclassClass1{publicvoidtryMePublic(){}protectedvoidtryMeProtected(){}}packagepackage2;importpackage1.Class1;publicclassClass2extendsClass1{doNow(){Class1c=newClass1();c.tryMeProtected();//ERROR:tryMeProtected()hasprotectedaccessinClass1tryMeProtected();//Noerro

java - Try/Try-with-resources 和 Connection、Statement 和 ResultSet 关闭

我最近和我的教授讨论了如何处理基本的jdbc连接方案。假设我们要执行两个查询,这就是他提出的publicvoiddoQueries()throwsMyException{Connectioncon=null;try{con=DriverManager.getConnection(dataSource);PreparedStatements1=con.prepareStatement(updateSqlQuery);PreparedStatements2=con.prepareStatement(selectSqlQuery);//SettheparametersofthePrepare

java - Try-with-resources 资源范围

在Java7的try-with-resources构造中,我可以在try语句,超出作用域会自动关闭。但是,我没有发现任何关于可用的资源范围的迹象。具体来说,它是否在声明它的tryblock的catch/finallyblock中可用?我在EclipseKepler中尝试了以下操作,但它给人的印象很复杂:资源变量由ContentAssist(代码完成)提供:QuickFix建议更改为资源变量,但这会递归地产生它试图修复的相同问题:在EclipseBugTracker中提出错误之前,我想知道正确的范围限制是什么。 最佳答案 这种语法称为

java - Maven 给出错误 : try-with-resources is not supported in -source 1. 5

尝试使用IntelliJ12.1.4和Java7使用Maven3.0.5创建jar时出现错误。我能够通过IDE毫无问题地运行该项目,但是当我尝试打包它时我得到以下错误。我的POM的相关部分(取自Sonatype的MavenByExample)是:maven-assembly-pluginjar-with-dependencies错误是:[ERROR]...[33,55]error:diamondoperatorisnotsupportedin-source1.5[ERROR]...[207,7]error:try-with-resourcesisnotsupportedin-sourc

java - 为什么Java的try-with-resource中需要声明

Java7的try-with-resources非常棒,但我无法理解为什么需要在try语句中包含资源声明。我的直觉说以下应该是可能的:CloseableResourcething;try(thing=methodThatCreatesAThingAndDoesSomeSideEffect()){//dosomeinterestingthings}thing.collectSomeStats();唉,这会导致语法错误(神秘地期待;)。将类型定义/声明移动到try语句中是可行的,这当然会将thing移动到相应的范围内。当我想从我的AutoClosable中得到更多而不是关闭时,我可以弄清楚

Java:跨包的 protected 访问

我想了解下面示例中发生的情况(通过子类从包外部访问protected成员)。我知道对于包外的类,子类只能通过继承才能看到protected成员。有两个包:package1和package2。package1:ProtectedClass.javapackageorg.test.package1;publicclassProtectedClass{protectedvoidfoo(){System.out.println("foo");}}package2:ExtendsprotectedClass.javapackageorg.test.package2;importorg.test.

java - 为什么 protected 可以在同一个包中访问而无需在 java 中继承?

这个问题在这里已经有了答案:Whydoesthe"protected"modifierinJavaallowaccesstootherclassesinsamepackage?(6个答案)关闭6年前。ModifierClassPackageSubclassWorldpublicYYYYprotectedYYYNnomodifierYYNNprivateYNNNpublicclassa{protectedintx;}publicclassb{b(){aA=newa();A.x=9;//whywecanaccessthisfield?}}请帮我了解下protected在Java中的具体工作