1.当使用内部类只是为了把一个类隐藏在另外一个类的内部,并不需要在内部类引用外部类对象时,可以将内部类为static,以便取消产生的引用。
2.只有内部类可以声明为static。静态内部类的对象除了没有对生成它的外部类对象的引用特权外,其他与所有内部类完全一样。
实例代码
测试类
public class test {
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("最小值:" + p.getFirst());
System.out.println("最大值:" + p.getSecond());
}
}
public class ArrayAlg {
public static class Pair
{
private double first;
private double second;
public Pair(double f,double s)
{
first = f;
second = s;
}
public double getFirst()
{
return first;
}
public double getSecond()
{
return second;
}
}
public static Pair minmax(double[] values)
{
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
for(double b : values)
{
if(b > max) max = b;
if(b < min) min = b;
}
return new Pair(min,max);
}
}
输出结果
原文地址:http://blog.csdn.net/zhurui_idea/article/details/44876791