在具有构造函数的抽象类中?如果是,则表示抽象构造函数的用途是什么? [英] In abstract class having the constructor? If yes means what is the use of abstract constructor?

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

问题描述

在具有构造函数的抽象类中?如果是,则表示抽象构造函数的用途是什么?



我尝试过:



In abstract class having the constructor? If Yes means what is the use of abstract constructor?

What I have tried:

In my knowledge constructor usage is initialize the data into data field.But in abstract class having the abstract methods then what is the use of constructor please any one help me 

推荐答案

虽然你无法实例化和抽象类 - 这使得构造函数无用 - 从抽象类继承的类可以调用基础构造函数并为此创建所有派生类的一些常见行为...
While it is true that you can not instantiate and abstract class - and that makes constructors useless - the class that inherits from the abstract class can call the base constructor and with that create some common behaviors for all the derived classes...


您可以在 abstract 中定义非抽象构造函数class来执行常见组件的初始化 - 那些在 abstract 类本身中定义的组件,因此可用于所有具体类。



但是......你不能定义一个抽象构造函数哪个会强迫co ncrete类来实现具有特定签名的构造函数,因为永远不能以多态方式调用非默认构造函数。构造函数不允许虚拟 abstract
You can define a non-abstract constructor in an abstract class to do initialization of common components - those that are defined in the abstract class itself, and are thus available to all concrete classes.

But ... you can't define an abstract constructor which would force concrete classes to implement a constructor with a specific signature because a non-default constructor can never be called polymorphically. Neither virtual or abstract are allowed on constructors.


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

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