我将sama java对象TestData输入到包(A& B)中。我已经创建了一个处理标准业务功能对象的函数。
CommonFunc.java:
import A.TestData ;
class CommonFunc
{
/// .....
public static TestData processTestData(Date d1, String s1){
TestData testData = new TestData ();
/// set some testData porperties based on d1 and s1
/// e.g : testData.setInitialDate(d1);
return testData ;
}
}
这里的问题是编译器必须从其中一个包加载对象,比如包(A),所以当我希望数据从包(B)返回到局部变量时,我得到不兼容的类型错误:
使用B TestData的文件,需要调用函数processTestData:
import B.TestData;
// ...
TestData obj = CommonFunc.processTestData(new Date(), "test");
// ...
有没有办法克服这个问题,保持两者的共同功能?
答案 0 :(得分:2)
有没有办法克服这个问题,保持两者的共同功能?
不,是的。在一般情况下,你不能。
但是你可以, IFF 你可以使这两个类采用相同的接口,并在同一个接口中声明常用方法。请参阅下文,并对类名称的更改道歉:
$(window).on("beforeunload", function() {
//Write your ajax code here
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: 'Your URL',
data: { "transactionno": transactionNo },
success: function (data) {
$('#Loader').hide();
},
error: function (e) {
$('#Loader').hide();
}
});
})
答案 1 :(得分:1)
我没有看到你发布的上述例子是如何可能的,最好的方法是使TestData成为一个接口并在2个包中实现。然后,要决定是返回TestDataImpl还是B TestDataImpl,请在processData中取另一个参数,为简单起见,我们先说一个布尔值。基于true或false实例化A TestDataImpl或B TestDataImpl并返回相同的内容。其中processData的返回类型是接口类型
这可能是重用processData方法最直接的方法。