As mentioned already, a default constructor is always automatically created if you don't create any constructor. Instead of using the default constructor, you can create your own constructor and pass it at least one argument. Here is an example: <script runat="server"> public class House { public long PropertyNumber; public House(long nbr) { } } </script> When implementing the constructor, you can assign the argument to its corresponding field. Here is an example: <script runat="server"> public class House { public long PropertyNumber; public House(long nbr) { PropertyNumber = nbr; } } </script> In the same way, you can use that constructor to initialize the other fields with the default values. When you declare a variable from the class, you can use this constructor to initialize the object. Here is an example: <%@ Page Language="C#" %> <html> <head> <script runat="server"> public class House { public long PropertyNumber; public string PropertyType; public uint Bedrooms; public double Value; public House(long nbr) { PropertyNumber = nbr; PropertyType = "Condominium"; Bedrooms = 1; Value = 0; } public string Describe() { string description; description = "<pre>=//= Altair Realty =//=<br />" + "Properties Inventory<br />" + "Property #: " + PropertyNumber + "<br />" + "Property Type: " + PropertyType + "<br />" + "Bedrooms: " + Bedrooms + "<br />" + "Market Value: " + Value + "</pre>"; return description; } } </script> <title>Exercise</title> </head> <body> <% House property = new House(290375); %> <% Response.Write(property.Describe()); %> </body> </html>
The default constructor is the favorite place to provide default values to the members of a class. Besides the default constructor, you can add as many constructors as you judge necessary. This also means that constructor can be overloaded. The rules to overload a constructor are the same as for methods: the constructors must have either different types of arguments or different numbers of arguments. Here is an example: <script runat="server"> public class House { public long PropertyNumber; public string PropertyType; public uint Bedrooms; public double Value; public House() { PropertyNumber = 0; PropertyType = "Condominium"; Bedrooms = 1; Value = 0; } public House(long nbr) { PropertyNumber = nbr; PropertyType = "Condominium"; Bedrooms = 1; Value = 0; } public House(long nbr, string type, uint beds, double value) { PropertyNumber = nbr; PropertyType = type; Bedrooms = beds; Value = value; } } </script> After creating the constructors, when declaring a variable from the class, you can use the constructor that suits you at one particular moment to initialize an object. If you create a class with only one constructor, when declaring an instance of the class, you must use that constructor: you cannot use the default constructor that doesn't take an argument. If you create a class with only one constructor and that constructor has at least one argument, the default constructor would not be available anymore. If you want to access a default constructor of an object, you have two alternatives:
As opposed to a constructor, a destructor is called when a program has finished using an object. A destructor does the cleaning behind the scenes. Unlike the constructor, the destructor cannot be overloaded. This means that, if you decide to create a destructor, you can have only one. Like the default constructor, a destructor also has the same name as its class. This time, the name of the destructor starts with a tilde "~". To create a destructor, type ~ followed by the name of the class. Here is an example: <script runat="server"> public class House { public long PropertyNumber; public string PropertyType; public uint Bedrooms; public double Value; public House() { PropertyNumber = 0; PropertyType = "Condominium"; Bedrooms = 1; Value = 0; } public ~House() { } } </script> |
|
|||||||
|