抽象类中的构造函数? [英] Constructors in abstract classes?

查看:138
本文介绍了抽象类中的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么抽象类有构造函数?
因为我们无法创建它的对象,为什么我们需要抽象类中的构造函数?

Why can an abstract class have a constructor? As we can't create an object of it why would we need a constructor in an abstract class?

推荐答案

在某些情况下,我们需要初始化抽象类中的字段。如果它是一个空构造函数,则由子类中的构造函数隐式完成,否则我们使用 super(参数)。带参数的构造函数强制子类指定一些参数(不一定是来自它自己的参数)。

In some cases we need to initialize the fields in the abstract class. If it is a empty constructor this is done implicit by the constructor in the child class, otherwise we use super(parameters). A constructor with parameters forces the child class to specify some parameters (not necessarily from its own parameters).

总而言之,这意味着构造函数由子进程使用类构造函数,而不是来自外部。

All in all, this means that the constructor is used by the child class constructor and not from the "outside".

这篇关于抽象类中的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆