带有变量参数列表的抽象方法 [英] Abstract method with variable list of arguments

查看:371
本文介绍了带有变量参数列表的抽象方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有找到解决这个问题的优雅方法。我有一个抽象类,其他几个类继承了一个抽象方法,该方法可以包含0到4-5个不同类型的参数。

I haven't quite found an elegant way to solve this issue. I have an abstract class that several other classes are inheriting with an abstract method that can contain anywhere from zero to 4-5 arguments of varying types.

public abstract class Item {
public abstract void use();
}

例如,我有一个继承了这个的Book类,并且在没有参数时重写use(),我有一个Key类,它继承并在重写时将String和Queue作为参数...等等。

For instance, I have a Book class that inherits this and takes no arguments when overriding use(), I have a Key class that inherits and takes a String and a Queue as arguments when overriding, etc...

我尝试过使用泛型但是我必须在实际取决于类时输入使用的数字,例如Item。

I've tried using generics but I have to input the number used, such as Item, when it actually depends on the class.

public abstract class Item<T,U> {
public abstract void use(T arg1, U arg2); //Number of arguments/types could be more or less
}

我试过了发送一个变量的对象列表,但对象类型总是可变的,我不确定在继承类中接收的语法。

I've tried sending a variable list of Objects but the object types are always variable and I've unsure as to the syntax to receive in the inheriting classes.

public abstract class Item<T> {
public abstract void use(T... arguments);
}

public class Book extends Item<?> {
public void use(?);
}

public class Book extends Item<String, Queue> { //Wrong number of arguments since I can't use Item<T...>
public void use(String str, Queue q); //fails
}

我可能只是做错了什么 - 任何人都可以提供任何帮助或洞察力?

I may just be doing something wrong - can anyone offer any assistance or insight?

推荐答案

我一直在努力解决同样的问题,而且没有一个完美的答案,但我可以给你一个几件事情要考虑。首先,您基本上是在尝试做一些本质上违反面向对象编程的事情,即您正在尝试创建一个可变接口。接口的关键是获取对象的抽象版本(例如Item而不是Book)的代码知道如何调用use()方法。这意味着他们必须知道可以传递给use()方法的内容。如果答案取决于抽象类或接口的实现,那么你需要确保使用它的代码实际上知道它正在使用什么样的实现(Book等),否则它不会知道如何调用use ()具有适当的参数。听起来你需要仔细地重构你的代码。

I've struggled with the same question, and there's not a perfect answer, but I can give you a few things to consider. First, you're basically trying to do something that is inherently against Object Oriented Programming, which is that you're trying to create a variable interface. The point of an interface is that code that gets an abstract version of the object (the Item rather than the Book, for example), knows how to invoke the use() method. This means that they must know what can be passed to the use() method. If the answer depends on the implementation of the abstract class or interface, then you need to ensure that the code using it actually knows what kind of implementation (Book, etc.) that it's using, otherwise it's not going to know how to invoke use() with the appropriate parameters anyway. It sounds like you need to refactor your code, in all honesty.

但是,有一种方法可以回答你提出的问题,而无需重构架构。您可以创建一个类,其中的数据是可能传递给use()方法的所有不同类型的参数,让调用代码设置该类的字段,然后将其传递给use()方法。例如:

However, there is a way to answer your question as stated without refactoring the architecture. You could create a class that's data is all of the different types of parameters that could possibly be passed to the use() method, have the calling code set the fields of that class, and then pass that to the use() method. For example:

public class UseParameters {
    private String string;
    private Queue queue;
    // Any other potential parameters to use(...)

    public void setString(String string) {
        this.string = string;
    }

    public String getString() {
        return string;
    }

    // All of the other accessor methods, etc.
}

然后,您可以在Item中定义使用方法,如下所示:

Then, you could define the use method in Item like this:

public abstract void use(UseParameters params);

使用Item的任何代码都必须适当地设置对象的参数:

And any code using an Item would have to set the parameters of the object appropriately:

Item item = // However you're going to get the item
UseParameters params = new UseParameters();
params.setString("good string");
params.setQueue(new Queue());
item.use(params);

我只想指出,如果上面的代码知道Item是一本书(这是怎么回事它知道设置字符串和队列,那么为什么不只是得到一本书并且跳过需要一个带有变量use()方法的抽象类?但是我离题了。无论如何,本书将实现use()方法,如下所示:

I just want to point out that if the code above knows the Item is a Book (which is how it knows to set the String and Queue, then why not just get a Book and skip needing an abstract class with a variable use() method altogether? But I digress. Anyway, the Book would then implement the use() method like so:

@Override
public void use(UseParameters params) {
    if(params.getString == null || params.getQueue() == null)
        // throw exception

    // Do what books do with strings and queues
}

我认为这可以让你得到你想要的,但我认为你应该考虑重构。

I think that gets you what you want, but you should consider refactoring, I think.

这篇关于带有变量参数列表的抽象方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆