Essential ActionScript 3.0 PHẦN 2

Chapter 3 CHAPTER 3 Instance Methods Revisited 4 In Chapter 1, we learned how to create instance methods. In this chapter, we’ll expand that basic knowledge by studying the following additional instance-method topics: • Omitting the this keyword • Bound methods • State-retrieval and state-modification methods • Get and set methods • Extra arguments Along the way, we’ll continue developing the virtual zoo program that we started in Chapter 1. But before we begin, take a minute to reacquaint yourself with the virtual zoo program. Example 3-1 shows the code as we last saw it. Example 3-1. Zoo program // VirtualPet class package zoo { internal. | CHAPTER 3 Instance Methods Revisited In Chapter 1 we learned how to create instance methods. In this chapter we ll expand that basic knowledge by studying the following additional instance-method topics Omitting the this keyword Bound methods State-retrieval and state-modification methods Get and set methods Extra arguments Along the way we ll continue developing the virtual zoo program that we started in Chapter 1. But before we begin take a minute to reacquaint yourself with the virtual zoo program. Example 3-1 shows the code as we last saw it. Example 3-1. Zoo program VirtualPet class package zoo internal class VirtualPet internal var petName private var currentCalories 1000 private var creationTime public function VirtualPet name new Date name public function eat numberOfCalories numberOfCalories public function getAge var currentTime new Date var age - 63 Example 3-1. Zoo program continued return age _ _ VirtualZoo class package zoo public class VirtualZoo private var pet public function VirtualZoo new VirtualPet Stan _ _ Omitting the this Keyword In Chapter 1 we learned that the this keyword is used to refer to the current object within constructor methods and instance methods. For example in the following code the expression name tells ActionScript to set the value of the instance variable petName on the object currently being created public function VirtualPet name name In the following code the expression numberOfCalories tells ActionScript to set the value of the instance variable currentCalories on the object through which the eat method was invoked public function eat numberOfCalories numberOfCalories In code that frequently accesses the variables and methods of the current object including this can be laborious and can lead to clutter. To reduce labor and improve readability .

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.