java 方法可以根据条件 return 取值吗?
Can a java method return value depending upon condition?
我有一个 ICoreClient
接口,AClient
和 BClient
类 实现了这个。
ICoreClient
公开给用户。
我需要在 ICoreClient
接口中添加一个新方法。因此,它需要在两个客户端中实施。我不能使此方法通用,因为它具有完全不同的签名但功能相似。
我有 2 个接口 xx
和 yy
ClientA
实现 xx
并且 ClientB
实现 yy
因此,我决定在 ICoreClient
中添加一个新的 testMethod
,它将根据客户为我提供 xx
或 yy
的实例。
我想 return 根据条件从单个方法中获取这些接口的实例。
在ClientA
:
public xx testMethod(){
return instanceof xx;
}
在ClientB
中:
public yy testMethod(){
return instanceof yy;
}
ICoreClient
界面应该写什么?
public zz testMethod()
我尝试放置一个虚拟接口 zz
(作为一个通用超类型)xx
和yy
都在实现这个。但是仍然无法在各自的客户端中公开 xx
和 yy
的方法,因为最终它在 zz
.
中被类型化了
对于这种情况有什么已知的方法吗?
Edit:如果我将 return 类型设为 Object
,则这些接口的方法不会公开。虽然,对象包含 xx
或 yy
、
的实例
用户仍然需要将其转换为(xx
或 yy
用户如何知道?)以使用界面中的方法。我想公开 ClientX
而不必转换为 ClientA
或 ClientB
...
仅当 xx
和 yy
具有共同的超类型(接口或 class)时才有可能。在最坏的情况下,你总是可以 return Object.
public Object testMethod () // or a more specific common super type of `xx` and `yy`
{
if (..some condition..) {
return ..instanceof `xx`..;
} else if (..other condition..) {
return ..instanceof `yy`..;
}
return null; // or some other default instnace
}
因为 returning 对象意味着您必须在客户端投射,您可以改为 return 一个包含您可能的值的自定义对象:
public class MethodResult
{
private xxx xResult;
private yyy yResult;
public MethodResult(xxx xResult){
this.xResult=xResult;
}
public MethodResult(yyy Result){
this.yResult=yResult;
}
public xxx getXResult(){return xResult;}
public yyy getYResult(){return yResult;}
}
然后 return 这种类型:
public MethodResult testMethod ()
{
if (..some condition..) {
return new MethodResult(new xxx());
} else if (..other condition..) {
return new MethodResult(new yyy());;
}
}
}
然后客户端可以检查哪个结果不为空并相应地使用类型,根据设置的是 xxx 或 yyy 上定义的所有方法。或者您可以添加一个方法,该方法允许您检查设置了哪个结果来做出决定而不是检查 null...
编辑后您可能正在寻找 generics。你可以让你的界面像这样
interface ICoreClient<T>{// T will be set by each class implementing this interface
T testMethod();
}
你的每个 类 都可以看起来像
class ClientA implements ICoreClient<xx>{
xx testMethod(){
//return xx
}
}
class ClientB implements ICoreClient<yy>{
yy testMethod(){
//return yy
}
}
我有一个 ICoreClient
接口,AClient
和 BClient
类 实现了这个。
ICoreClient
公开给用户。
我需要在 ICoreClient
接口中添加一个新方法。因此,它需要在两个客户端中实施。我不能使此方法通用,因为它具有完全不同的签名但功能相似。
我有 2 个接口 xx
和 yy
ClientA
实现 xx
并且 ClientB
实现 yy
因此,我决定在 ICoreClient
中添加一个新的 testMethod
,它将根据客户为我提供 xx
或 yy
的实例。
我想 return 根据条件从单个方法中获取这些接口的实例。
在ClientA
:
public xx testMethod(){
return instanceof xx;
}
在ClientB
中:
public yy testMethod(){
return instanceof yy;
}
ICoreClient
界面应该写什么?
public zz testMethod()
我尝试放置一个虚拟接口 zz
(作为一个通用超类型)xx
和yy
都在实现这个。但是仍然无法在各自的客户端中公开 xx
和 yy
的方法,因为最终它在 zz
.
对于这种情况有什么已知的方法吗?
Edit:如果我将 return 类型设为 Object
,则这些接口的方法不会公开。虽然,对象包含 xx
或 yy
、
用户仍然需要将其转换为(xx
或 yy
用户如何知道?)以使用界面中的方法。我想公开 ClientX
而不必转换为 ClientA
或 ClientB
...
仅当 xx
和 yy
具有共同的超类型(接口或 class)时才有可能。在最坏的情况下,你总是可以 return Object.
public Object testMethod () // or a more specific common super type of `xx` and `yy`
{
if (..some condition..) {
return ..instanceof `xx`..;
} else if (..other condition..) {
return ..instanceof `yy`..;
}
return null; // or some other default instnace
}
因为 returning 对象意味着您必须在客户端投射,您可以改为 return 一个包含您可能的值的自定义对象:
public class MethodResult
{
private xxx xResult;
private yyy yResult;
public MethodResult(xxx xResult){
this.xResult=xResult;
}
public MethodResult(yyy Result){
this.yResult=yResult;
}
public xxx getXResult(){return xResult;}
public yyy getYResult(){return yResult;}
}
然后 return 这种类型:
public MethodResult testMethod ()
{
if (..some condition..) {
return new MethodResult(new xxx());
} else if (..other condition..) {
return new MethodResult(new yyy());;
}
}
}
然后客户端可以检查哪个结果不为空并相应地使用类型,根据设置的是 xxx 或 yyy 上定义的所有方法。或者您可以添加一个方法,该方法允许您检查设置了哪个结果来做出决定而不是检查 null...
编辑后您可能正在寻找 generics。你可以让你的界面像这样
interface ICoreClient<T>{// T will be set by each class implementing this interface
T testMethod();
}
你的每个 类 都可以看起来像
class ClientA implements ICoreClient<xx>{
xx testMethod(){
//return xx
}
}
class ClientB implements ICoreClient<yy>{
yy testMethod(){
//return yy
}
}