简体   繁体   English

每次扩展类并需要其构造函数来触发时,都必须调用super吗?

[英]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.

相关问题 当其超类具有较少参数时,如何向子类构造函数添加更多参数? - How do I add more arguments to a subclass constructor when its super class has fewer arguments? 我可以调用超级构造函数并在java中传递类名吗? - Can I call the Super Constructor and pass the Class name in java? 如果我从类文件中删除超级构造函数调用会发生什么? - What happens if I remove the super constructor call from class file? 我已经编写了构造函数类,但是我需要对其进行测试。 我怎么做? - I have my constructor class written, but I need to test it. How do I do that? 为什么在抽象类中需要构造函数? - Why do i need a constructor in an abstract class? 准备好创建类的对象后,如何调用no arg构造函数? - How do I call the no arg constructor after I have allready created an object of the class? 在Java中,要使用“super”关键字,我是否必须导入目标类? - In Java, to use the “super” keyword, do I have to import the target class? 了解数组 Object,我想做的是在我拥有的每个 class 中调用我的数组 - understand Array Object, and what I want to do is call my array in every class I have 在java中为什么总是子类调用超类构造函数? 我想要内部流程 - In java why always child class call the super class constructor ? I want to the internal flow 当我需要使用带有参数的构造函数时,如何使用Rhino子类化(扩展)Java类? - How can I subclass (extend) a Java class using Rhino when I need to use a constructor that takes an argument?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM