我的假设是,通过编译器优化(例如,内联),方法是否“嵌套”几个级别实际上没有区别。真的会是这样吗?
例如,假设声明了以下 3 个类:
public class Third extends Second
{
public int test3() // Call test2() and nothing else
{
return super.test2();
}
}
public class Second extends First
{
public int test2()
{
return super.test1(); // Call test1() and nothing else
}
}
public class First
{
public int test1() // Calculate a result somehow
{
int result = 0;
...
return result;
}
}
给定 3 个已经实例化的对象和third
,以下调用的成本实际上是否相同?second
first
third.test3();
third.test2();
third.test1();
second.test2();
second.test1();
first.test1();
如果方法的名称相同,优化会有什么不同吗?