关于抽象类?
URL: 链接 (1)
根据此网站 .. 您不能实现抽象类,而是派生自他们。这是有道理的,我已经读过很多次了。
与接口一样,您无法实现抽象类的实例,但是您可以在抽象类中实现可由子类使用的方法、字段和属性。
但在 MSDN
URL 上: TextWriter MSDN 上的 CLass
TextWriter 是一个抽象类,但它定义了两个构造函数...并且根据 MS 70-536 书,以下声明是有效的:
TextWriter tw = new File.CreateText("myFile.Txt")
静态文件类及其 CreateText 方法对我来说很好,因为我已经研究过了 MSDN 但有人可以解释一下我发现的这个小矛盾吗?我肯定不是第一个吧?
为什么可以实例化基抽象类?
URL: Link (1)
According to this wesbite .. you cannot implement Abstract classes but derive from them. This makes sense and I have read this many times.
Like an interface, you cannot implement an instance of an abstract class, however you can implement methods, fields, and properties in the abstract class that can be used by the child class.
But on MSDN
TextWriter is an abstract class but it has two constructors defined ... and according to the MS 70-536 book, the following statement is valid:
TextWriter tw = new File.CreateText("myFile.Txt")
The static file class and it's CreateText method is fine by me as I have studied it on
MSDN but can somebody explain this little contradiction I have found? Surely I am not the first?
Why is instantaion of base abstract classes possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
File.CreateText 不返回 TextWriter,而是返回 StreamWriter,它 实现一个 TextWriter。
The File.CreateText doesn't return a TextWriter, but a StreamWriter, which implements a TextWriter.
所有抽象类都至少有一个构造函数 - 要么您实现一个构造函数,要么编译器生成一个无参数默认构造函数。构造函数在派生类实例化时执行 - 基类是否抽象并不重要。
但您无法实例化抽象类 -
File.CreateText()
是静态方法,并返回派生自TextWriter
的类的实例,而不是TextWriter
代码 > 实例。All abstract classes have at least one constructor - either you implement one or the compiler generates a parameterless default constructor. The constructor is executed when a derived class is instantiated - it does not matter that the base class is abstract.
But you cannot instantiate an abstract class -
File.CreateText()
is a static method and returns an instance of a class derived fromTextWriter
but not aTextWriter
instance.您可以在这里实例化似乎是的抽象类,其原因与您可以实例化似乎接口的原因相同:您实际上并没有创建该类型的类,但创建一个可以转换为该类型的类。
tw
可以定义为 TextWriter,尽管它实际上不能成为 TextWriter。但是,我对 new 关键字持怀疑态度。你确定这有效吗?不应该。
File.CreateText
创建并返回(继承自)一个 TextWriter,但new
指示构造函数,这不是正在发生的情况。You can instantiate what seems to be an abstract class here for the same reason you can instantiate what seems to be an interface: You're not actually creating a class of that type, but creating a class that can be cast to that type.
tw
can be defined as a TextWriter, though it could not actually be a TextWriter.However, I'm suspicious of the new keyword. Are you sure that works? It shouldn't.
File.CreateText
creates and returns (something that inherits from) a TextWriter, butnew
indicates a constructor, which is not what's happening.File.CreateText 的实现正在创建并返回一个类型,该类型是 TextWriter 的具体实现。也就是说,无论返回什么类型,都是 TextWriter —— 它不是抽象类的某种神奇实例。
尽管您无法创建抽象类,但您可以通过其基类型将其引用为派生类型的实例,而不会出现问题。如果你做不到这一点,那么多态性就毫无用处了!
The implementation of File.CreateText is creating and returning a type that is a concrete implementation of TextWriter. I.e. Whatever type is returned is-a TextWriter -- it's not some magical instantiation of an abstract class.
Although you cannot create an abstract class, you can refer to it an instance of a derived type by its base type without a problem. If you couldn't do this, then polymorphism would be pretty useless!
多态性/继承的魔力。
File.CreateText
返回一个StreamWriter
,它继承自TextWriter
。Magic of polymorphism/inheritance.
File.CreateText
returns aStreamWriter
, which inherits fromTextWriter
.这是不可能的。
发生的情况(一旦删除
new
关键字以使代码正常工作)是CreateText
方法正在创建StreamWriter
的实例,它继承了TextWriter
。StreamWriter
引用被分配给TextWriter
变量,该变量是有效的,因为StreamWriter
是TextWriter
。抽象类之所以有构造函数,是为了初始化抽象类中的数据。构造函数不能直接使用,而是会被继承抽象类的类中的构造函数调用。
It's not possible.
What's happening (once you remove the
new
keyword to make the code work at all) is that theCreateText
method is creating an instance ofStreamWriter
, which inheritsTextWriter
. TheStreamWriter
reference is assigned to aTextWriter
variable, which is valid because aStreamWriter
is aTextWriter
.The reason that an abstract class has constructors is to initialise the data in the abstract class. The constructor can't be used directly, but it will be called by the constructor in the class that inherits the abstract class.
CreateText 返回一个 StreamWriter
StreamWriter 是 TextWriter 的子类,因此可以存储在 TextWriter 变量中。该示例使用抽象类,因为它不关心将文本写入的内容的实际实现。
CreateText returns a StreamWriter
StreamWriter is a subclass of TextWriter and thus can be stored in the TextWriter variable. The example is using the abstract class because it does not care about the real implementation of what it is writing the text to.