Java-重写方法,该方法具有泛型类型参数,并在调用时对其进行类型转换

Java-重写方法,该方法具有泛型类型参数,并在调用时对其进行类型转换,java,oop,overriding,generic-type-argument,Java,Oop,Overriding,Generic Type Argument,我有一个实用类OldRemote,现在已经被弃用了,但它仍将被使用一段时间,直到新类NewRemote稳定为止。这两个实用程序类具有相同的方法名和参数,但返回类型pojo类不同。即使返回类型pojo结构是相同的,但命名是不同的 简单来说,两种函数返回类型都是具有不同字段名的pojo 在下面的用例中有没有处理这个问题的通用方法 我创建了一个服务接口,它具有新旧类的通用方法契约 public interface RemoteService { //contract [ return typ

我有一个实用类
OldRemote
,现在已经被弃用了,但它仍将被使用一段时间,直到新类
NewRemote
稳定为止。这两个实用程序类具有相同的方法名和参数,但返回类型pojo类不同。即使返回类型pojo结构是相同的,但命名是不同的

简单来说,两种函数返回类型都是具有不同字段名的pojo

在下面的用例中有没有处理这个问题的通用方法

我创建了一个服务接口,它具有新旧类的通用方法契约

public interface RemoteService {

    //contract [ return type is object to receive all/any Pojo classes ]
    Object turnOnTV();

    static Service GetRemoteservice(boolean isOldRemote){
        if(isOldRemote){
            return new OldRemote();
        }
        return new NewRemote();
    }
}
旧远程类

public class OldRemote implements RemoteService{
    @Override
    public OldPojo turnOnTV() {
        OldPojo oldPojo = new OldPojo();
        System.out.println("OldPojo");
        return oldPojo;
    }
}
public class NewRemote implements Service{
    @Override
    public NewPojo turnOnTV() {
        NewPojo newPojo = new NewPojo();
        System.out.println("NewPojo");
        return newPojo;
    }
}

新远程类

public class OldRemote implements RemoteService{
    @Override
    public OldPojo turnOnTV() {
        OldPojo oldPojo = new OldPojo();
        System.out.println("OldPojo");
        return oldPojo;
    }
}
public class NewRemote implements Service{
    @Override
    public NewPojo turnOnTV() {
        NewPojo newPojo = new NewPojo();
        System.out.println("NewPojo");
        return newPojo;
    }
}

演示上述实现的用法

public class DemoTvRemote {
    public static void main(String[] args) {

        RemoteService remoteService1 = RemoteService.GetRemoteservice(true);
        OldPojo oldRemote = (OldPojo) remoteService1.turnOnTV();

        RemoteService remoteService2 = RemoteService.GetRemoteservice(false);
        NewPojo shr = (NewPojo) Service2.test();
    }
}
上述代码工作正常。但问题是我不想在我的整个代码库中使用
turnOnTV()
的所有地方键入cast。即使我必须这样做,我也必须编写一个条件,以便在调用
turnOnTV()
的地方在
OldPojo
NewPojo
之间切换


有没有办法解决这个问题?

您可以创建一个基类或接口,它们都可以扩展/实现

public abstract class RemoteServiceBase<E> {
    public abstract E turnOnTv();
}

public class NewRemoteService extends RemoteServiceBase<NewRemotePojo >{
    public NewRemotePojo turnOnTv() {
         return new NewRemotePojo();
    } 
}


public class OldRemoteService extends RemoteServiceBase<OldRemotePojo >{
    public OldRemotePojo turnOnTv() {
         return new OldRemotePojo();
    } 
}
公共抽象类RemoteServiceBase{
公共摘要E turnOnTv();
}
公共类NewRemoteService扩展了RemoteServiceBase{
公共NewRemotePojo turnOnTv(){
返回新的NewRemotePojo();
} 
}
公共类OldRemoteService扩展了RemoteServiceBase{
公共OldRemotePojo turnOnTv(){
返回新的OldRemotePojo();
} 
}

只有知道服务类型,这仍然有效。否则,您将按照预期使用公共泛型类型

您可以创建它们都扩展/实现的基类或接口

public abstract class RemoteServiceBase<E> {
    public abstract E turnOnTv();
}

public class NewRemoteService extends RemoteServiceBase<NewRemotePojo >{
    public NewRemotePojo turnOnTv() {
         return new NewRemotePojo();
    } 
}


public class OldRemoteService extends RemoteServiceBase<OldRemotePojo >{
    public OldRemotePojo turnOnTv() {
         return new OldRemotePojo();
    } 
}
公共抽象类RemoteServiceBase{
公共摘要E turnOnTv();
}
公共类NewRemoteService扩展了RemoteServiceBase{
公共NewRemotePojo turnOnTv(){
返回新的NewRemotePojo();
} 
}
公共类OldRemoteService扩展了RemoteServiceBase{
公共OldRemotePojo turnOnTv(){
返回新的OldRemotePojo();
} 
}

只有知道服务类型,这仍然有效。否则,您将按照预期使用公共泛型类型

我们可以通过以下方法处理此问题:

1) 我们可以在一个公共位置创建一个伪POJO类,同时将OldPojo和NewPojo的引用作为数据成员

public class CommonPojo  {

     OldPojo oldPojo;
     NewPojo newPojo; 

     public void setOldPojo(OldPojo oldPojo){
         this.oldPojo=oldPojo;
      }

      public void setNewPojo(NewPojo newPojo){
         this.newPojo=newPojo;
      }

     public OldPojo getOldPojo(){
         return oldPojo;
      }

      public NewPojo getNewPojo(){
         return newPojo;
      }
    } 

2) 我们可以编写一个实用方法,如下所示,它可以给出一个commonpojo对象:

public class CommonRemote {


 public  static CommonPojo turnOnTv(Boolean isOldRemote){

    CommonPojo commonPojo = new CommonPojo  

    if(isOldRemote){
         OldPojo oldPojo =new OldPojo();
         commonPojo.setOldPojo(oldPojo);

       }else{
           NewPojo newPojo =new NewPojo();
           commonPojo.setNewPojo (newPojo);
       }

    }

}

3) 将此方法用作turnOnTv(),如下所示:

public class DemoTvRemote {
    public static void main(String[] args) {

       CommonPojo remote1 = CommonRemote.turnOnTv(true);
        OldPojo oldRemote = remote1.getOldPojo();

       CommonPojo remote2 = CommonRemote.turnOnTv(false);
        NewPojo newRemote = remote2.getNewPojo();

    }
}


通过这种方法,代码几乎没有变化,我们可以在不进行任何类型转换的情况下实现您的要求。

我们可以通过以下方法处理此问题:

1) 我们可以在一个公共位置创建一个伪POJO类,同时将OldPojo和NewPojo的引用作为数据成员

public class CommonPojo  {

     OldPojo oldPojo;
     NewPojo newPojo; 

     public void setOldPojo(OldPojo oldPojo){
         this.oldPojo=oldPojo;
      }

      public void setNewPojo(NewPojo newPojo){
         this.newPojo=newPojo;
      }

     public OldPojo getOldPojo(){
         return oldPojo;
      }

      public NewPojo getNewPojo(){
         return newPojo;
      }
    } 

2) 我们可以编写一个实用方法,如下所示,它可以给出一个commonpojo对象:

public class CommonRemote {


 public  static CommonPojo turnOnTv(Boolean isOldRemote){

    CommonPojo commonPojo = new CommonPojo  

    if(isOldRemote){
         OldPojo oldPojo =new OldPojo();
         commonPojo.setOldPojo(oldPojo);

       }else{
           NewPojo newPojo =new NewPojo();
           commonPojo.setNewPojo (newPojo);
       }

    }

}

3) 将此方法用作turnOnTv(),如下所示:

public class DemoTvRemote {
    public static void main(String[] args) {

       CommonPojo remote1 = CommonRemote.turnOnTv(true);
        OldPojo oldRemote = remote1.getOldPojo();

       CommonPojo remote2 = CommonRemote.turnOnTv(false);
        NewPojo newRemote = remote2.getNewPojo();

    }
}


通过这种方法,代码几乎没有变化,我们就可以实现您的要求,而无需任何类型转换。

@Carl是您要找的吗?@Carl是您要找的吗?不需要
RemoteServiceBase
。接口可以是通用的:
公共接口远程服务
啊,你就是这么做的;我错过了。那么,为什么要刻意偏离问题中的代码呢?不需要
RemoteServiceBase
。接口可以是通用的:
公共接口远程服务
啊,你就是这么做的;我错过了。那么,为什么要刻意偏离问题中的准则呢?