简体   繁体   English

使用泛型和扩展Abstract类

[英]Using Generics and extending Abstract classes

I've researched this issue a great deal, but have not been very successful in finding a solution close to my situation. 我已经对这个问题进行了大量研究,但是未能成功找到适合我情况的解决方案。 I apologize if I am redundant. 如果有多余,我深表歉意。

Anyway, I am new to generics and so I'm not entirely clear on the syntax. 无论如何,我是泛型新手,因此我对语法尚不完全清楚。 But here is my situation: 但是这是我的情况:

I have an Interface (let's call it IService ) and an implementation ( ServiceImpl ). 我有一个接口(我们称其为IService )和一个实现( ServiceImpl )。 The implementation class has an abstract method that is to be overridden by an extending class ( ServiceExt ). 实现类具有一个抽象方法,该抽象方法将被扩展类( ServiceExt )覆盖。

I have an abstract bean (let's call it AbstractBean ) which has a few fields and is meant to be extended by another bean ( BeanExt ) in order to add some more fields. 我有一个抽象bean(我们称它为AbstractBean ),它具有几个字段,并且打算由另一个bean( BeanExt )进行扩展以添加更多字段。

Ok, now that the situation is laid out, here is my abbreviated code: 好的,现在情况已经摆好了,这是我的缩写代码:

Interface: 接口:

public interface IService <B extends AbstractBean> {
    B method(B bean, Object data) throws Exception;
}

Implementation: 执行:

public abstract class ServiceImpl <B extends AbstractBean> implements IService<B> {
    public abstract B method(B bean, Object data);
}

Abstract Bean : 抽象豆

public abstract class AbstractBean{

    private String fname;

    private String lname;

    [getters and setters]
}

Extended Bean: 扩展Bean:

public class BeanExt extends AbstractBean{

    private String phoneNum;

    [getter and setter]
}

Extending Class: aka Class giving me issues 扩展班:又名班给我问题

public class ServiceExt <B extends AbstractBean> extends ServiceImpl <B> {

    @Override
    public <B> BeanExt method(Bbean, Object data) {
        if(beaninstanceof AbstractBean){
            BeanExt beanExt = (BeanExt) session;
            beanExt.setFname("John");
            beanExt.setLname("Doe");
            beanExt.setPhoneNum("123456789");
            return beanExt;
        }else{
            return null;
        }
    }
}

Ok, so now eclipse is giving me all kinds of non-descriptive issues and I don't know what to do. 好的,所以现在eclipse给了我各种各样的非描述性问题,我不知道该怎么办。 I've played around with the extending class methods a ton but have not found success. 我已经大量使用了扩展类方法,但没有成功。 Above is how it looks after my last tinkering. 以上是我最后一次修修补补后的样子。

The issues currently presented by my IDE within the class ServiceExt are: 我的IDE当前在ServiceExt类中提出的问题是:

  • The method method(B, Object) of type ServiceImpl must override or implement a supertype method ServiceImpl类型的方法method(B,Object)必须重写或实现一个超类型方法
  • The type ServiceExt must implement the inherited abstract method [some other non-abstract method in the interface] 类型ServiceExt必须实现继承的抽象方法[接口中的其他一些非抽象方法]

Edit: For further clarification, what I am ultimately trying to do is: 编辑:为了进一步澄清,我最终想做的是:

  • Create a bean which extends the AbstractBean and add another field 创建一个扩展AbstractBean的bean并添加另一个字段
  • Define the abstract method inside the class ServiceExt 在类ServiceExt中定义抽象方法
  • Within the method, set some values in the extended bean 在方法内,在扩展bean中设置一些值
  • Return the extended bean with its new values 返回具有新值的扩展bean

Update - I've taken all of your input and the changes are reflected below. 更新 -我已接受您的所有输入,所做的更改如下所示。 The issue currently presented by my IDE within the class ServiceExt is: 我的IDE当前在ServiceExt类中提出的问题是:

  • The type ServiceExt must implement the inherited abstract method [some other non-abstract method in the interface] 类型ServiceExt必须实现继承的抽象方法[接口中的其他一些非抽象方法]

Interface: 接口:

public interface IService <B extends AbstractBean> {
    B method(B bean, Object data) throws Exception;
    B otherMethod(HttpServletRequest request) throws Exception;
}

Implementation: 执行:

public abstract class ServiceImpl <B extends AbstractBean> implements IService<B> {
    public abstract B method(B bean, Object data);

    @Override
    public final B otherMethod(HttpServletRequest request) {
        return [something]; 
    }
}

Abstract Bean : 抽象豆

public abstract class AbstractBean{

    private String fname;

    private String lname;

    [getters and setters]
}

Extended Bean: 扩展Bean:

public class BeanExt extends AbstractBean{

    private String phoneNum;

    [getter and setter]
}

Extending Class: aka Class giving me issues 扩展班:又名班给我问题

public class ServiceExt extends ServiceImpl <BeanExt> {

    @Override
    public BeanExt method(BeanExt bean, Object data) {
        BeanExt beanExt = (BeanExt) session;
        beanExt.setFname("John");
        beanExt.setLname("Doe");
        beanExt.setPhoneNum("123456789");
        return beanExt;
    }
}

You are not really overriding the method in super class. 您并没有真正重写超类中的方法。 Here's the overridden method: 这是重写的方法:

@Override
public BeanExt method(BeanExt bean, Object data) {
        BeanExt beanExt = (BeanExt) session;
        beanExt.setFname("John");
        beanExt.setLname("Doe");
        beanExt.setPhoneNum("123456789");
        return beanExt;
}

Since you're extending from ServiceImpl<BeanExt> , the type parameter B is replaced with BeanExt type, so does the return type and parameter of the method. 由于您是从ServiceImpl<BeanExt>扩展而来的,因此类型参数BBeanExt类型替换, BeanExt的返回类型和参数也被替换。

Also note that I've removed the instanceof check, as it is really not needed. 还要注意,我已经删除了instanceof检查,因为实际上并不需要它。 bean will always be an instance of AbstractBean , because that is enforced by the bound you gave to the type parameter declaration of ServiceImpl class. bean将始终是AbstractBean的实例,因为它由您对ServiceImpl类的类型参数声明赋予的绑定强制执行。

I did not entirely understand what you're trying to do, but is this acceptable/working? 我不完全了解您要做什么,但这可以接受吗?

public class ServiceExt <B extends BeanExt> extends ServiceImpl <B> {

    @Override
    public B method(B bean, Object data) {
        bean.setFname("John");
        bean.setLname("Doe");
        bean.setPhoneNum("123456789");
        return bean;
    }
}

I just corrected all the errors: 我只是纠正了所有错误:

interface IService <B extends AbstractBean> {
    B method(B bean, Object data) throws Exception;
}

abstract class ServiceImpl <B extends AbstractBean> implements IService<B> {
    public abstract B method(B bean, Object data);
}

abstract class AbstractBean{

    String fname;
    String lname;
}

class BeanExt extends AbstractBean {

    String phoneNum;
}

class ServiceExt <B extends AbstractBean> extends ServiceImpl <B> {

    @Override public B method(B bean, Object data) {
        if (bean instanceof BeanExt) {
            BeanExt beanExt = (BeanExt) bean;
            beanExt.fname = "John";
            beanExt.lname = "Doe";
            beanExt.phoneNum = "123456789";
            return bean;
        }else{
            return null;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM