head first java second edition phần 5

Làm thế nào trình biên dịch sẽ nhìn thấy: . Tạo một tên gọi là chuỗi biến và thiết lập này là null. . Gán giá trị đầu tiên Trong nameArray để đặt tên. . Chạy cơ thể của vòng lặp (khối mã bao quanh bởi dấu ngoặc nhọn). . Chỉ định các giá trị tiếp theo Trong nameArray | constructors and gc How do you invoke a superclass constructor You might think that somewhere in say a Duck constructor if Duck extends Animal you d call Animal . But that s not how it works public class Duck extends Animal int size public Duckfint newSize size newSize This The only way to call a super constructor is by calling super . That s right super calls the super constructor. What are the odds public class Duck extends Animal Int size public Duck int newSize super 0 -------you M size newSlze And how is it that we ve gotten away without doing it You probably figured that out. Our good friend the compiler puts in a call to super if you don t. So the compiler gets involved in constructor-making In two ways Q If you don t provide a constructor The compiler puts one in that looks like public ClassNameO super 1 Animal is A call to super in your constructor puts the superclass constructor on the top of the Stack And what do you think that superclass constructor does Calls its superclass constructor. And so it goes until the Object constructor is on the top of the Stack Once ObjectO finishes it s popped off the Stack and the next thing down the Slack die subclass constructor that called Object is now on top. That constructor finishes and so it goes until the original constructor is on the top of the Stack where ừ can now finish. 2 If you do provide a constructor but you do not put in the call to super The compiler will put a call to super In each of your overloaded constructors The compiler-supplied call looks like super It always looks like that. The compiler-inserted call to super Is always a no-arg call. If the superclass has overloaded constructors only the no-arg one Is called. Unless the constructor calls another overloaded constructor you ll see that in a few pages . you are here 253 object lifecycle Can the child exist before the parents If you think of a superclass as the parent to the subclass child you can figure out which has to exist first. The .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.