简体   繁体   English

从抽象类的静态方法返回类引用

[英]Returning a class reference from the static method of an abstract class

I understand that an abstract class can't be instantiated. 我了解无法实例化抽象类。 Also, we can not use new keyword inside of a static method. 同样,我们不能在静态方法中使用new关键字。

I'm wondering that how does the static Create method of XMLReader returns the reference of its class in the following statement: 我想知道XMLReader的静态Create方法如何在以下语句中返回其类的引用:

XmlReader xmlReader=XmlReader.Create() XmlReader xmlReader = XmlReader.Create()

Thanks 谢谢

It returns an instance of a derived class. 它返回派生类的实例。 It does not construct an XmlReader directly. 它不会直接构造XmlReader

abstract class A {
    public static A Create() { return new B(); }
}
class B : A {
}

This is the same basic idea, and you can tell that that's what it does by calling GetType() on XmlReader.Create 's result. 这是相同的基本思想,您可以通过在XmlReader.Create的结果上调用GetType()来说明其作用。 It won't return typeof(XmlReader) . 它不会返回typeof(XmlReader)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM