简体   繁体   English

为什么不能实例化抽象类和接口?

[英]Why abstract class and interface cannot be instantiated?

Why are we not able to create the instance of the abstract class and interface? 为什么我们不能创建抽象类和接口的实例? Means, abstract class and interface cannot be instaniated and only can be implemented by the Class. 手段,抽象类和接口不能被实例化,只能由该类实现。

I like this answer : 我喜欢这个答案

An abstract class is a special kind of class that cannot be instantiated. 抽象类是无法实例化的一种特殊类。 So the question is why we need a class that cannot be instantiated? 所以问题是为什么我们需要一个无法实例化的类? An abstract class is only to be sub-classed (inherited from). 抽象类仅是子类(从继承)。 In other words, it only allows other classes to inherit from it but cannot be instantiated. 换句话说,它仅允许其他类从中继承而不能实例化。 The advantage is that it enforces certain hierarchies for all the subclasses. 优点是它为所有子类强制执行某些层次结构。 In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards. 简而言之,它是一种契约,它迫使所有子类进行相同的层次结构或标准。

An interface is not a class. 接口不是类。 It is an entity that is defined by the word Interface. 它是由接口一词定义的实体。 An interface has no implementation; 接口没有实现; it only has the signature or in other words, just the definition of the methods without the body. 它仅具有签名,换句话说,只有方法的定义而没有主体。 As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. 作为与Abstract类的相似之处之一,它是用于定义所有子类的层次结构或定义特定的方法集及其参数的协定。

In a normal situation, this is because to create an instance of a class requires that a block of memory is set aside for that instance to reside in. An abstract class or an interface does not contain all of the information required to create an instance of it. 在正常情况下,这是因为要创建一个类的实例,需要为该实例留出一块内存以供驻留。抽象类或接口不包含创建该类的实例所需的所有信息。它。 It only contains the information needed to describe the class. 它仅包含描述类所需的信息。

Trying to create an instance of an abstract class or an interface is like spending money on a box of bricks, but with nothing inside of it. 尝试创建一个抽象类或接口的实例就像在一个盒子上花钱,但里面什么也没有。 You only have the outside, nothing inside. 你只有外面,里面没有。

暂无
暂无

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

相关问题 类型是接口或抽象类,不能被实例化 - Type is an interface or abstract class and cannot be instantiated 无法创建类型的实例。 类型是接口或抽象类,不能被实例化 - Could not create an instance of type . Type is an interface or abstract class and cannot be instantiated ReadAsAsync - 错误:“Type是一个接口或抽象类,无法实例化。” - ReadAsAsync - Error : “Type is an interface or abstract class and cannot be instantiated.” C#JSON反序列化:Type是接口或抽象类,无法实例化 - C# JSON Deserialization : Type is an interface or abstract class and cannot be instantiated 为什么抽象类不能被实例化,什么是无法实例化的类的使用 - why abstract class cannot be instantiated ,what is the use of a class which cannot be instantiated 类型是接口或抽象类,无法实例化。 c#发送一个包含抽象对象列表的对象 - Type is an interface or abstract class and cannot be instantiated. c# send a object with contain a list of abstract object 为什么接口不能继承纯抽象类 - Why interface cannot inherit pure abstract class 无法创建类型 X 的实例。类型是接口或抽象类,无法实例化 - Could not create an instance of type X. Type is an interface or abstract class and cannot be instantiated 无法创建抽象类或接口的实例,但它不是抽象类,也不是接口? - Cannot create an instance of the abstract class or interface, but it is not an abstract class and it's not an interface? 为什么抽象类实现接口? - Why Abstract Class Implementing Interface?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM