我们知道,C#中的Array类是抽象的。
但是,这一类固定制造方法将阿雷拉类物品归为一种目标。
如何做到这一点?
我们知道,C#中的Array类是抽象的。
但是,这一类固定制造方法将阿雷拉类物品归为一种目标。
如何做到这一点?
无,你可以创立一个抽象的类别。
<<>MSDN>/strong>: 摘要班与接口密切相关。 这些班级无法即时使用<>/strong>,而且经常得到部分执行,或根本没有执行。 抽象的班级和接口之间的一个关键差别是,班级可实施数量有限的接口,但只能从一个抽象的(或任何其他种类)类别继承。 从抽象类别中得出的一个类别仍可采用接口。 摘要班在创建部件时是有用的,因为课程允许你在某种方法中确定一定程度的功能,但将其他方法的实施留待具体实施。 这些文件也版本良好,因为如果在衍生产品类别中需要额外的功能,就可以在基类中添加这种功能而不必打碎代码。
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);
http://msdn.microsoft.com/en-us/library/zb3cfh7k.aspx”rel=“nofollow”
方法<代码>Array.CreateInstance()的各种超载被归类为<代码>Array,这确实是一个抽象的类别。 但是,他们返回的物体类型不是直接的<代码>Array,而是从<代码>中选取的某种类型inherits。 (什么类型完全取决于所使用的超载和你所穿的参数)。
例如:
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
为抽象的基础类型,Cat
和Dog
为从Animal
和animalType
继承的具体类型。 告诉我们,这种方法应当返回哪一类。
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).
<代码>Array似乎是抽象类别的特殊案例。 http://msdn.microsoft.com/en-us/library/system.array.aspx” rel=“nofollow”Documentation。 我建议,<代码>Array的设定和职能在内部处理。 NET Framework Code——很可能广泛使用本地法典,以取得更好的业绩成果。 我认为,为什么这一类别是抽象的。
如果有人更了解,我很高兴。 该网络内部可以改进我的答案。
我认为,与这一局势有关的最佳途径是考虑一种方法,把你称之为一个接口!
你们知道,我们能够而不是<>创造接口的事例,但内部的方法可能知道一个类别实施该类交接和返回。
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();
}
}
What is the use of default keyword in C#? Is it introduced in C# 3.0 ?
I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...
I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber. I want to create a many to many association ...
How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...
Since I cannot order my dictionary, what is the best way of going about taking key value pairs and also maintaing an index?
Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...