After deriving a class, it becomes available and you can use it just as you would any other class. Here is an example: <%@ Page Language="VB" %> <html> <head> <script language="VB" runat="server"> Public Class Circle End Class Public Class Sphere Inherits Circle End Class </script> <title>Exercise</title> </head> <body> <% Dim ball As Sphere = New Sphere %> </body> </html> When a class is based on another class, all public members of the parent class are made available to the derived class that can use them as necessary. While other methods and classes can also use the public members of a class, the difference is that the derived class can call the public members of the parent as if they belonged to the derived class. That is, the child class doesn't have to "qualify" the public members of the parent class when these public members are used in the body of the derived class. This is illustrated in the following program: <%@ Page Language="VB" %> <html> <head> <script language="VB" runat="server"> Public Class Circle Public Radius As Double Public Function CalculateDiameter() As Double Return Radius * 2 End Function Public Function CalculateCircumference() As Double Return CalculateDiameter() * 3.14159 End Function End Class Public Class Sphere Inherits Circle Public Function Describe$() ' Because Sphere is based on Circle, you can access ' any public member(s) of Circle without qualifying it(them) Radius = 28.55 Return "Circle Characteristics<br />" & _ "---------------------------------<br />" & _ "Radius: " & CStr(Radius) & "<br />" & _ "Diameter: " & CStr(CalculateDiameter()) & "<br />" & _ "Circumference: " & _ CStr(CalculateCircumference()) & "<br />" & _ "======================" End Function End Class </script> <title>Exercise</title> </head> <body> <% Dim Ball As Sphere = New Sphere Response.Write(Ball.Describe()) %> </body> </html> This would produce: Based on the relationship between a child class and its parent, you can use Me in the child to access the public members of the parent class. Here are examples: <script language="VB" runat="server"> Public Class Circle Public Radius As Double Public Function CalculateDiameter() As Double Return Radius * 2 End Function Public Function CalculateCircumference() As Double Return CalculateDiameter() * 3.14159 End Function End Class Public Class Sphere Inherits Circle Public Function Describe$() ' Because Sphere is based on Circle, you can access ' any public member(s) of Circle without qualifying it(them) Me.Radius = 22.855 Return "Circle Characteristics<br />" & _ "---------------------------------<br />" & _ "Radius: " & CStr(Me.Radius) & "<br />" & _ "Diameter: " & CStr(Me.CalculateDiameter()) & "<br />" & _ "Circumference: " & _ CStr(Me.CalculateCircumference()) & "<br />" & _ "======================" End Function End Class </script> We mentioned that, when deriving a new class based on an existing one, all public members of the parent class are accessible to its child classes. In fact, those public members are also available to all other non-child classes and external procedures of the parent class. On the other hand, when creating a class, if you think that a certain member would not need to be accessed outside of the class, you should make it private by starting it with the Private keyword. Here is an example: <script language="VB" runat="server"> Public Class Circle Public Radius As Double Private Function Describe$() Return "A circle is a round geometric shape constructed " & _ "so that all considered points of the shape are " & _ "at an equal distance from a common point called " & _ "the center. Also, two equally opposite points from " & _ "the center are at the exact same dictance from that center." End Function Public Function CalculateDiameter() As Double Return Radius * 2 End Function Public Function CalculateCircumference() As Double Return CalculateDiameter() * 3.14159 End Function End Class </script> After creating a class, its private members are not accessible to clients of the class, not even to the children of that class. If you attempt to access a private member of a class outside of that class, you would receive an error. That's what would happen with the following program: <%@ Page Language="VB" %> <html> <head> <script language="VB" runat="server"> Public Class Circle Public Radius As Double Private Function Describe$() Return "A circle is a round geometric shape constructed " & _ "so that all considered points of the shape are " & _ "at an equal distance from a common point called " & _ "the center. Also, two equally opposite points from " & _ "the center are at the exact same dictance from that center." End Function Public Function CalculateDiameter() As Double Return Radius * 2 End Function Public Function CalculateCircumference() As Double Return CalculateDiameter() * 3.14159 End Function End Class Public Class Sphere Inherits Circle Public Function Describe$() ' Because Sphere is based on Circle, you can access ' any public member(s) of Circle without qualifying it(them) Me.Radius = 22.855 Return "Circle Characteristics<br />" & _ "---------------------------------<br />" & _ "Description: " & Describe$() & "<br />" & _ "Radius: " & CStr(Me.Radius) & "<br />" & _ "Diameter: " & CStr(Me.CalculateDiameter()) & "<br />" & _ "Circumference: " & _ CStr(Me.CalculateCircumference()) & "<br />" & _ "======================" End Function End Class </script> <title>Exercise</title> </head> <body> <% Dim Ball As Sphere = New Sphere Response.Write(Ball.Describe()) %> </body> </html> This would produce: When creating a class that would be used as the base of other classes, in some cases, you may want to create a special relationship among a class and its eventual children. For example, you may want to create some members of the parent class that only its derived class can access. These types of members must be created with the Protected keyword. Here is an example: <script language="VB" runat="server"> Public Class Circle Protected Radius As Double Private Function Describe$() Return "A circle is a round geometric shape constructed " & _ "so that all considered points of the shape are " & _ "at an equal distance from a common point called " & _ "the center. Also, two equally opposite points from " & _ "the center are at the exact same dictance from that center." End Function Public Function CalculateDiameter() As Double Return Radius * 2 End Function Public Function CalculateCircumference() As Double Return CalculateDiameter() * 3.14159 End Function End Class </script> Not just the member variables but you can also create a method as protected as long as you intend it only for the class and its children. Remember that a protected member cannot be accessed by the clients of the class, only by the class itself and its children. When accessing the protected members of a class from its children, you can use Me to locate those members: Me gives you access to non-Shared public and protected members of both the parent(s) and its class. Here is an example: <%@ Page Language="VB" %> <html> <head> <script language="VB" runat="server"> Public Class Circle Protected Radius As Double Protected Function Describe$() Return "A circle is a round geometric shape constructed " & _ "so that all considered points of the shape are " & _ "at an equal distance from a common point called " & _ "the center. Also, two equally opposite points from " & _ "the center are at the exact same dictance from that center." End Function Public Function CalculateDiameter() As Double Return Radius * 2 End Function Public Function CalculateCircumference() As Double Return CalculateDiameter() * 3.14159 End Function End Class Public Class Sphere Inherits Circle Public Function Describe$() ' Because Sphere is based on Circle, you can access ' any public member(s) of Circle without qualifying it(them) Me.Radius = 22.855 Return "Circle Characteristics<br />" & _ "---------------------------------<br />" & _ "Description: " & Me.Describe$() & "<br />" & _ "=- -=- -=- -=- -=- -=- -=- -=- -=- -=<br />" & _ "Radius: " & CStr(Me.Radius) & "<br />" & _ "Diameter: " & CStr(Me.CalculateDiameter()) & "<br />" & _ "Circumference: " & _ CStr(Me.CalculateCircumference()) & "<br />" & _ "======================" End Function End Class </script> <title>Exercise</title> </head> <body> <% Dim Ball As Sphere = New Sphere Response.Write(Ball.Describe()) %> </body> </html> This would produce: |
|
|||||||
|