Java-如何根据继承类调用不同的super();什么是构造函数参数?
我试图使继承类要求更少的参数,并计算超级类的“正确”错误参数。 正在寻求有关如何在不使用工厂方法的情况下执行此操作的帮助 这是一个简化事情的示例代码。Son(int)将根据int的值调用super(int,boolean)Java-如何根据继承类调用不同的super();什么是构造函数参数?,java,inheritance,super,Java,Inheritance,Super,我试图使继承类要求更少的参数,并计算超级类的“正确”错误参数。 正在寻求有关如何在不使用工厂方法的情况下执行此操作的帮助 这是一个简化事情的示例代码。Son(int)将根据int的值调用super(int,boolean) class Base { ... public Base (int num, boolean boo2) { ...} ... } class Son extends Base { ... public Son (int num) { if (n
class Base {
...
public Base (int num, boolean boo2) { ...}
...
}
class Son extends Base {
...
public Son (int num) {
if (num > 17)
super(num, true);
else
super(num , false);
}
...
}
我还考虑将Base作为接口,但这不允许我强制执行一些参数正确性检查
谢谢你的帮助。我不是100%肯定,但这行得通吗
class Son extends Base {
...
public Son (int num) {
super(num, (num>17));
}
...
}
super()调用必须是构造函数中的第一条语句。在这种情况下,您可以使用:
class Base {
public Son(int num) {
super(num, num > 17);
}
}
如果需要执行的计算工作长于单个表达式,则可以将其移动到从构造函数调用的静态方法中:
class Son extends Base {
public Son(int num) {
super(num, calcBoo(num));
}
private static boolean calcBoo(int num) {
if (num > 17)
return true;
else
return false;
}
}
class Son extends Base {
private Son(int num, boolean boo) {
super(num, boo);
}
public static Son create(int num) {
boolean boo;
// ... statements here ... //
return new Son(num, boo);
}
}
另一个选项是隐藏构造函数并添加静态工厂方法,这将允许您在调用超级构造函数之前执行任意复杂的工作:
class Son extends Base {
public Son(int num) {
super(num, calcBoo(num));
}
private static boolean calcBoo(int num) {
if (num > 17)
return true;
else
return false;
}
}
class Son extends Base {
private Son(int num, boolean boo) {
super(num, boo);
}
public static Son create(int num) {
boolean boo;
// ... statements here ... //
return new Son(num, boo);
}
}
请注意,构造函数调用必须是构造函数中的第一条语句。因此,您需要:
public Son (int num) {
super(num, (num>17));
}
这将做同样的事情,因为num>17
将被计算为true
或false
,并且它是构造函数中的第一条语句,因此它将被编译
见:
调用超类构造函数必须是
子类构造函数
构造函数中的第一行必须是对super的调用,否则就不能使用它 为什么不将行为提升到基类呢?无论如何,这将是解决这个问题的更好办法 所以你会有这样的想法:
Son {
super(num,17); //in base, you have a "break point" parameter
....
}
Class Son extends Base {
private static boolean getMyBoolean(int num) {
return num > 17; //or any complex algorithm you need.
}
public Son (int num) {
super(num, getMyBoolean(num));
}
...
}
我假设您询问的是对super的调用必须是构造函数中的第一行的要求
您可以像其他答案一样使用一个简单的布尔表达式,也可以泛化解决方案(如果您的示例就是这样,那么就是一个示例),并使用返回正确值的内联函数调用。您还可以检查三元运算符。如果查找其他参数是一个复杂的操作(即,不能简化为单个表达式),您可以添加一个静态方法,为您执行此操作,并在超级调用中引用它,例如:
Son {
super(num,17); //in base, you have a "break point" parameter
....
}
Class Son extends Base {
private static boolean getMyBoolean(int num) {
return num > 17; //or any complex algorithm you need.
}
public Son (int num) {
super(num, getMyBoolean(num));
}
...
}
否则,如果可以使用简单表达式(如您给出的具体示例中所示)计算缺少的参数,只需编写:
Class Son extends Base {
public Son (int num) {
super(num, num > 17);
}
...
}
从:
构造函数体的第一条语句可以是对同一类或直接超类()的另一个构造函数的显式调用
用
super进行测试(num,num>17)代码>你有两个或一个超级构造函数吗?考虑重新设计。@SotiriosDelimanolis对此表示感谢,这与我的具体案例无关(这实际上是继承而不是组合),但阅读起来很有趣。如果组合不是选项,请避免创建同一类的多个风格,您的calcBoo
可以在一行中。。只是returnnum>17
@nachokk我知道,但我这样写是为了说明它可以做构造函数不能做的事情:多个语句。这很好。它也符合我的实际情况,即查找参数比(num>17)要复杂一些。另外,调用super()时使用该条件的原因也更加清楚。