Java - 覆盖方法,它具有泛型类型参数,并在调用时将其类型化

问题描述 投票:1回答:2

我有一个实用程序类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();
    }
}

OldRemote类

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

NewRemote类

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()的所有地方输入强制类型。即使我必须这样做,我也必须写一个条件来切换OldPojoNewPojo,其中turnOnTV()被调用。

有什么方法可以解决这个问题吗?

java oop override generic-type-argument
2个回答
0
投票

您可以创建一个扩展/实现的基类或接口。

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();
    } 
}

如果您知道服务类型,这仍然有效。否则,您可以按照预期使用通用泛型类型。


0
投票

我们可以用以下方法处理这个问题:

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();

    }
}

使用这种方法,代码变化很小我们可以在没有任何类型转换的情况下实现您的需求。

© www.soinside.com 2019 - 2024. All rights reserved.