为什么抽象类无法实例化,无法实例化的类的用途是什么 [英] why abstract class cannot be instantiated ,what is the use of a class which cannot be instantiated

查看:124
本文介绍了为什么抽象类无法实例化,无法实例化的类的用途是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解并了解了抽象类和接口,但我从未理解的一点是,无法实例化的类有什么用. 我可以使用普通类和虚拟方法代替抽象类吗? 当我实例化基类时会发生什么?

解决方案

在派生类之间共享某些通用功能时,通常使用抽象类.也就是说,您不能使用界面,因为您要提供一些默认功能.

看看 System.IO.Stream 类.此类提供了一些通用的基本功能,但要求特定类型的流实现某些成员才能起作用.这些成员也被标记为abstract,这向编译器和运行时指示没有合适的基类实现.派生抽象类必须的非抽象类将覆盖所有继承的抽象成员,就像实现接口的类必须实现接口上定义的所有成员一样.

在流示例中, Read() 方法是抽象,但是 ReadByte() 方法不是-因为<可以根据对Read()的调用来实现c3>. (尽管不是最优的,这就是为什么ReadByte()是虚拟的,以便可以有选择地提供更有效的实现.)虚拟成员是不同的,因为它们 do 具有实现,但是可以有选择地被覆盖.抽象成员默认情况下没有实现,并且必须被覆盖.

换句话说,抽象类上的方法可以使用该类上的其他抽象成员,即使它们没有实现!这是因为需要派生的非抽象类来提供实现-可以保证在调用该方法时就存在一个实现.这类似于即使接口上没有成员,也可以使用接口的成员,因为可以确保实现接口的对象必须实现其所有成员.

诸如 MemoryStream Stream对象的方法,而不必关心它实际上是哪种类型的流.

Stream foo = new Stream();       // Invalid, Stream is abstract.
Stream foo = new MemoryStream(); // Valid.

因此,现在总结一下您在标题中提出的问题的答案.无法实例化抽象类,因为它可能包含抽象的成员且没有实现.抽象类的使用是双重的:首先,将其抽象化,并允许子类共享某些成员的通用实现;其次,允许通过引用抽象类来使用子类的任何对象的实例.

I know and read about abstract class and interface but one point I never understood is that, what is the use of class which cannot be instantiated. I can use normal class and virtual method instead of abstract class? what will happen when I instantiate base class?

You typically use an abstract class when you have some set of common functionality to be shared between derived classes. That is, you cannot use an interface because you want to provide some default functionality.

Take a look at the System.IO.Stream class. This class provides some common base functionality, but requires that specific types of streams implement some members in order for it to function. These members are also tagged abstract, which indicates to the compiler and runtime that there is no suitable base-class implementation. A non-abstract class that derives an abstract class must override all inherited abstract members, just like a class that implements an interface must implement all members defined on the interface.

In the stream example, the Read() method is abstract, but the ReadByte() method is not -- because ReadByte() can be implemented in terms of a call to Read(). (Although not optimally, which is why ReadByte() is virtual, so that a more efficient implementation can optionally be provided.) Virtual members are different, because they do have an implementation, but can optionally be overridden. Abstract members have no implementation by default, and must be overridden.

In other words, methods on an abstract class can use other abstract members on the class, even though they have no implementation! This is because a derived non-abstract class is required to provide an implementation -- an implementation is guaranteed to exist at the point that the method is invoked. This is analogous to how you can use members of an interface even though the members have no implementation on the interface, because it's guaranteed that an object implementing the interface must implement all of its members.

Subclasses like MemoryStream and FileStream override all of the abstract methods to form a concrete class, and they can be instantiated. However, you are able to store them in a Stream reference variable and treat them like a generic "black box" stream. This allows you to declare a method that accepts a Stream object, and you don't have to care what kind of stream it actually is.

Stream foo = new Stream();       // Invalid, Stream is abstract.
Stream foo = new MemoryStream(); // Valid.

So, now to summarize the answers to the questions you posed in your title. An abstract class cannot be instantiated because it may contain members that are abstract and have no implementation. The use of an abstract class is twofold: first, to be subclassed and allow the subclasses to share a common implementation of some members, and second, to allow instances of any objects of subclasses to be used through references to the abstract class.

这篇关于为什么抽象类无法实例化,无法实例化的类的用途是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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