tailieunhanh - Learn Objective C on the Mac phần 7

Nếu bạn biết rằng các chương trình của bạn sẽ chỉ được chạy trên Leopard hoặc sau đó, bạn có thể tận dụng thu gom rác thải của Objective-C , chúng tôi sẽ thảo luận ở cuối chương này. Chúng tôi sẽ không cảm thấy buồn nếu bạn bỏ qua để kết thúc, thực sự. | CHAPTER 10 Object Initialization 199 - id initWithPressure float p if self self initWithPressure p treadDepth return self initWithPressure - id initWithTreadDepth float td if self self initWithPressure treadDepth td return self initWithTreadDepth NOTE__ You don t really need the empty bodies for the if statements as in initWithPressure treadDepth . We like to do that so that all the init methods have a consistent look. Adding the AllWeatherRadial Initializer Now it s time to add an initializer to AllWeatherRadial. The only method we need to add is an override of the designated initializer - id initWithPressure float p treadDepth float td if self super initWithPressure p treadDepth td rainHandling snowHandling return self initWithPressure treadDepth 200 CHAPTER 10 Object Initialization Now when we run the program the proper defaults are set AllWeatherRadial AllWeatherRadial AllWeatherRadial AllWeatherRadial I am a slant-6. VROOOM VROOM indeed Initializer Rules You re not required to create an initializer method for your class. If you don t have any state you need to set up or the default behavior of alloc in clearing everything out to zero is good enough you might choose not to bother with an init. If you do write an initializer be sure you call the superclass s designed initializer in your own designated initializer. If you have more than one initializer pick one to be the designated initializer. That method will be the one that calls the superclass s designated initializer. Implement all of your other initializers in terms of your designated initializer as we did previously. Summary In this chapter you learned all about object allocation and initialization. In Cocoa these are two separate operations alloc a class method that comes from NSObject allocates a chunk of memory and clears it to zero. .

TỪ KHÓA LIÊN QUAN
crossorigin="anonymous">
Đã 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.