intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Classes, Top-Level Classes, and Instances Basically

Chia sẻ: Qweqwdasd Qweqdasda | Ngày: | Loại File: PDF | Số trang:2

73
lượt xem
4
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

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ữ...

Chủ đề:
Lưu

Nội dung Text: Classes, Top-Level Classes, and Instances Basically

  1. < Day Day Up > Classes, Top-Level Classes, and Instances Basically, a class is a definition or blueprint of how an object is made up and how it should act. For example, an instance of the Array class (an Array object) can store multiple 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 this because it was defined that way by the Array class. The Array class has hidden logic and definitions (code) that work behind the scenes to define the way an Array object works and how it's used. Think of a class as a template from which objects are created. This is a vague description of a class, but as you progress through this lesson and are introduced to more concepts, terminology, and examples, you'll gain a better understanding of what a class really is. A class generally exists to produce an instance of itself on demand. You create an instance 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 the constructor method of the Array class. You cannot use the Array class directly. You must create an instance of the class to use any of its properties and methods. When creating the array instance, the Array class creates an object and then populates it with properties and methods. In a sense, the Array class is similar to a factory for array objects. When asked, it creates and returns an Array object.
  2. There also are classes that you can use without the need to create an instance. This type of 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 there ever 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 doesn't store any of the information that you feed it, so only one copy is needed. On the other hand, arrays store unique data, so it wouldn't make sense to access the Array class directly because you would only be able to have one array. TIP Some programmers call a top-level class a singleton. < Day Day Up >
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2