简体   繁体   English

创建抽象类的对象。 怎么可能

[英]Create object of abstract class. How possible

Why it is possible? 为什么有可能? :

BitmapSource i = Imaging.CreateBitmapSourceFromHBitmap(...);

I'm writing some app ni find this line and im cunfused, because MSDN says that BitmapSource is abstract class. 我正在写一些应用程序ni找到这一行并且我是cunfused,因为MSDN说BitmapSource是抽象类。

BitmapSource is an abstract class and thus can't be created directly , but Imaging.CreateBitmapSourceFromHBitmap returns some concrete class that inherits from BitmapSource and thus can be "cast" to a BitmapSource . BitmapSource是一个抽象类,因此不能直接创建,但Imaging.CreateBitmapSourceFromHBitmap返回从继承一些具体类BitmapSource ,因此可以“投”到BitmapSource

It's analagous to having an abstract Animal class, but having a concrete Giraffe class that inherits from it: 它有一个抽象的Animal类,但有一个继承自它的具体Giraffe类:

Animal a = new Animal();  // illegal
Animal a = Zoo.CreateAnimalFromName("Giraffe"); // valid - returns a Giraffe instance

The call to Imaging.CreateBitmapSourceFromHBitmap returns some concrete class which inherits from BitmapSource . Imaging.CreateBitmapSourceFromHBitmap的调用返回一些继承自BitmapSource具体类。 So this call does not create an instance of the abstract class BitmapSource . 因此,此调用不会创建抽象类BitmapSource的实例。 You were just confused about this. 你只是对此感到困惑。

To simplify the situation, this is similar to 为了简化这种情况,这类似于
Animal an1 = DogFactory.createDog();
or to 或者
Animal an2 = CatFactory.createCat();
if we assume Animal is an abstract class, while Cat 如果我们假设Animal是一个抽象类,而Cat
and Dog are concrete classes which inherit from Animal . Dog是继承自Animal具体类。

BitmapSource is an abstract class but Imaging.CreateBitmapSourceFromHBitmap creates an object with type of concrete subclass InteropBitmap , you can see it in .NET reference source : BitmapSource是一个抽象类,但Imaging.CreateBitmapSourceFromHBitmap创建一个具有类型为具体子类InteropBitmap的对象,您可以在.NET参考源中看到它:

unsafe public static BitmapSource CreateBitmapSourceFromHBitmap(
    IntPtr bitmap,
    IntPtr palette,
    Int32Rect sourceRect,
    BitmapSizeOptions sizeOptions)
{
    SecurityHelper.DemandUnmanagedCode();

    // CR: [....] (1681459)
    return CriticalCreateBitmapSourceFromHBitmap(bitmap, palette, sourceRect, sizeOptions, WICBitmapAlphaChannelOption.WICBitmapUseAlpha);
}

unsafe internal static BitmapSource CriticalCreateBitmapSourceFromHBitmap(
    IntPtr bitmap,
    IntPtr palette,
    Int32Rect sourceRect,
    BitmapSizeOptions sizeOptions,
    WICBitmapAlphaChannelOption alphaOptions)
{
    if (bitmap == IntPtr.Zero)
    {
        throw new ArgumentNullException("bitmap");
    } 
    return new InteropBitmap(bitmap, palette, sourceRect, sizeOptions, alphaOptions); // use the critical version
}

And you can assign InteropBitmap to BitmapSource type variable because it is its base class (directly or not), exactly as in: 并且您可以将InteropBitmap分配给BitmapSource类型变量,因为它是它的基类(直接或不是),完全如下:

interface ISomeInterface { };
abstract class SomeBaseClass : ISomeInterfac { };
class SomeClass : SomeBaseClass { };

and then you can: 然后你可以:

ISomeInterface var1 = new SomeClass();

or: 要么:

SomeBaseClass var2 = new SomeClass();

and eventually you can create some factory method that hides creating an object: 最后你可以创建一些隐藏创建对象的工厂方法:

class SomeFactoryClass
{
   public SomeBaseClass CreateObject() { return new SomeClass(); }
}

SomeBaseClass var3 = SomeFactoryClass.CreateObject();

Exactly as in the above exept from the .NET reference source code. 正如上面的.NET参考源代码所示。

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

相关问题 如何从抽象类创建对象? - How is it possible to create an object from the abstract class? 如何创建抽象类的实例? - How is it possible to create an instance of an abstract class? 是否可以在抽象 class 中创建事件? - Is it possible to create an event in an abstract class? 内部静态类包含私有类。 如何使用Microsoft Fake创建私有类的对象以进行单元测试 - Internal static class contains private class. How to create object of private class for unit testing using microsoft fakes 如何将一个类中的对象添加到另一个类中的List。 - How to add object from a class to List in another class. 设计一个抽象基类。 使用什么类型,抽象或具体? - Designing an abstract base class. What types to use, abstract or concrete? 如何在抽象类的静态属性中使用HttpContext.Current.Session。 - How to use HttpContext.Current.Session in static property of a abstract class.? 在抽象基础 class 的帮助下避免 DRY 违规。 如何重构它? 寻找替代品 / arguments - Avoid DRY violation with help of abstract base class. How to refactor it? Looking for alternatives / arguments 如何创建一个抽象的类,但不是内部的类 - How create a class that is abstract, but not internally 如何动态地将多个对象实例传递给另一个类。 - How to dynamically pass multiple object instance to another class.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM