java 7和java之间的源代码兼容性8用于重载功能 [英] Source code compatibility between java 7 & 8 for overloaded functions

查看:451
本文介绍了java 7和java之间的源代码兼容性8用于重载功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Java 7&创建了一个jar。 8用于JDBC驱动程序(使用-source / -target编译选项)。但是,我在编译使用 ResultSet 界面中的新/重载方法的应用程序时遇到困难:

I have created a single jar for Java 7 & 8 for a JDBC driver (using -source/-target compile options). However, I am having difficulty compiling applications that use the new/overloaded methods in the ResultSet interface:


// Java 8中的新功能

//New in Java 8

updateObject(int columnIndex,Object x,SQLType targetSqlType)

//在Java 7中可用

// Available in Java 7

updateObject(int columnIndex,Object x,int targetSqlType)

请注意,SQLType是Java 8中引入的新接口。

Note that SQLType is a new interface introduced in Java 8.

我有使用Java 8编译驱动程序,运行正常。但是,当任何使用该驱动程序的应用程序从Java 7访问方法 updateObject(int,Object,int)时,它会收到一个编译错误,指出java.sql的类文件。找不到SQLType,尽管应用程序未使用 SQLType 。我认为这是因为Java查看所有重载方法以确定最具体的方法,并且在这样做时它无法访问Java 8中的新 updateObject 方法(如 SQLType 未在Java 7中定义。知道如何解决这个问题吗?

I have compiled the driver using Java 8, which worked fine. However, when any application using the driver accesses the method updateObject(int, Object, int) from Java 7, it gets a compilation error saying "class file for java.sql.SQLType not found", although the application is not using SQLType. I think this is because Java looks at all the overloaded methods to determine the most specific one, and when doing so it can not access the new updateObject method in Java 8 (as SQLType is not defined in Java 7). Any idea how I can resolve this issue?

请注意 updateObject 方法默认 strong>在Java 8中的 ResultSet 接口中的实现---所以我甚至不能在新方法中使用更通用的类型而不是SQLType。在这种情况下,任何使用new方法的应用程序都会收到编译错误,指出 updateObject 是不明确的。

Note that the updateObject method has a default implementation in the ResultSet interface in Java 8 --- so I can not even use a more generic type instead of SQLType in the new method. In that case any application that uses the new method gets a compilation error saying updateObject is ambiguous.

推荐答案

你不能使用在 Java 8 中编译的东西(对于在较低版本中(例如 Java 7 )。您将获得类似 Unsupported major.minor version ... 的内容。您需要使用两个JAR ,一个用于版本 1.7 ,另一个用于版本 1.8 。最终, 1.7 的那个不能具有 SQLType ,如果该JDK不支持它;另一方面,当您执行 1.8 版本时,建议您维护重载版本。

You can't use something compiled in Java 8 (for instance) in a lower version (say Java 7). You will get something like Unsupported major.minor version.... You need to use two JARs, one for version 1.7 and the other one for version 1.8. Eventually, the one for the 1.7 can't have that SQLType if it's not supported on that JDK; on the other hand, you are encouraged to maintain the overloaded version when you do the 1.8 version.

请注意没有与向后兼容性无关。

Notice that this doesn't have nothing to do with backwards compatibility.

这篇关于java 7和java之间的源代码兼容性8用于重载功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆