Thinging in java 中的问题

来源:百度知道 编辑:UC知道 时间:2024/05/21 07:18:23
class Person{
public void eat(Apple apple){
Apple peeled = apple.getPeeled();
System.out.println("Yummy");
}
}

class Peeler{
static Apple peel(Apple apple){
return apple;
}
}

class Apple{
Apple getPeeled(){ return Peeler.peel(this);}
}

public class PassingThis{
public static void main(String args[]){
new Person().eat(new Apple());
}
}

最终输出:Yummy
请问:(1)各个类的初始化顺序,以及先调用哪个方法。
(2)new Person().eat(new Apple());中,new Person()分配空间,是给哪个对象分配的空间?这个对象有名字?还是系统默认的对象?

(1)顺序为 Person, Apple
Peeler 没有被初始化因为MAIN里面对只对它调用了STATIC成员

(2) NEW PERSON() 是划分了空间给Person这个对象, 这个对象因为没有被放到任何变量引用里面,所以是系统对象.在呼叫语句
new Person().eat(new Apple());结束后其占用内存就被释放了