简体   繁体   English

如何在Java中创建自上而下的重载构造函数

[英]How to create a top-down overload constructor in java

I am currently working on an android project, where I want to create an object with a dynamic number of parameters. 我目前正在开发一个android项目,我想在其中创建一个具有动态数量参数的对象。

A RequestOperation object will always have the attribute Type and shall be able to be created with 0 to 3 optional string attributes, which are stored in an ArrayList<String> attribute called requestParams . 一个RequestOperation对象将始终具有Type属性,并应能够使用0到3个可选的字符串属性来创建,这些属性存储在一个名为requestParamsArrayList<String>属性中。

When writing the constructors the following idea came into my mind: 在编写构造函数时,我想到了以下想法:

Is it possible to create the constructors "pesudo-recursive", so that the constructor method, that takes three strings, adds the 3rd one to the object and calls the constructor, that takes two strings. 是否可以创建“伪伪递归”构造函数,以便采用三个字符串的构造函数方法,将第三个字符串添加到对象,并调用采用两个字符串的构造函数。 This one would add the 2nd one to the object and then call the constructor that takes one string, which would do the same, and then passes the object with the filled requestParams arraylist to the constructor that takes no strings and just adds the Type attribute. 这将向对象添加第二个对象,然后调用采用一个字符串的构造函数,该操作将执行相同的操作,然后将带有填充的requestParams数组列表的对象传递给不包含字符串的构造函数,仅添加Type属性。

That would save a lazy programmer from writing (in fact long) constructors over and over again. 这样可以避免懒惰的程序员一次又一次地编写(实际上是很长时间)构造函数。

Here's how I imagined that (strongly simplified): 这是我的想象(非常简化):

public class RequestOperation {

    public enum Type {
        //enum values and mappings
    }

    private String applicationUrl;
    private ArrayList<String> requestParams = new ArrayList<>();

    public RequestOperation(Type operationType) {
        this.applicationUrl = composeUrl(operationType, requestParams);
    }

    public RequestOperation(Type operationType, String paramFirst) {
        this.requestParams.add(0, paramFirst);
        new RequestOperation(operationType);
}

    public RequestOperation(Type operationType, String paramFirst, String paramSecond) {
        this.requestParams.add(0, paramSecond);
        new RequestOperation(operationType, paramFirst);
    }

    public RequestOperation(Type operationType, String paramFirst, String paramSecond, String paramThird) {
        this.requestParams.add(0, paramThird);
        new RequestOperation(operationType, paramFirst, paramSecond);
    }

With this approach I'm afraid, that I would create a new object inside the constructors and the params would get written to the wrong objects. 恐怕使用这种方法时,我会在构造函数内部创建一个新对象,并且参数将被写入错误的对象。 Is there by any chance a way to pass the object itself to be the target of the following constructor call? 是否有机会将对象本身传递为以下构造函数调用的目标? If this is a bad approach at all, please let me know, i am still learning, how to program properly. 如果这根本不是一个好的方法,请告诉我,我仍在学习如何正确编程。

You could use varargs in a form such as: 您可以使用以下形式的varargs:

public RequestOperation(Type operationType, String... parameters) {
    for(String param : parameters) {
       requestParams.add(param);
   }
}

This has the added benefit that you can also add as many request parameters as you wish. 这样做的好处是,您还可以根据需要添加任意数量的请求参数。

EDIT: as mentioned in the comments by PPartisan an even more concise way would be 编辑:如PPartisan的评论中所述,一种更为简洁的方法是

public RequestOperation(Type operationType, String... parameters) {
   requestParams = Arrays.asList(parameters);
} 

Probably it would be a better approach to define a static method for the class RequestOperation called like newInstance which takes as parameters the Type object and a List<String> and then recursively calls itself until the length of the List is 0, then it returns the RequestOperation created with the constructor that takes the Type object. 为类RequestOperation定义一个静态方法(如newInstance将是一个更好的方法,该方法将Type对象和List<String>作为参数,然后递归调用自身,直到List的长度为0,然后返回使用带有Type对象的构造函数创建的RequestOperation。

I'd say it's a better approach than the one you described because with your method you're creating a new RequestOperation object each recursive call, I think it's not a very good practice. 我想说这是一种比您描述的方法更好的方法,因为使用您的方法将在每个递归调用中创建一个新的RequestOperation对象,我认为这不是一个很好的做法。

Hope it helps! 希望能帮助到你!

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

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