using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;namespace Ch11Ex03
{
public class Primes
{
private long min;
private long max;
public Primes()
: this(2, 100)/* 这应该是一个构造函数,后面的“this(2, 100)”是什么意思,又是起什么作用? */
{
} public Primes(long minimum, long maximum)
{
if (min < 2)/* “min”是一个私有的字段,声明时没赋值,在这个方法里面的这个步骤之前也没有赋值,那“min < 2”还有何意义呢? */
{
min = 2;
}
min = minimum;
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;
}
}
}
}
}
的意思是调用它本身的另外一个构造函数即下面的:
public Primes(long minimum, long maximum)
using System.Collections;
using System.Collections.Generic;
using System.Text;namespace Ch11Ex03
{
public class Primes
{
private long min;
private long max;
public Primes()
: this(2, 100)/* 这应该是一个构造函数,后面的“this(2, 100)”是什么意思,又是起什么作用? */这句话的意思是调用public Primes(long minimum, long maximum)
这个构造函数 {
} public Primes(long minimum, long maximum)
{
if (min < 2)/* “min”是一个私有的字段,声明时没赋值,在这个方法里面的这个步骤之前也没有赋值,那“min < 2”还有何意义呢? */ 这句话我也不知道什么意思,但是变量min 在类初始化的时候也初始化了 默认值是0 {
min = 2;
}
min = minimum;
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;
}
}
}
}
}
: this(2, 100)
{
}
will call another constructor Primes(long minimum, long maximum) with minimum = 2 and maximum = 100;2, if (min < 2)
{
min = 2;
}
you are right. the code is wrong.
所以,我想,if (min < 2)应该没有错,只是,我还没有理解其中的意欲