无法创建抽象类或接口的实例
我不熟悉使用抽象类
。
我正在尝试调用抽象类
并收到此错误无法创建抽象类或接口的实例
并且我已经研究了此错误,但我真的很困惑这。
这是我的代码:
string B1String;
while ((B1String = OasisFile.ReadLine()) != null)
{
Questions_Base oQuestions_Base = new Questions_Base(); // error here
oQuestions_Base.Import(B1String);
}
请给我建议..谢谢!
I'm not familiar on using abstract class
.
I'm trying to call a abstract class
and get this error Cannot create an instance of the abstract class or interface
and I already research this error but I'm really confused on this.
Here's my code:
string B1String;
while ((B1String = OasisFile.ReadLine()) != null)
{
Questions_Base oQuestions_Base = new Questions_Base(); // error here
oQuestions_Base.Import(B1String);
}
Please advice me.. thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
抽象类的目的是作为类层次结构的一部分,其中更多派生类共享一些公共实现。
如果您有一个飞行模拟器,您可以定义一个抽象类
ThingsThatFly
来实现所有飞行物体都具有的一些属性(空速、高度、航向)和方法(TakeOff()、Land())常见,但会被声明为抽象,因为 ThingsThatFly 是所有会飞的具体事物的抽象。当然,您也可以在两者之间添加类,例如Cessna172
可以继承自Airplane
,而Airplane
又继承自ThingsThatFly
。如果所有飞机都有一些共同的实现,例如鸟类没有的(例如,剩余燃料属性),您就可以这样做。然后,您将拥有许多像塞斯纳 172、航天飞机和鸭子一样飞行的具体(=现实生活)物体。其中每一个都是从
ThingsThatFly
派生的具体类,这与让具体类实现诸如
IThingsThatFly
之类的接口不同,因为抽象类不仅提供定义预期的属性和方法,而且还提供了这些属性和方法的(希望有用的)实现。The purpose of an abstract class it to serve as part of a class hierarchy where more-derived classes share some common implementation.
If you have a flight simulator, you might define an abstract class
ThingsThatFly
that implements some properties (air speed, altitude, heading) and methods (TakeOff(), Land()) that all flying things have in common, but would be declared abstract because ThingsThatFly is an abstraction of all concrete things that fly. You could certainly have classes inbetween as well, for exampleCessna172
could inherit fromAirplane
that inherits fromThingsThatFly
. You would do that if all airplanes have some common implementation that e.g. birds don't have (for example, a Fuel Remaining property).You would then have a number of concrete (= real life) things that fly like a Cessna 172, a Space Shuttle and a Duck. Each of those would be a concrete class that derives from
ThingsThatFly
This is different than having the concrete classes implement an interface such as
IThingsThatFly
in that the abstract class provides not only a definition of the properties and methods that are expected, but also provides a (hopefully useful) implementation of those properties and methods.抽象类只能被继承。
这是一个关于抽象类及其使用方法的链接。
An Abstract class can only be inherited.
Here's a link all about abstract classes and how to use them.
您无法创建抽象类的实例。
您需要定义一个继承抽象类的具体类,并创建它的实例。
You cant create an instance of an abstract class.
You need to define a concrete class that inherits the abstract class, and create an instance of that.
Abstract
类被派生类覆盖。如果您必须拥有Abstract
类,请首先从中创建Derived
类并使用Derived
类构造函数。Questions_Base
类声明中删除abstract
一词,从而使其成为非抽象的。另外,因为在提供的代码中我没有看到任何abstract
成员,所以这可能是正确的选择。问候。
Abstract
class is made to be overriden by Derived class. If you have to haveAbstract
class, first create sDerived
class from it and useDerived
class contructor.abstract
word fromQuestions_Base
class declaration, so making that non abstract one. Also because in code provided I don't see anyabstract
member, so may this one is correct choice.Regards.
抽象类不能被实例化。您必须为该类提供一个实现。
有关详细信息,请参阅摘要上的 MSDN
An abstract class cannot be instantiated. You must provide an implementation for the class.
See MSDN on abstract for more information
文档中:“abstract 关键字使您能够创建不完整且必须在派生类中实现的类和类成员。”
使用抽象的目的正是为了防止实例化,因为您创建的类只是用作基类,而不想创建实例。
更多信息请参见此处。
From the documentation: "The abstract keyword enables you to create classes and class members that are incomplete and must be implemented in a derived class."
The purpose of using abstract is exactly to prevent instantiation, because you only created the class to use as a base class and never want an instance created.
More here.
抽象类是必须被继承的类。
它介于接口(仅定义类必须实现的接口而没有实现代码)和可以创建其实例的类(定义接口和实现代码)之间。 .NET框架中有几个抽象类,例如CollectionBase。您无法创建 CollectionBase 的实例,它旨在让您创建一个继承它并扩展其功能的类。
您应该能够简单地从 Questions_Base 的类定义中删除 kwy 工作“抽象”,或者创建一个继承自它的新类定义。
An abstract class is one which MUST be inherited.
It falls somewhere between an Interface, which defines only the interface that a class must implement and no implementation code and a class that you can create an instance of which defines both the interface and the implementation code. There are several abstract classes in the .NET framework such as CollectionBase. You cannot create an instance of CollectionBase, it is intended for you to create a class that inherits from it and extends it's capabilities.
You should simpley be able to remove the kwy work "abstract" from your class definition of Questions_Base or create a new class definition that inherits from it.
抽象类在类定义中由关键字abstract 标记,通常用于定义层次结构中的基类。它们的特别之处在于您无法创建它们的实例 - 如果您尝试,您将收到编译错误。相反,您必须按照继承一章中的介绍对它们进行子类化,并创建子类的实例。那么什么时候需要抽象类呢?这实际上取决于你做什么。老实说,您可以在不需要抽象类的情况下走很长的路,但它们对于特定的事物(例如框架)非常有用,这就是为什么您会在 .NET 框架本身中找到相当多的抽象类。一个好的经验法则是,这个名字实际上很有意义——抽象类经常(如果不是总是)用来描述抽象的东西,更多的是概念而不是真实的东西。
Abstract classes, marked by the keyword abstract in the class definition, are typically used to define a base class in the hierarchy. What's special about them, is that you can't create an instance of them - if you try, you will get a compile error. Instead, you have to subclass them, as taught in the chapter on inheritance, and create an instance of your subclass. So when do you need an abstract class? It really depends on what you do. To be honest, you can go a long way without needing an abstract class, but they are great for specific things, like frameworks, which is why you will find quite a bit of abstract classes within the .NET framework it self. A good rule of thumb is that the name actually makes really good sense - abstract classes are very often, if not always, used to describe something abstract, something that is more of a concept than a real thing.