[英]Do I have to call super every time I extend a class and need its constructor to trigger?
Is there any way I can trigger class constructor automatically while extending from the class? 从类扩展时,有什么方法可以自动触发类构造函数?
I have a class TestSet.java 我有一个类TestSet.java
public class TestSet {
public TestSet(String name) {
Logger.msg("Test set: " + name);
}
}
And I would like for the constructor to trigger every time that I extend from this class. 我希望构造函数在每次从此类扩展时都触发。
It seems that I need to call the constructor "manually" with: 看来我需要用以下方式“手动”调用构造函数:
public class TC1SendAnEmail extends TestSet{
//I have to type this every single time again and again...
//---------------------------------------------------------
public TC1SendAnEmail(String name) {
super(name);
}
//---------------------------------------------------------
public void run() {
new EmailLogin().run();
...
}
}
Which I would like to avoid. 我想避免。 (Because I will be creating possibly hundreds/thousands of those extended classes.)
(因为我可能会创建成百上千的扩展类。)
From what I have managed to research, I guess that this function is not implemented in Java. 从我的努力研究中,我猜想该功能未在Java中实现。 But it just seems weird that I would have to "copy-paste" the constructor again, again and again...
但是我似乎不得不一次又一次地“复制粘贴”构造函数,这似乎很奇怪。
Maybe there is another solution that I dont see? 也许还有另一种我看不到的解决方案? (Maybe without even using the constructor to "do something every time an instance of a class that extends my TestSet class is created".)
(也许甚至没有使用构造函数“每次创建扩展我的TestSet类的类的实例时都做某事”。)
EDIT: Yes, I can see why you think that creating hundreds/thousands of subclasses is wrong. 编辑:是的,我可以看到您为什么认为创建成千上万的子类是错误的。 I am creating a big automation project.
我正在创建一个大型自动化项目。 Every class of this type will be a "test".
这种类型的每个类都是一个“测试”。 And there will be thousands of tests...
将会有成千上万的测试...
EDIT#2: The point of this question was that I needed to trigger the superclass constructor every time I extend from it. 编辑#2:这个问题的重点是我每次从超类构造函数扩展时都需要触发它。 My mistake was adding parameter to the superclass constructor.
我的错误是在超类构造函数中添加了参数。 If you don´t add a parameter to the constructor, then it is triggered automatically while extending.
如果不向构造函数添加参数,则在扩展时会自动触发该参数。
From your comments I kinda understand what you're trying to achieve. 从您的评论中,我有点理解您要实现的目标。 (Automatically printing the name of the current running test)
(自动打印当前正在运行的测试的名称)
So what about the following snippet: 那么下面的代码片段呢?
public class TestSet {
public TestSet(){
Logger.msg("Test set: " + getClass().getSimpleName());
}
}
This baseclass just prints the name of the implementing class when it is created. 该基类在创建时仅打印实现类的名称。 Eg when using the following class:
例如,使用以下类别时:
public class TC1SendAnEmail extends TestSet {
// your methods
}
it prints: 它打印:
Test set: TC1SendAnEmail
测试集:TC1SendAnEmail
This works, because in java the default constructor (constructor with no arguments) of the superclass doesn't have to be overridden, because the compiler will generate it automatically. 之所以可行,是因为在Java中不必重写超类的默认构造函数(不带参数的构造函数),因为编译器会自动生成它。
Yes. 是。
Otherwise, how will it know that you want to pass name
into it and not some other parameter? 否则,它将如何知道您要将
name
而不是其他参数传递给它?
What you can do is replace this copy-paste code with some sort of Template method pattern : 您可以做的是将这种复制粘贴代码替换为某种Template方法模式 :
public class SO50215241 {
public abstract static class TestSet {
public TestSet() {
System.out.println("Test set: " + getName());
}
abstract String getName();
}
public static class TC1SendAnEmail extends TestSet{
@Override
String getName() {
return "TC1Name";
}
}
public static void main(String[] args) {
new TC1SendAnEmail();
}
}
Prints: 印刷品:
Test set: TC1Name
Then you can try to extend some specific version of TestSet
for concrete implementations instead of extending TestSet
itself. 然后,您可以尝试扩展一些特定版本的
TestSet
以实现具体实现,而不是扩展TestSet
本身。 Or calculate name dynamically inside getName
method body. 或在
getName
方法主体内部动态计算名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.