在Java中为抽象类创建构造函数的用法是什么?
问题描述:
我想知道抽象类的构造函数的用途;因为我们不实例化抽象类,为什么我们需要这样的构造函数?
I would like to know what purpose a constructor for an abstract class serves; as we do not instantiate abstract classes, why would we ever need such a constructor?
答
所有继承类都需要设置的实例变量的公共初始化。当你扩展一个抽象类时,你需要实例化一个抽象类,具体的类有一个构造函数,它将为抽象类的构造函数提供参数。
there will be times when you have some common initialization of instance variables that all the inheriting classes need to set up. You do instantiate an abstract class when you extend it and that concrete class has a constructor that will either supply the parameters to the constructor of the abstract class.