using
System;
using System.Collections;
public class Tokens: IEnumerable
... {
private string[] elements;
Tokens(string source, char[] delimiters)
...{
elements = source.Split(delimiters);
}
// IEnumerable 接口实现:
public TokenEnumerator GetEnumerator() // 非 IEnumerable 版本
...{
return new TokenEnumerator(this);
}
IEnumerator IEnumerable.GetEnumerator() // IEnumerable 版本
...{
return (IEnumerator) new TokenEnumerator(this);
}
// 内部类实现 IEnumerator 接口:
public class TokenEnumerator: IEnumerator
...{
private int position = -1;
private Tokens t;
public TokenEnumerator(Tokens t)
...{
this.t = t;
}
public bool MoveNext()
...{
if (position < t.elements.Length - 1)
...{
position++;
return true;
}
else
...{
return false;
}
}
public void Reset()
...{
position = -1;
}
public string Current // 非 IEnumerator 版本:类型安全
...{
get
...{
return t.elements[position];
}
}
object IEnumerator.Current // IEnumerator 版本:返回对象
...{
get
...{
return t.elements[position];
}
}
}
// 测试标记,TokenEnumerator
static void Main()
...{
Tokens f = new Tokens("This is a well-done program.",
new char [] ...{' ','-'});
foreach (string item in f) // 试图将 string 更改为 int
...{
Console.WriteLine(item);
}
}
}
using System.Collections;
public class Tokens: IEnumerable
... {
private string[] elements;
Tokens(string source, char[] delimiters)
...{
elements = source.Split(delimiters);
}
// IEnumerable 接口实现:
public TokenEnumerator GetEnumerator() // 非 IEnumerable 版本
...{
return new TokenEnumerator(this);
}
IEnumerator IEnumerable.GetEnumerator() // IEnumerable 版本
...{
return (IEnumerator) new TokenEnumerator(this);
}
// 内部类实现 IEnumerator 接口:
public class TokenEnumerator: IEnumerator
...{
private int position = -1;
private Tokens t;
public TokenEnumerator(Tokens t)
...{
this.t = t;
}
public bool MoveNext()
...{
if (position < t.elements.Length - 1)
...{
position++;
return true;
}
else
...{
return false;
}
}
public void Reset()
...{
position = -1;
}
public string Current // 非 IEnumerator 版本:类型安全
...{
get
...{
return t.elements[position];
}
}
object IEnumerator.Current // IEnumerator 版本:返回对象
...{
get
...{
return t.elements[position];
}
}
}
// 测试标记,TokenEnumerator
static void Main()
...{
Tokens f = new Tokens("This is a well-done program.",
new char [] ...{' ','-'});
foreach (string item in f) // 试图将 string 更改为 int
...{
Console.WriteLine(item);
}
}
}
其中我最感兴趣的是这一小段。
public
TokenEnumerator GetEnumerator()
//
非 IEnumerable 版本
... {
return new TokenEnumerator(this);
}
IEnumerator IEnumerable.GetEnumerator() // IEnumerable 版本
... {
return (IEnumerator) new TokenEnumerator(this);
}
... {
return new TokenEnumerator(this);
}
IEnumerator IEnumerable.GetEnumerator() // IEnumerable 版本
... {
return (IEnumerator) new TokenEnumerator(this);
}
有点象可以根据函数返回值重载啊!但事实上它不是。IEnumerable.GetEnumerator()方法名(注:接口名.方法名()的写法,我没见过)和GetEnumerator()确实不一样。编译器如何来决定调用哪一个呢?我试了下,如果把public TokenEnumerator GetEnumerator()前的public去掉,则调用的是IEnumerator IEnumerable.GetEnumerator(),否则就调用TokenEnumerator GetEnumerator()方法。而IEnumerator IEnumerable.GetEnumerator()方法不能用public,编译器会报错。这样写法在java是编译都通不过的。这能不有解决接口方法名重的问题呢!我们来试下以下的代码。
using
System;
namespace PractiseC_
... {
public interface IInterface1
...{
void fun();
}
}
using System;
namespace PractiseC_
... {
public interface IInterface2
...{
void fun();
}
}
using System;
namespace PractiseC_
... {
public class TestInterface:IInterface1,IInterface2
...{
public TestInterface()
...{
}
void IInterface1.fun()...{
Console.WriteLine("Here implements IInterface1 fun()");
}
void IInterface2.fun()...{
Console.WriteLine("Here implements IInterface2 fun()");
}
public void fun()...{
Console.WriteLine("Here implements IInterfaces fun()");
}
public static void Main()...{
TestInterface ti = new TestInterface();
ti.IInterface1.fun();
ti.IInterface2.fun();
ti.fun();
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}
namespace PractiseC_
... {
public interface IInterface1
...{
void fun();
}
}
using System;
namespace PractiseC_
... {
public interface IInterface2
...{
void fun();
}
}
using System;
namespace PractiseC_
... {
public class TestInterface:IInterface1,IInterface2
...{
public TestInterface()
...{
}
void IInterface1.fun()...{
Console.WriteLine("Here implements IInterface1 fun()");
}
void IInterface2.fun()...{
Console.WriteLine("Here implements IInterface2 fun()");
}
public void fun()...{
Console.WriteLine("Here implements IInterfaces fun()");
}
public static void Main()...{
TestInterface ti = new TestInterface();
ti.IInterface1.fun();
ti.IInterface2.fun();
ti.fun();
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}
如果 ti.IInterface1.fun(); ti.IInterface2.fun();这两句能编译通过并运行,就能解决接口中函数同名的问题了。事实上编译通不过!很遗憾!看来能调用接口名.方法名()的只能c Sharp自己的编译器了,我们手工办不到。