如何从抽象类创建对象?

发布于 2024-12-26 13:46:12 字数 84 浏览 3 评论 0原文

我们知道c#中的Array类是抽象的。

但该类的静态CreateInstance方法返回Array类的对象。

怎么可能呢?

We know that the Array class in c# is abstract.

But the static CreateInstance method of this class returns an object of the Array class.

How is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

为人所爱 2025-01-02 13:46:12

描述

不,您不能创建抽象类的实例。

MSDN:抽象类与接口密切相关。 它们是无法实例化的类,并且通常要么部分实现,要么根本不实现。抽象类和接口之间的一个关键区别是,一个类可以实现无限数量的接口,但只能从一个抽象(或任何其他类型)类继承。从抽象类派生的类仍然可以实现接口。抽象类在创建组件时很有用,因为它们允许您在某些方法中指定不变的功能级别,但保留其他方法的实现,直到需要该类的特定实现为止。它们的版本也很好,因为如果派生类中需要附加功能,可以将其添加到基类中而不会破坏代码。

更多信息

Description

No, you cant create an instance of an abstract class.

MSDN: Abstract classes are closely related to interfaces. They are classes that cannot be instantiated, and are frequently either partially implemented, or not at all implemented. One key difference between abstract classes and interfaces is that a class may implement an unlimited number of interfaces, but may inherit from only one abstract (or any other kind of) class. A class that is derived from an abstract class may still implement interfaces. Abstract classes are useful when creating components because they allow you specify an invariant level of functionality in some methods, but leave the implementation of other methods until a specific implementation of that class is needed. They also version well, because if additional functionality is needed in derived classes, it can be added to the base class without breaking code.

More Information

魂归处 2025-01-02 13:46:12

它是一个返回数组实例的静态工厂方法。此示例为整数值类型创建一个长度为 10 的数组。

System.Array myIntArray = Array.CreateInstance(typeof(int),10);

这应该为您提供更多详细信息 http://msdn.microsoft.com/en-我们/library/zb3cfh7k.aspx

It's a static factory method that returns an instance of array. this example creates an array of length 10 for integer value types.

System.Array myIntArray = Array.CreateInstance(typeof(int),10);

This should give you some more detail http://msdn.microsoft.com/en-us/library/zb3cfh7k.aspx

围归者 2025-01-02 13:46:12

方法 Array.CreateInstance() 的各种重载被类型化为返回 Array,这确实是一个抽象类。但它们返回的对象类型并不是直接Array,而是Array继承的某种类型(具体类型取决于所使用的重载)以及您传入的参数)。

例如:

Array a = Array.CreateInstance(typeof(int), 10); //create some array
Type type = a.GetType(); // type is int[], which is not abstract
Type baseType = type.BaseType; // baseType is Array

基本上和下面的工厂方法原理是一样的:

abstract class Animal
{
    public static Animal CreateInstance(AnimalType animalType)
    {
        if (animalType == AnimalType.Cat)
            return new Cat();
        if (animalType == AnimalType.Dog)
            return new Dog();
        // etc.
    }
}

这里,Animal是一个抽象基类型,CatDog是具体的从 AnimalanimalType 继承的类型告诉我们该方法应返回哪种类型。

现在,.Net 中对数组进行了多种特殊处理(例如,有专门处理数组的 IL 指令)。但它们在类型系统中并不是例外(也许数组协变除外)。

The various overloads of the method Array.CreateInstance() are typed as returning Array, which indeed is an abstract class. But the type of the object they return is not directly Array, it's some type that inherits from Array (what type exactly depends on the overload used and the parameters you pass in).

For example:

Array a = Array.CreateInstance(typeof(int), 10); //create some array
Type type = a.GetType(); // type is int[], which is not abstract
Type baseType = type.BaseType; // baseType is Array

Basically, it's the same principle like the following factory method:

abstract class Animal
{
    public static Animal CreateInstance(AnimalType animalType)
    {
        if (animalType == AnimalType.Cat)
            return new Cat();
        if (animalType == AnimalType.Dog)
            return new Dog();
        // etc.
    }
}

Here, Animal is an abstract base type, Cat and Dog are concrete types that inherit from Animal and animalType tells us which type should the method return.

Now, arrays are treated specially in several ways in .Net (for example, there are IL instructions specifically for dealing with arrays). But they are not an exception in the type system (except, maybe for array covariance).

甜尕妞 2025-01-02 13:46:12

Array 似乎是抽象类的一个特例。根据我在 文档 中读到的内容,我建议数组的创建和函数以某种方式在 .NET Framework 代码中进行内部处理 - 最有可能广泛使用本机代码以获得更好的性能结果。我认为这就是为什么这个类被抽象的原因。

如果更了解 .NET Framework 内部结构的人可以改进我的答案,我会很高兴。

Array seems to be a special case of abstract class. From what I've read in Documentation I'd suggest that Array creation and functions are somehow handled internally in .NET Framework code - most probably extensively using native code for better performance results. I think that's why this class have been made abstract.

I'd be glad if someone more knowledgable of .NET Framework internals can improve my answer.

甜中书 2025-01-02 13:46:12

我认为解决这种情况的最佳方法是考虑一种返回接口实例的方法!

好吧,您知道我们不能创建接口的实例,但在内部,方法可能知道实现该接口的类并返回该类的实例。

I think the best way to relate to this situation is by considering a method that returns you instance of an interface!!!

Well, you know that we can not create instance of an interface, but internally a method may know about a class implementing that interface and return instance of that class.

人疚 2025-01-02 13:46:12
namespace ConsoleApplication1
{
    public class Circle : Shape
    {
        public override void Draw()
        {
            Console.WriteLine("Draw a Circle");
        }
    }

    public abstract class Shape
    {    
        public abstract void Draw();
    }
}

你可以这样做

class Program
{
    static void Main(string[] args)
    {       
        Shape v;
        v = new Circle();
        v.Draw();
    }
}
namespace ConsoleApplication1
{
    public class Circle : Shape
    {
        public override void Draw()
        {
            Console.WriteLine("Draw a Circle");
        }
    }

    public abstract class Shape
    {    
        public abstract void Draw();
    }
}

You can do like this

class Program
{
    static void Main(string[] args)
    {       
        Shape v;
        v = new Circle();
        v.Draw();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文