TAILIEUCHUNG - Learn Objective C on the Mac phần 3

Để tạo ra một đối tượng mới, chúng tôi gửi tin nhắn mới đến lớp chúng tôi đang quan tâm. Một khi lớp tiếp nhận và xử lý các tin nhắn mới, chúng tôi sẽ có một trường hợp đối tượng mới để chơi với. Một trong những tính năng tiện lợi của Objective-C là bạn có thể điều trị một lớp giống như một đối tượng và gửi tin nhắn. | CHAPTER 3 Introduction to Object-Oriented Programming 51 you get with freshly allocated memory. When the allocation and initialization steps are done we say that a new object instance has been created. NOTE_ Because an object s local variables are specific to that instance of the object we call them instance variables often shortened to ivars. To create a new object we send the new message to the class we re interested in. Once the class receives and handles the new message we ll have a new object instance to play with. One of the nifty features of Objective-C is that you can treat a class just like an object and send it messages. This is handy for behavior that isn t tied to one particular object but is global to the class. The best example of this kind of message is allocating a new object. When you want a new circle it s appropriate to ask the Circle class for that new object rather than asking an existing circle. Here is Shapes-Object s main function which creates the circle rectangle and spheroid int main int argc const char argv id shapes 3 ShapeRect rect0 0 0 10 30 shapes 0 Circle new shapes 0 setBounds rect0 shapes 0 setFillColor kRedColor ShapeRect rect1 30 40 50 60 shapes 1 Rectangle new shapes 1 setBounds rect1 shapes 1 setFillColor kGreenColor ShapeRect rect2 15 19 37 29 shapes 2 OblateSphereoid new shapes 2 setBounds rect2 shapes 2 setFillColor kBlueColor drawShapes shapes 3 return 0 main 52 CHAPTER 3 Introduction to Object-Oriented Programming You can see that Shapes-Object s main is very similar to Shapes-Procedural s. There are a couple of differences though. Instead of an array of shapes Shapes-Object has an array of id elements which you probably remember are pointers to any kind of object . You create individual objects by sending the new message to the class of object you want to create shapes 0 Circle new shapes 1 Rectangle new shapes 2 OblateSphereoid new .

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã 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.