代码一:
public class CountInstances
{
private static int numInstances=0;
protected static int getNumInstance()
{
return numInstances;
}
CountInstances()
{
numInstances++;
}
public static void main(String[] arg)
{
System.out.println("Starting with "+CountInstances.getNumInstance() +" instance");
for(int i=0;i<10;++i)
new CountInstances();
System.out.println("Starting with "+CountInstances.getNumInstance() +" instance");
}
}
代码二:
public class CountInstances
{
private static int numInstances=0;
protected static int getNumInstance()
{
return numInstances;
}
private static void assInstance()
{
numInstances++;
}
CountInstances()
{
CountInstances.addInstance();
}
public static void main(String[] arg)
{
System.out.println("Starting with "+CountInstances.getNumInstance() +" instance");
for(int i=0;i<10;++i)
new CountInstances();
System.out.println("Starting with "+CountInstances.getNumInstance() +" instance");
}
}
请比较两个代码哪个优化,为什么?