Array 声明如下:
public abstract class Array
: ICloneable, IList, ICollection, IEnumerable {
Run Code Online (Sandbox Code Playgroud)
我想知道为什么不是它:
public partial class Array<T>
: ICloneable, IList<T>, ICollection<T>, IEnumerable<T> {
Run Code Online (Sandbox Code Playgroud)
如果它被声明为泛型类型会有什么问题?
如果它是泛型类型,我们还需要非泛型吗?如果我们愿意的话,它是否可以衍生出来Array<T>(如上所述)?
public partial class Array: Array<object> {
Run Code Online (Sandbox Code Playgroud)更新:
第二点对于不再使非泛型Array成为基本类型的假设也很重要.
假设我们有两个方法M1(),并M2()在接口.抽象类也有两个相同的抽象方法.如果任何类实现了此接口或从抽象类继承,则必须同时实现其中的方法.
所以对我来说,似乎我的场景中的接口或抽象类的行为相同.那么,任何人都可以在这个特定的情况下突出显示这两者之间的区别,并建议是否在这里使用抽象类或接口?
摘要
我试图在javascript中正确地实现继承和封装,就像在基于类的语言(如c#)中一样.
丑陋的部分是受保护的成员在私有实例中有多个副本,只能通过闭包访问,除了将这些成员刷新到私有实例之外,我没有任何想法.
如果有可能,我想摆脱我transmit和transfer我的代码Function.extend.
更新 对于对引用或研究感兴趣的人,这里是源代码存储库:
故事
由于程序集可能是一个超出javascript范围的概念,我不考虑internal修改器,但是public,protected和private.
public和private修饰语并不难实现; 但是继承,protected非常棘手.然而,使用javascript并不是一个推荐的事情,我读过的大多数文章都说带有特殊字符的前缀并记录它.
但似乎我坚持使用javascript来模拟基于类的语言..我偷了这个想法并以我的方式实现,代码在这篇文章的后面.
场景背后的想法是使用更高的原型提高可访问性,并使用闭包访问最高的原型.
假设我们有三个原型A,D并且G,它看起来像

因为对象不可能是另一种不属于原型链的类型的实例; 我选择的方式是protected水平链接水平并从声明类型的原型复制成员.这使得嵌套类成为可能,因为在较少派生类型上声明的成员可以传播到更多派生类型; transmit我的代码中的方法是这样做的.如果A,D并G拥有自己的受保护成员,它看起来像:

用于访问私有实例的闭包是this[''].它需要一个用于识别类的参数.修饰符支架正好类标识符,命名y中Function.extend和_测试代码,它不应该被类声明暴露在外.它也被用作快捷方式this[''].
_['base']事实上,不仅是基础构造函数调用者,还有私有实例创建者.它this['']使用继承为每个构造函数创建私有实例和更新,因此应始终在构造函数中调用它.
虽然私有实例可以访问公共成员,但不应该使用它来更改它们,因为this['']在访问公共成员时不能保证调用它们.但访问私有实例是; recent记住最近访问的私有实例,如果有更改,则更新受保护的成员.
我的问题是,我怎样才能摆脱受保护成员的这种清新?是否有更好的想法来实现更真实的封装?
ps:我实际上不想要一个使用非标准方法/属性的解决方案..如果使用的方法/属性对旧浏览器来说过于时尚,那么填充更好.
Function.extend …
请考虑以下代码:
public class MyClass
{
public delegate string PrintHelloType(string greeting);
public void Execute()
{
Type[] types = new Type[] { typeof(string), typeof(float), typeof(int)};
List<PrintHelloType> helloMethods = new List<PrintHelloType>();
foreach (var type in types)
{
var sayHello =
new PrintHelloType(greeting => SayGreetingToType(type, greeting));
helloMethods.Add(sayHello);
}
foreach (var helloMethod in helloMethods)
{
Console.WriteLine(helloMethod("Hi"));
}
}
public string SayGreetingToType(Type type, string greetingText)
{
return greetingText + " " + type.Name;
}
...
}
Run Code Online (Sandbox Code Playgroud)
调用后myClass.Execute(),代码打印以下意外响应:
Hi Int32 Hi Int32 Hi Int32
很显然,我希望 …
我花了很多时间试图让DTVViewer的DirectShow样本工作不幸,但没有成功.DVBT网络的视频格式是H264,我发现IntelliConnect行为IFilterGraph更喜欢使用Mpeg2视频格式.
对于那些想要查看代码的人来说,就是这样.如果你对DirectShow一无所知我分享了我对这段代码的经验.最可能的问题在本教程的第5步和第6步中描述.
连接过滤器的辅助函数代码:
public static void UnsafeConnectFilters(IFilterGraph2 graph, IBaseFilter source, IBaseFilter dest, Func<AMMediaType, bool> sourceMediaPredicate=null, Func<AMMediaType, bool> destMediaPredicate=null) {
foreach(IPin spin in IteratePinsByDirection(source, PinDirection.Output)) {
if(IsConnected(spin))
continue;
int fetched;
AMMediaType[] sourceTypes=GetMajorType(spin, out fetched);
if(fetched>0) {
Guid sourceType=sourceTypes[0].majorType;
try {
if(sourceMediaPredicate!=null&&!sourceMediaPredicate(sourceTypes[0]))
continue;
foreach(IPin pin in IteratePinsByDirection(dest, PinDirection.Input)) {
if(IsConnected(pin))
continue;
var types=GetMajorType(pin, out fetched);
try {
if(fetched>0) {
Guid destType=types[0].majorType;
if(destMediaPredicate!=null&&!destMediaPredicate(types[0]))
continue;
if(sourceType==destType) {
spin.Connect(pin, types[0]);
return;
}
}
else {
spin.Connect(pin, sourceTypes[0]);
return;
}
}
finally …Run Code Online (Sandbox Code Playgroud)要回答的事情:
不要担心差异,而有问题的项目则Array不是T[].
多维数组的类似情况是[ 这里 ]
也就是说,N-dims到线性变换总是可行的.所以这个问题引起了我的注意,因为它已经实现IList了线性索引器.
题:
在我的代码中,我有以下声明:
public static Array ToArray<T>(this T source);
Run Code Online (Sandbox Code Playgroud)
我的代码知道如何使souce礼物成为一个数组(在运行时).我正试图让消费代码直接访问其索引器.但如果没有"作为IList",它就不可能完成.
要返回object[]可能需要额外的转换/转换,这就是我要阻止做的事情.
我能做的是:
public static IList ToArray<T>(this T source);
Run Code Online (Sandbox Code Playgroud)
但我认为一个名为ToArrayreturn的方法IList看起来很奇怪.
因此,我对此感到困惑:
在声明中Array,有
object IList.this[int index];
Run Code Online (Sandbox Code Playgroud)
这样我们就可以
Array a;
a=Array.CreateInstance(typeof(char), 1);
(a as IList)[0]='a';
Run Code Online (Sandbox Code Playgroud)
但我们做不到
a[0]='a';
Run Code Online (Sandbox Code Playgroud)
除非它被宣布为
public object this[int index];
Run Code Online (Sandbox Code Playgroud)
我能看到的唯一区别是它需要我们通过IList实现它的接口显式地使用它的索引器,但为什么呢?有好处吗?或者是否存在暴露问题?
在C#中,结构是值类型,但我能够将new它们视为引用类型.为什么是这样?
有IsAssignableFrom方法返回一个布尔值,表示一种类型是否可以从另一种类型分配.
怎能不只有他们分配的测试从或到对方,但也知道了最低协变,以获得最佳类型?
考虑以下示例(C#4.0)
码
// method body of Func is irrelevant, use default() instead
Func<char[]> x = default(Func<char[]>);
Func<int[]> y = default(Func<int[]>);
Func<Array> f = default(Func<Array>);
Func<IList> g = default(Func<IList>);
g=x;
g=y;
y=x; // won't compile
x=y; // won't compile
// following two are okay; Array is the type for the covariance
f=x; // Array > char[] -> Func<Array> > Func<char[]>
f=y; // Array > int[] -> Func<Array> > Func<int[]>
// following …Run Code Online (Sandbox Code Playgroud)从一个-5问题再次启发!
我读了@Quartermeister的 [ 评论 ] 并感到惊讶!
那么为什么这会编译
switch(1) {
case 2:
}
Run Code Online (Sandbox Code Playgroud)
但事实并非如此.
int i;
switch(i=1) {
case 2: // Control cannot fall through from one case label ('case 2:') to another
}
Run Code Online (Sandbox Code Playgroud)
这不是
switch(2) {
case 2: // Control cannot fall through from one case label ('case 2:') to another
}
Run Code Online (Sandbox Code Playgroud)
更新:
该-5问题变得-3.
我正在编写以下代码:
#include<iostream>
#include<stdio.h>
using namespace std;
main() {
unsigned char a;
a=1;
printf("%d", a);
cout<<a;
}
Run Code Online (Sandbox Code Playgroud)
它打印1和一些垃圾.
为什么cout表现如此?
c# ×9
types ×2
.net ×1
arrays ×1
c++ ×1
covariance ×1
directshow ×1
dvb ×1
generics ×1
h.264 ×1
ilist ×1
indexer ×1
inheritance ×1
interface ×1
iterator ×1
javascript ×1
lambda ×1
new-operator ×1
oop ×1
printf ×1
struct ×1
value-type ×1