[英]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.