在Java中为抽象类创建构造函数有什么用? [英] What is the use of creating a constructor for an abstract class in Java?

查看:25
本文介绍了在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.

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

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