有多个参数,或者有没有办法强制使用setter(Java)?
我有一个名为Transactions的java类,有几个方法。特别是,在Sqlite数据库中插入表示新事务的所有值,如下所示:有多个参数,或者有没有办法强制使用setter(Java)?,java,android,sqlite,oop,setter,Java,Android,Sqlite,Oop,Setter,我有一个名为Transactions的java类,有几个方法。特别是,在Sqlite数据库中插入表示新事务的所有值,如下所示: package de.jeanma.stackOverflow; import java.lang.Integer; import java.lang.String; public class Transactions{ private String date, time, category, value, tags, description; pr
package de.jeanma.stackOverflow;
import java.lang.Integer;
import java.lang.String;
public class Transactions{
private String date, time, category, value, tags, description;
private int payee, payer;
public Transactions(){
//Add everything here, needed for the constructor
}
public Transactions(String date, String time, String category, int payee, //I recommend creating an constructor that sets the values as well, beacause you
int payer, String value, String tags, String Description){ // might want to create the object and call the save() directly withoud calling every setter one by one
this.date = date;
this.time = time;
this.category = category;
this.value = value;
this.tags = tags;
this.description = description;
this.payee = payee;
this.payer = payer;
//Add everything here, needed for the constructor as well
}
//Here you can place all your other methods
public void save(){
if(!(isInitialized(date) && isInitialized(time) && isInitialized(category) && isInitialized(value) //here all values are checked if they are initialized.
&& isInitialized(tags) && isInitialized(description) && isInitialized(Integer.valueOf(payee)) //The primitive int's are made to complex Integer's because
&& isInitialized(Integer.valueOf(payer)))){ // the method isInitialized() expects an object
//here you could throw an exception or do something like: System.exit(-1);
}
}
private boolean isInitialized(Object Obj){ // this is the method that's checking if the value is initialized
if(Obj.equals(null)) return false;
else return true;
}
//Add all the setters here (I'm too lazy to do that now)
}
日期;时间类别收款人;付款人;价值标签;说明(…)
当我调用该方法来保存新事务时,它看起来像:
new Transactions().saveNewTransaction(String date, String time, String category, Int Payee, Int Payer, String value, String tags, String Description (...)
我认为这个方法看起来很大,不利于可读代码,最好的方法是将这些字段作为Transactions类的变量,方法saveNewTransaction()不带参数,而是访问类内的变量
唯一的问题是:如何强制一个类(在我的例子中是一个活动类)调用保存新事务所需的所有setter
风险在于调用saveNewTransaction()和几个字段,这些字段的值不是由Activity类设置的(至少该方法确保所有字段都必须由调用方设置)
谢谢如果您的要求是每个创建的事务对象都应该导致一些数据库条目,您可以考虑放弃SaveNWTTAcExchange方法,而在一些“Builder”类中执行保存操作: 当开发人员试图将null传递给带有@NonNull注释的setter时,发出编译器警告 显然,在您的情况下,构建器将有一些不同的with()-方法,即一些与Transaction对象/saveNewTransaction()方法所需的内容相匹配的方法 Ps我没有考虑过你的事务类是关于什么的。但是,如果Transactions类只有一个方法,我将创建上述解决方案的一个变体:
您可以不初始化所有这些变量,或者将它们设置为设置程序不应该出现的值(例如-1) 然后在
newtransactions().save();
中,您需要检查它们是否仍然具有该值
但是这个解决方案不会像您上面写的那样工作,因为您在创建对象时已经保存了事务。在这里,您需要先创建新对象,然后调用所有setter
您的事务类可以如下所示:
package de.jeanma.stackOverflow;
import java.lang.Integer;
import java.lang.String;
public class Transactions{
private String date, time, category, value, tags, description;
private int payee, payer;
public Transactions(){
//Add everything here, needed for the constructor
}
public Transactions(String date, String time, String category, int payee, //I recommend creating an constructor that sets the values as well, beacause you
int payer, String value, String tags, String Description){ // might want to create the object and call the save() directly withoud calling every setter one by one
this.date = date;
this.time = time;
this.category = category;
this.value = value;
this.tags = tags;
this.description = description;
this.payee = payee;
this.payer = payer;
//Add everything here, needed for the constructor as well
}
//Here you can place all your other methods
public void save(){
if(!(isInitialized(date) && isInitialized(time) && isInitialized(category) && isInitialized(value) //here all values are checked if they are initialized.
&& isInitialized(tags) && isInitialized(description) && isInitialized(Integer.valueOf(payee)) //The primitive int's are made to complex Integer's because
&& isInitialized(Integer.valueOf(payer)))){ // the method isInitialized() expects an object
//here you could throw an exception or do something like: System.exit(-1);
}
}
private boolean isInitialized(Object Obj){ // this is the method that's checking if the value is initialized
if(Obj.equals(null)) return false;
else return true;
}
//Add all the setters here (I'm too lazy to do that now)
}
我希望这个awnser能让您满意。您可以拥有一个属性与要保存的值相同的类,然后通过传递新创建的类的实例来创建一个类似saveNewTransaction(object)的方法。。