Java - 在超级方法中使用条件指定参数
Java - Using a Conditional To Specify a Parameter in a Super Method
我有一个从另一个 class 扩展而来的参数化方法。抽象 class Account 是父类,SavingsAccount 从它继承构造函数。此构造函数是参数化构造函数。我想使用条件允许(或禁止)某些值进入双 init_balance 字段,然后为父构造函数调用 super() 。
if(init_balance < minimumBalance){
//Print message to user asking for larger deposit. (is this enough to do?)
}
但是 java 语言要求我首先在子构造函数中调用父构造函数。所以我无法通过子构造函数过滤进入父构造函数的内容。
这是我在 gist
上的代码
我相信这回答正确:Why do this() and super() have to be the first statement in a constructor?
但基本上你需要把super(init_id, init_balance);
放在第一位。
正如其他人所说,该语言要求您首先调用超类构造函数(我相信这是为了避免子类构造函数在超类字段初始化之前访问超类字段或方法可能出现的问题)。通常这不是问题,只是会浪费几纳秒。如果这确实是一个问题——如果违反约束,超类构造函数会做一些你不想做的事情——将子类构造函数设为私有并使用工厂方法:
class SavingsAccount extends Account {
private SavingsAccount(String init_id, double init_balance)
{
super(init_id, init_balance);
}
public static SavingsAccount newSavingsAccount(String init_id, double init_balance) {
if (init_balance < minimumSavings) {
System.out.println("Sorry, but you need to give us moar moneyz!");
throw new Exception("Not enough money.");
}
return new SavingsAccount(init_id, double init_balance);
}
}
您不能再在其余代码中说 new SavingsAccount(id, balance)
;你必须说 SavingsAccount.newSavingsAccount(id, balance)
。但是这个价格可能是值得的,这取决于您的需求。
class Account
{
public Account(String init_id, double init_balance)
{
super(init_id, init_balance);
}
public void condition_Method()
{
//your condition create object only if condition satisfied or else give //error message
Account object=new Account(init_id, init_balance);
}
}
如果你想保留基于构造函数的对象创建,你可以想出这个:
public class SavingsAccount extends Account {
private SavingsAccount(String init_id, double init_balance)
{
super(init_id, validate(init_balance));
}
public static double validate(double init_balance) {
if (init_balance < minimumSavings) {
System.out.println("Message");
throw new RuntimeException("Message"); // or handle this error
}
return init_balance;
}
}
但是 - 查看您的示例,我可能会选择在构造函数外部进行验证。
我有一个从另一个 class 扩展而来的参数化方法。抽象 class Account 是父类,SavingsAccount 从它继承构造函数。此构造函数是参数化构造函数。我想使用条件允许(或禁止)某些值进入双 init_balance 字段,然后为父构造函数调用 super() 。
if(init_balance < minimumBalance){
//Print message to user asking for larger deposit. (is this enough to do?)
}
但是 java 语言要求我首先在子构造函数中调用父构造函数。所以我无法通过子构造函数过滤进入父构造函数的内容。
这是我在 gist
上的代码我相信这回答正确:Why do this() and super() have to be the first statement in a constructor?
但基本上你需要把super(init_id, init_balance);
放在第一位。
正如其他人所说,该语言要求您首先调用超类构造函数(我相信这是为了避免子类构造函数在超类字段初始化之前访问超类字段或方法可能出现的问题)。通常这不是问题,只是会浪费几纳秒。如果这确实是一个问题——如果违反约束,超类构造函数会做一些你不想做的事情——将子类构造函数设为私有并使用工厂方法:
class SavingsAccount extends Account {
private SavingsAccount(String init_id, double init_balance)
{
super(init_id, init_balance);
}
public static SavingsAccount newSavingsAccount(String init_id, double init_balance) {
if (init_balance < minimumSavings) {
System.out.println("Sorry, but you need to give us moar moneyz!");
throw new Exception("Not enough money.");
}
return new SavingsAccount(init_id, double init_balance);
}
}
您不能再在其余代码中说 new SavingsAccount(id, balance)
;你必须说 SavingsAccount.newSavingsAccount(id, balance)
。但是这个价格可能是值得的,这取决于您的需求。
class Account
{
public Account(String init_id, double init_balance)
{
super(init_id, init_balance);
}
public void condition_Method()
{
//your condition create object only if condition satisfied or else give //error message
Account object=new Account(init_id, init_balance);
}
}
如果你想保留基于构造函数的对象创建,你可以想出这个:
public class SavingsAccount extends Account {
private SavingsAccount(String init_id, double init_balance)
{
super(init_id, validate(init_balance));
}
public static double validate(double init_balance) {
if (init_balance < minimumSavings) {
System.out.println("Message");
throw new RuntimeException("Message"); // or handle this error
}
return init_balance;
}
}
但是 - 查看您的示例,我可能会选择在构造函数外部进行验证。