那里。
我一直在从核心Java第1卷第9版学习Java,我对这本书的例子有些困惑(代码6.8)
为什么在类的方法名称(minmax
)之前有一个'Pair' 'ArrayAlg'?
核心Java卷1第6章6.4.7静态内部类
public static Pair minmax(double[] values)
的源代码是如下:
package staticInnerClass;
public class StaticInnerClassTest
{
public static void main(String[] args)
{
double[] d = new double[20];
for (int i = 0; i < d.length; i++)
d[i] = 100 * Math.random();
ArrayAlg.Pair p = ArrayAlg.minmax(d);
System.out.println("min = " + p.getFirst());
System.out.println("max = " + p.getSecond());
}
}
class ArrayAlg
{
/**
* A pair of floating-point numbers
*/
public static class Pair
{
private double first;
private double second;
/**
* Constructs a pair from two floating-point numbers
* @param f the first number
* @param s the second number
*/
public Pair(double f, double s)
{
first = f;
second = s;
}
/**
* Returns the first number of the pair
* @return the first number
*/
public double getFirst()
{
return first;
}
/**
* Returns the second number of the pair
* @return the sceond number
*/
public double getSecond()
{
return second;
}
}
/**
* Computes both the minimum and the maximum of an array
* @param values an array of floating-point numbers
* @return a pair whose first element is the minimum and whose second element is the maximum
*/
public static Pair minmax(double[] values)
{
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
for (double v : values)
{
if (min > v)
min = v;
if (max < v)
max = v;
}
return new Pair(min, max);
}
}
这是该方法的返回值的类型。 –
注意:根据JLS,A * static inner *类不存在。它被称为*嵌套类*。一个* inner *类永远不会是静态的。 –