Escolar Documentos
Profissional Documentos
Cultura Documentos
Console.WriteLine(b.ToString())
End SubEnd Module
METHODS
Option Strict On
Module Example
Class Circle
Public Radius As Integer
Public Sub SetRadius(ByVal Radius As Integer)
Me.Radius = Radius
End Sub
Public Function Area() As Double
Return Me.Radius * Me.Radius * Math.PI
End Function
End Class
Sub Main()
Dim c As New Circle
c.SetRadius(5)
Console.WriteLine(c.Area())
End Sub
End Module
of the object. The name of the constructor in Visual Basic is New. The
constructors are methods, so they can be overloaded too.
Option Strict On
Module Example
Class Being
Sub New()
Console.WriteLine("Being is being created")
End Sub
Sub New(ByVal name As String)
Console.WriteLine("Being {0} is created", name)
End Sub
End Class
Sub Main()
Dim b As New Being
Dim t As New Being("Tom")
End Sub
End Module
INHERITANCE
The inheritance is a way to form new classes using classes that have
already been defined. The newly formed classes are called derived
classes; the classes that we derive from are called base classes. Important
benefits of inheritance are code reuse and reduction of complexity of a
program. The derived classes (descendants) override or extend the
functionality of base classes (ancestors).
Option Strict On
Module Example
Class Being
Sub New()
Console.WriteLine("Being is created")
End Sub
End Class
Class Human
Inherits Being
Sub New()
Console.WriteLine("Human is created")
End Sub
End Class
Sub Main()
Dim h As New Human
End Sub
End Module
In this program, we have two classes. A base Being class and a derived
Human class. The derived class inherits from the base class.
Member Functions and Encapsulation
A member function of a class is a function that has its definition or its
prototype within the class definition like any other variable. It operates on
any object of the class of which it is a member and has access to all the
members of a class for that object.
Member variables are attributes of an object (from design perspective)
and they are kept private to implement encapsulation. These variables
can only be accessed using the public member functions.
Let us put above concepts to set and get the value of different class
members in a class:
Modulemybox
ClassBox
Public length AsDouble' Length of a box
Public breadth As Double 'Breadth of a box
Public height AsDouble' Height of a box
Public Sub setLength(ByVallen As Double)
length = len
End Sub
Public Sub setBreadth(ByValbre As Double)
breadth = bre
End Sub
Public Sub setHeight(ByValhei As Double)
height = hei
End Sub
Public Function getVolume() As Double
Return length * breadth * height
End Function
End Class
Sub Main()
Dim Box1 As Box = New Box()
Volume of Box2:1560
Me.x = x
Me.y = y
Me.Radius = r
End Sub
Public Overrides Function Area() As Double
Return Me.Radius * Me.Radius * Math.PI
End Function
Public Overrides Function ToString() As String
Return String.Format("Circle, at x: {0}, y: {1}, radius: {2}", _
Me.x, Me.y, Me.Radius)
End Function
End Class
Sub Main()
Dim c as New Circle(12, 45, 22)
Console.WriteLine(c)
Console.WriteLine("Area of circle: {0}", c.Area())
Console.WriteLine(c.GetCoordinates())
End Sub
End Module
Polymorphism
Polymorphism allows objects to be represented in multiple forms. Even
though classes are derived or inherited from the same parent class, each
derived class will have its own behavior. Polymorphism is a concept linked
to inheritance and assures that derived classes have the same functions
even though each derived class performs different operations.Figure 2
shows a Rectangle, a Circle, and Square. All of them are shapes and as
shapes their area and perimeter can be calculated; however, each shape
calculates its area in a specialized way. Declaring a member as abstract
allows polymorphism. The Shape class defines the CalculateArea() and
CalculatePerimeter() methods as abstract, this allows each derived class to
override the implementation of the parent's methods.To following sample
code shows an implementation of a derived class (rectangle). The specific
CalculateArea() and CalculatePerimeter() methods for the rectangle class
illustrate polymorphism:
Code
1. using System;
2. namespace DotNetTreats.OOSE.OOPSamples
3. {
4.
5.
6.
7.
8.
9.
10.
_height = height;
11.
_width = width;
12.
13.
14.
15.
get
16.
17.
return _height;
18.
19.
set
20.
21.
_height = value;
22.
23.
24.
25.
26.
get
27.
28.
return _width;
29.
30.
set
31.
32.
_width = value;
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
}
}