Danh mục

Classes, Top-Level Classes, and Instances Basically

Số trang: 2      Loại file: pdf      Dung lượng: 9.45 KB      Lượt xem: 14      Lượt tải: 0    
10.10.2023

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Lớp Array đã ẩn logic và định nghĩa (code) mà làm việc đằng sau hậu trường để xác định cách một đối tượng Array công trình và làm thế nào nó được sử dụng. Hãy suy nghĩ của một lớp học như là một tiêu bản mà từ đó các đối tượng được tạo ra. Đây là một mô tả mơ hồ của một lớp, nhưng khi bạn tiến bộ thông qua bài học này và được giới thiệu với các khái niệm nhiều hơn, thuật ngữ...
Nội dung trích xuất từ tài liệu:
Classes, Top-Level Classes, and Instances Basically < Day Day Up >Classes, Top-Level Classes, and InstancesBasically, a class is a definition or blueprint of how an object is made up and how itshould act. For example, an instance of the Array class (an Array object) can storemultiple pieces of information in numerically indexed locations (myArray[0],myArray[1], myArray[2], and so on). How can the array do this? It knows how to do thisbecause it was defined that way by the Array class. The Array class has hidden logic anddefinitions (code) that work behind the scenes to define the way an Array object worksand how its used. Think of a class as a template from which objects are created. This is avague description of a class, but as you progress through this lesson and are introduced tomore concepts, terminology, and examples, youll gain a better understanding of what aclass really is.A class generally exists to produce an instance of itself on demand. You create aninstance of a class by invoking the constructor method of that class. For example:var myArray:Array = new Array():The action to the right of the equals sign, new Array() in this example, executes theconstructor method of the Array class. You cannot use the Array class directly. You mustcreate an instance of the class to use any of its properties and methods. When creating thearray instance, the Array class creates an object and then populates it with properties andmethods. In a sense, the Array class is similar to a factory for array objects. When asked,it creates and returns an Array object.There also are classes that you can use without the need to create an instance. This typeof a class is called a top-level class. Examples of this type of class include the Math,Mouse, and Key classes. When you think about it, top-level classes make sense. Is thereever really a need to have more than one instance of the Mouse class or the Math class?With the Math class, you simply pass a number into a method and a result is returned.The Math class doesnt store any of the information that you feed it, so only one copy isneeded. On the other hand, arrays store unique data, so it wouldnt make sense to accessthe Array class directly because you would only be able to have one array.TIPSome programmers call a top-level class a singleton. < Day Day Up >

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