如何在Java的构造函数中使用x个参数

huangapple go评论148阅读模式
英文:

How to make a x number of parameters in a constructor java

问题

以下是您要求的翻译部分:

请有人向我展示如何制作一个通用类,用于创建实例并允许您稍后设置和返回值。目前我有以下代码,但正如您所看到的,它非常凌乱,我希望它可以从任意数量的 x 个参数开始。是否有快捷方式可供使用?我是否可以在这方面使用 HashMaps/HashTables/TreeMaps 等?

我之所以想这么做,是为了从 SQL 中获取表格(许多不同的表格)并将它们存储在一个通用类中,然后再使用 HashMap 存储表格的每一行。然后我想为每个表格都有单独的 HashMap。

public class Data {
    String data1, data2, data3, data4, data5, data6, data7, data8, data9;

    public Data(String data1) {
        this.data1 = data1;
    }
    
    public Data(String data1, String data2) {
        this.data1 = data1;
        this.data2 = data2;
    }
    
    // 持续进行此过程,直到为每个添加的参数添加一个新构造函数
    
    public Data(String data1, String data2, String data3, String data4, String data5, String data6, String data7, String data8, String data9) {
        this.data1 = data1;
        this.data2 = data2;
        this.data3 = data3;
        this.data4 = data4;
        this.data5 = data5;
        this.data6 = data6;
        this.data7 = data7;
        this.data8 = data8;
        this.data9 = data9;
    }
    
    // 对类中的每个变量重复下面的两行
    public void setData1(String data1) { this.data1 = data1; }
    public String getData1(String data1) { return data1; }
}

请注意,代码部分已按照您的要求进行翻译,但是您提供的代码可能需要一些重构,以实现更清晰和可维护的解决方案。如果您想了解有关如何改进这段代码的建议,请随时提问。

英文:

Someone please show me how to make a generic class that creates an instance and lets you set and return the values later. Right now I have the below code, but as you can tell, its very messy and I'd like it to have x number of parameters from any number. Is there a short cut to this? Is there a way I can use HashMaps/HashTables/TreeMaps/etc. for this?

The reason I want to do this is to take a table from SQL (many different tables) and store them in a generic class and then a HashMap to store every row of the table. Then I want individual HashMaps for every table.

public class Data {
	String data1, data2, data3, data4, data5, data6, data7, data8, data9;

	public Data(String data1) {
		this.data1 = data1;
	}
	public Data(String data1, String data2) {
		this.data1 = data1;
		this.data2 = data2;
	}
	//Continue this process until there is a new constructor for every added parameter

	public Data(String data1, String data2, String data3, String data4, String data5, String data6, String data7, String data8, String data9) {
		this.data1 = data1;
		this.data2 = data2;
		this.data3 = data3;
		this.data4 = data4;
		this.data5 = data5;
		this.data6 = data6;
		this.data7 = data7;
		this.data8 = data8;
		this.data9 = data9;
	}
	
    //Repeat the next two lines for every variable in class
	public void setData1(String data1) {this.data1 = data1;}
	public String getData1(String data1) {return data1;}
}

答案1

得分: 0

如此记录在此处 https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html#varargs
您可以使用“任意数量的参数”

即:

public class Data {

    public Data(String... strings) {
        int numberOfStrings = strings.length;
        // String data1 = strings[0]; // 第一个参数

    }
}

new Data(); // 无参数
new Data(new String[]{"data1"}); // 一个参数
new Data("data1"); // 一个参数
new Data(new String[]{"data1", "data2", "data3"});
new Data("data1", "data2", "data3");
英文:

As documented here https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html#varargs
you can use "Arbitrary Number of Arguments"

Ie:

public class Data {

    public Data(String... strings) {
        int numberOfStrings = strings.length;
        // String data1 = strings[0]; // first arg

    }
}

new Data(); // no parameter
new Data(new String[]{"data1"}); // one parameter
new Data("data1"); // one parameter
new Data(new String[]{"data1", "data2", "data3"});
new Data("data1", "data2", "data3");

答案2

得分: 0

虽然我对于问题中的SQL部分不是很确定,但总体而言,构建者设计模式是一种很好的方式,用于逐步构建一个类,而不是使用过多的构造函数参数,特别是当您计划拥有具有大量参数的构造函数时。

要使用它,我们需要创建另一个构建者类,该类可以逐个参数地构建我们的对象参数。因此,例如针对问题中的Data类,我们可以编写:

public class DataBuilder {
    // 为它们提供一些默认值,以防它们没有被用户初始化
    private String data1 = "";
    private String data2 = "";
    private String data3 = "";
    private String data4 = "";
    private String data5 = "";
    private String data6 = "";
    private String data7 = "";
    private String data8 = "";
    private String data9 = "";

    public DataBuilder() {
    }

    // 返回对自身的引用,以支持链式调用
    public DataBuilder data1(String data1) {
        this.data1 = data1;
        return this;
    }

    public DataBuilder data2(String data2) {
        this.data2 = data2;
        return this;
    }

    // 对于所有字段,都可以采用类似的方法

    // 从DataBuilder最终构建Data对象的方法
    public Data build() {
        return new Data(data1, data2, data3, data4, data5, data6, data7, data8, data9);
    }
}

// 使用链式调用一步构建
Data data = new DataBuilder().data1("data1").data2("data2").build();

// 分多步构建
DataBuilder builder = new DataBuilder();
builder.data1("data1");
builder.data2("data2");

Data data = builder.build();
英文:

While I'm not too sure on the SQL portion of the question, in general, the Builder Design Pattern is a great way to incrementally construct a class, rather than overloaded constructors, especially when you plan to have constructors with a large number of arguments.

To use it, we would create another builder class that can be used in building our object parameter by parameter. So for instance, for the Data class in the question, we could write:

public class DataBuilder {
        // give them some default value in case they aren't initialized by the user
        private String data1 = "";
        private String data2 = "";
        private String data3 = "";
        private String data4 = "";
        private String data5 = "";
        private String data6 = "";
        private String data7 = "";
        private String data8 = "";
        private String data9 = "";
        
        public DataBuilder() {
            return new DataBuilder();
        }
        
        // returns a reference to itself to allow for chaining
        public DataBuilder data1(String data1) {
            this.data1 = data1;
            return this;
        }
        
        public DataBuilder data2(String data2) {
            this.data2 = data2;
            return this;
        }
        
        // and so on for all of the fields
        
        // a method to finally build the Data object from the DataBuilder
        public Data build() {
            return Data(data1, data2, data3, data4, data5, data6, data7, data8, data9);
        }    
    }

// build in one step with chaining
Data data = new DataBuilder().data1("data1").data2("data2").build();

// build in multiple steps
DataBuilder builder = new DataBuilder();
builder.data1("data1");
builder.data2("data2");

Data data = builder.build();

huangapple
  • 本文由 发表于 2020年10月15日 07:20:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64362766.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定