using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
public class Primes : IEnumerable
{
private long min;
private long max;
public Primes()
: this(2, 100)
{
}
public Primes(long minimum, long maximum)
{
min = minimum;
if (min < 2)
{
min = 2;
}
max = maximum;
}
public IEnumerator GetEnumerator()
{
for (long possiblePrime = min; possiblePrime <= max; possiblePrime++)
{
bool isPrime = true;
for (long possibleFactor = 2; possibleFactor <= (long)Math.Floor(Math.Sqrt(possiblePrime)); possibleFactor++)
{
long remainderAfterDivision = possiblePrime % possibleFactor;
if (remainderAfterDivision == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
yield return possiblePrime;
}
}
}
}
}=======================================
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
public class Primes
{
private long min;
private long max;
public Primes()
: this(2, 100)
{
}
public Primes(long minimum, long maximum)
{
min = minimum;
if (min < 2)
{
min = 2;
}
max = maximum;
}
public IEnumerator GetEnumerator()
{
for (long possiblePrime = min; possiblePrime <= max; possiblePrime++)
{
bool isPrime = true;
for (long possibleFactor = 2; possibleFactor <= (long)Math.Floor(Math.Sqrt(possiblePrime)); possibleFactor++)
{
long remainderAfterDivision = possiblePrime % possibleFactor;
if (remainderAfterDivision == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
yield return possiblePrime;
}
}
}
}
}
========================================
这2段代码结果是一样的.区别是第一段代码支持IEnumerable接口,而第二段代码没有支持IEnumerable接口...
想问的是:
为什么不支持IEnumerable接口,怎么能实现IEnumerator GetEnumerator()方法?
这是一个例子,还好多不继承类也不支持接口都能实现他们的方法...我不理解?
using System.Collections;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
public class Primes : IEnumerable
{
private long min;
private long max;
public Primes()
: this(2, 100)
{
}
public Primes(long minimum, long maximum)
{
min = minimum;
if (min < 2)
{
min = 2;
}
max = maximum;
}
public IEnumerator GetEnumerator()
{
for (long possiblePrime = min; possiblePrime <= max; possiblePrime++)
{
bool isPrime = true;
for (long possibleFactor = 2; possibleFactor <= (long)Math.Floor(Math.Sqrt(possiblePrime)); possibleFactor++)
{
long remainderAfterDivision = possiblePrime % possibleFactor;
if (remainderAfterDivision == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
yield return possiblePrime;
}
}
}
}
}=======================================
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
public class Primes
{
private long min;
private long max;
public Primes()
: this(2, 100)
{
}
public Primes(long minimum, long maximum)
{
min = minimum;
if (min < 2)
{
min = 2;
}
max = maximum;
}
public IEnumerator GetEnumerator()
{
for (long possiblePrime = min; possiblePrime <= max; possiblePrime++)
{
bool isPrime = true;
for (long possibleFactor = 2; possibleFactor <= (long)Math.Floor(Math.Sqrt(possiblePrime)); possibleFactor++)
{
long remainderAfterDivision = possiblePrime % possibleFactor;
if (remainderAfterDivision == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
yield return possiblePrime;
}
}
}
}
}
========================================
这2段代码结果是一样的.区别是第一段代码支持IEnumerable接口,而第二段代码没有支持IEnumerable接口...
想问的是:
为什么不支持IEnumerable接口,怎么能实现IEnumerator GetEnumerator()方法?
这是一个例子,还好多不继承类也不支持接口都能实现他们的方法...我不理解?
我知道你说的意思,但是我是要Primes的一个事例迭代集合中的对象,代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
/// <summary>
///
/// </summary>
class Program
{
static void Main(string[] args)
{
Primes primeForm1Tp1000 = new Primes(1, 1000);
foreach (long i in primeForm1Tp1000)
{
Console.Write("{0},", i);
}
Console.ReadKey();
}
}
}
不继承可以有这个函数也可以没有。
并不有这个函数的类都是实现上述接口
9楼的,动态接口是什么?GOOGLE搜不到啊?
那么yield又是干什么的呢?!!
是这个意思吗?
实现IEnumerable接口的话,foreach可以
Primes p;
foreach long l in p
{}
不实现IEnumerable接口的话,通过yield return,foreach需要改成
Primes p;
foreach long l in p.GetEnumerator()//函数名字可以随便改
{}另外,动态接口-Dynamic Interface,google上有的
下面的代码一样可以用
Primes primeForm1Tp1000 = new Primes(1, 1000);
foreach (long i in primeForm1Tp1000)
{
Console.Write("{0},", i);
}
============================
而不需要你说的
foreach long l in p.GetEnumerator()//函数名字可以随便改
{}
试想,一个项目即使不用一个接口也能够实现,但是接口可以说是定义了一个规则或一个约束,所有继承自某接口的类都必须实现该接口的方法,这就给项目的设计带来了很多的灵活性,能够更多的控制项目(这就是为什么总是说要针对抽象编程,依赖倒置原则)。也就是说,如果该类是封装好的,即使不看其具体代码,只要知道它实现了IEnumerable接口,那么这个类就必定能用foreach,如果该类不实现该方法,则会报错。而如果不继承该接口,就不一定会有GetEnumerator方法,就必须关心类的具体内容,这就涉及了类的内部,破坏了类的封装性,因为即使不实现GetEnumerator方法,该类还是正确的,不会报错。
其实对接口的理解也体现了对于面向对象的理解程度,相信搂主继续学下去就会体会到接口的神奇。
还有就是,yield return主要是用来返回一个IEnumerable接口,lz的代码里面却是返回IEnumerator接口,这种用法比较少,更常见的用法是:
public static IEnumerable<int> GetEven(int min, int max)
{
for (int i = min; i < max; i++)
{
if (i % 2 == 0)
yield return i;
}
}
我是个新手好多都不太懂...所以要好好学习...