简体   繁体   English

我可以避免在子类中实现参数化构造函数

[英]Can I Avoid Implementing Parameterized Constructors in Sub-classes

I have an abstract class with a 1-param constructor which should be identical for any concrete sub-class. 我有一个带有1-param构造函数的抽象类,它对任何具体的子类都应该是相同的。 Does every concrete sub-class have to have that same 1-param constructor, and if so, why? 每个具体的子类都必须具有相同的1-param构造函数吗?如果是,为什么?

Abstract: 抽象:

public abstract class AbstractClass {

public AbstractClass(String name){}

public AbstractClass getConcreteClass(){
    return (AbstractClass) new ConcreteClass("name");   //Does not work
}

}

Concrete: 具体:

public class ConcreteClass { /*Would like to have no constructor*/ }

Each class must have at least one constructor. 每个类必须至少具有一个构造函数。 In classes without an explicit constructor, there is the implicit constructor without any arguments and without any special initialization. 在没有显式构造函数的类中,存在没有任何参数且没有任何特殊初始化的隐式构造函数。

If your parent class declares a constructor, children cannot use an implicit constructor, because the parent class basically indicates "I cannot be instantiated implicitly", so to provide the necessary information, the child classes must also declare at least one explicit constructor. 如果您的父类声明了一个构造函数,则子类不能使用隐式构造函数,因为父类基本上指示“我不能隐式实例化”,因此,为了提供必要的信息,子类还必须声明至少一个显式构造函数。

Also, automatic copying of constructors would violate abstractions and information hiding, as your child class might unwillingly expose constructors to callers which it does not want to expose. 同样,自动复制构造函数会违反抽象和信息隐藏,因为您的子类可能不愿意将构造函数公开给不想公开的调用者。

Of course you could argue that this should be the default and if I do not want a constructor to be exposed I could deliberately indicate this. 当然,您可能会争辩说这应该是默认值,如果我不希望公开构造函数,则可以故意指出这一点。 However, Java decided to go the other way. 但是,Java决定采用其他方式。 A similar discussion is that on method overriding: do I have to allow a method to be overridden in my parent class (eg, C#'s virtual modifier) or do I simply forbid it for certain methods (Java's final modifier). 类似的讨论是关于方法重写的:我是否必须允许在父类中重写方法(例如C#的virtual修饰符),或者我只是禁止某些方法使用它(Java的final修饰符)。

Does every concrete sub-class have to have that same 1-param constructor 是否每个具体的子类都必须具有相同的1-param构造函数

Well, strictly speaking they don't have to have that constructor - but they'll need to have a constructor, in order to pass a value to the AbstractClass constructor. 好吧,严格来说,他们不必具有该构造函数-但他们需要一个构造函数,才能将值传递给AbstractClass构造函数。

and if so, why? 如果是这样,为什么?

Because constructors aren't inherited. 因为构造函数不是继承的。 The information required for a subclass often isn't the same as the information required for a superclass. 子类所需的信息通常与父类所需的信息不同。

Imagine if constructors were inherited - then everything would have a parameterless constructor, because Object does. 试想一下,如果构造函数继承-那么一切都将有一个参数的构造函数,因为Object一样。 So you'd be able to write: 这样您就可以编写:

FileInputStream stream = new FileInputStream();

... what would that mean? ...那是什么意思?

Bottom line: add the constructors to your subclasses. 底线:将构造函数添加到您的子类中。 It's only three lines per subclass... that shouldn't be an issue. 每个子类只有三行……这不成问题。

You can use a nullary constructor if you instantiate the expected parameter within your subcontroller: 如果在子控制器中实例化期望的参数,则可以使用null构造函数:

public class ConcreteClass extends AbstractClass {
    public ConcreteClass() {
        super("concrete");
    }
}

You cannot provide no constructor though. 但是,您不能提供任何构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM