| Constructor (computer Science) |
Article Index for Constructor |
Information AboutConstructor (computer Science) |
|
Java Example public class Example { //declaration of instance Variable (s). protected int data; //definition of the constructor. public Example() { data = 1; data = 2; } } REALbasic Example Constructors in REALbasic can be in one of two forms. Each form uses a regular method declaration with a special name (and no return value). The older form uses the same name as the Class itself, and the newer form uses the name "Constructor." The newer form is the preferred one because it makes Refactoring your class easier. Class Foobar // Old form Sub Foobar( someParam as String ) End Sub // New form Sub Constructor() End Sub End Class CONSTRUCTORS SIMPLIFIED Constructors exists within classes. Classes in programming language refer to a generic specfication which are not real, but just a prototype. A simple analogy to understanding constructors would be to imagine the following scenerios: Compare class in this case to a class of students. Thus we have Pseudocode class Students{However, the class Students is just a generic prototype of what a student should be. Let's analyse this from the position of the IS manager should the school wants to create a database of every student in a computerised database. Let's assume in this case that they want to create each student as an object. Thus they apply the code below to create a prototype or "mould" to create an instance of the class Students (ie to create a real student under the class Students). These moulds are often used as constructors. Pseudocode class Students{Once done, these specific students that we create using the above are also known as instances of the class Students. To create these instances, we use constructors within a class. A constructor is the same as a mould defining the specifications of the student mould we describe above. It defines the type of student, with variables that distinguishes it from other instances. Constructors are normally named the same as their class name. Thus, to create a constructor of a student in a class with some predefined variables, we do the following: Every student will have characteristics that distinguishes them. For example, students have their individual Name, Sex, Address, Unique ID etc. We want to create a prototype, or "mould" for these students such that we can construct each student's profile using simple code like Students("John","M","Dover Road", 12345). If the analyst wants to print out the information for its students, it can use the following code; Pseudocode print(Students.studentName) // output "John" SEE ALSO REFERENCES
|
|
|