java - Core Java Volume 1 第 6 章 6.4.7 静态内部类

标签 java inner-classes

那里。
我一直在从 Core Java Volume 1 第 9 版学习 Java,我对书中的一个示例有点困惑( list 6.8)
为什么“ArrayAlg”类的方法名称(minmax)之前有一个“Pair”?

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);
    }
}

最佳答案

声明 public static Pair minmax(double[] value) 中的 Pair 是方法的返回类型。 Pair 是一个内部类这一事实是无关紧要的,如果它是一个顶级类,则声明看起来会完全相同(当然,假设您导入该类)。

关于java - Core Java Volume 1 第 6 章 6.4.7 静态内部类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32986461/

相关文章:

java - Netbeans - 读取 src 文件夹中的数据文件

scala - 丰富一个内部类

java - 模拟 java.lang.Exception : Class should be public when I use inner classes in tests

java - 从另一个类的方法中调用 mutator

java - 类文件中的内部类属性有什么用?

单独文件中的 Java 静态嵌套类

java - 检测加载程序的 SQLite 数据库上的更改

java - 子流中列表大小的总和

java - LogHandler 刷新与发布中的直接输出

java - Tomcat 6 和 log4j 应用程序日志记录不产生输出