Danh mục

Getters and Setters

Số trang: 5      Loại file: pdf      Dung lượng: 13.24 KB      Lượt xem: 1      Lượt tải: 0    
Hoai.2512

Hỗ trợ phí lưu trữ khi tải xuống: 2,000 VND Tải xuống file đầy đủ (5 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Thu khí và setters Khi bạn đang nhận thức rõ bởi bây giờ, khi bạn tạo một thể hiện của một lớp học mà bạn đang thực sự tạo ra một đối tượng. Thông thường các đối tượng có các thuộc tính, theo định nghĩa của lớp tập của họ. Ví dụ, nhìn vào định nghĩa lớp: lớp nhà nước dân số (var: Số; chức năng nhà nước () (/ / thi công))
Nội dung trích xuất từ tài liệu:
Getters and Setters < Day Day Up >Getters and SettersAs youre well aware by now, when you create an instance of a class youre actuallycreating an object. Often these objects have properties, as defined by their class files. Forexample, look at this class definition:class State { var population:Number; function State() { //Constructor }}This defines the State class. This class has a single property named population, which isobviously used to hold the number of people in the state.Creating an instance of this class would look like this:var northCarolina:State = new State();You can now set the value of the population property of the northCarolina instance in thefollowing manner:northCarolina.population = 8000000;While this may seem fine, problems can arise if you need to change the way in which thevalue of population is determined. Right now, population represents a number, so settinga new numeric value involves nothing more than entering the new number. But what ifthe instances of the State class need to be updated so that when setting the value ofpopulation, a growth percentage of 5 percent is factored in automatically? You could editevery script in every project that references this property to read similar to this:northCarolina.population = 8000000 + (8000000 * .05);But all that editing would take a lot of work. Its better to make your classes versatileenough to handle this kind of change simply by updating the class file. This is wheregetters and setters become useful. Look at the updated State class definition:class State { var population:Number; function State() { //Constructor } function setPopulation(num:Number){ population = num + (num * .05); } function getPopulation():Number{ return population; }}As defined by the class now, setting and getting the population property are handled bymethods, which can be used in the following way:northCarolina.setPopulation(8000000); //automatically adds a 5% growth rate when setnorthCarolina.getPopulation(); // returns a value of 8400000Either of these getter or setter methods could be changed or enhanced as needed fromwithin the class definition. As a result, all scripts in all projects that use the State classwould automatically reflect the new functionality.Because of the versatility of getters and setters, getting and setting property valuesdirectly is considered bad coding practice. Set up and use getter and setter methodsinstead.Implicit get and set MethodsNow that you understand the power and efficiency of using getter and setter methods, itstime to introduce alternate syntax that might seem a 180-degree turn from what you justlearned. Look at the following updated State class definition:class State { var statePopulation:Number; function State() { //Constructor } function set population(num:Number){ statePopulation = num + (num * .05); } function get population():Number{ return statePopulation; }}While it might not be obvious, the names of the setPopulation() and getPopulation()methods have been changed to set population and get population, respectively. Thischange converts the methods to what are known as implicit get and set methods. Whatdoes this mean? You get the best of both worlds—property values can be set or retrievedwithin the class file by using functions, but referencing them in a script is as easy as this:northCarolina.population = 8000000;or this:var myVariable:Number = northCarolina.population;With this syntax, it seems as if were once again referencing the population propertydirectly, but were actually calling either the set population or get population method(depending on the task) to take care of the states population. Notice that we changed thename of the population property to statePopulation. If we hadnt done this, using thefollowing syntax:northCarolina.population = 8000000;would result in an error. Flash wouldnt know if we were attempting to set the propertynamed population or invoking the set population set method, because doing eitherrequires the same syntax. Changing the population property name to statePopulationsolves this problem.NOTEUsing implicit get and set methods offers no technical advantages over using the getterand setter methods described in the previous section, other than saving a few keystrokes. < Day Day Up >

Tài liệu được xem nhiều: