Date post: | 21-Dec-2015 |
Category: |
Documents |
View: | 220 times |
Download: | 0 times |
Compunet Corporation 1
Programming with Visual Basic .NET
Creating and Using Classes
Lecture # 8
Tariq Ibn Aziz
Compunet Corporation 2
Classes
• A class may contain three type of items:
– Variables (state)
– methods (behavior)
– constructors
• variable store the values
• methods provides the logic, that constitutes the behavior of class
• Constructor initialize the state of a new instance of a class
Class Members
Compunet Corporation 3
Method
• A method is essentially a set of program statement
• Each method exist as part of a class
• Visual Basic program contain one or more classes, each may contain one or more method
• Method may call other methods in the same or a different class
• No program code can exist outside a method and no method can exist outside a class
• Note: Main() is also a method
Compunet Corporation 4
Shared and Instance Members
• There are two ways that an item can be a member of a class– Shared member
– Instance member
• Shared methods are accessed through the class. • Non Shared methods, also called instance
methods, are accessed through instances of the class.
Compunet Corporation 5
Shared Method and Variable
• A Shared method is associated with a class, so no need to create an instance of that class.
• To call Shared method, use the following syntax:
className.mthName(args)
• Shared variable is associated with a class, so no need to create an instance of that class.
• To call Shared variable, use the following syntax:
className.variable1
Compunet Corporation 6
Object-Oriented Programming
• In OOP classes are Types
• Objects are Instances of classes
• Objects may have member data or member methods or both
• The mechanism to create a new Object is called instantiation
Compunet Corporation 7
Three Principles of OOP
• Three language features are fundamental to object-oriented programming
– Encapsulation is a combination of Data and Behavior in one package and hiding the implementation of data from the user
or
– Encapsulation is the mechanism that associate data with code that manipulate it
Compunet Corporation 8
Three Principles of OOP
– Inheritance, The original class is often called the base class or the super class, and the new class is often called the derived class or the subclass. Inheritance is often referred to as extending the base class or super class.
Compunet Corporation 9
Three Principles of OOP
– Polymorphism (from the Greek, meaning "many forms", or something similar) is the quality that allows one name to be used for more than one (hopefully related) but technically different purpose
– Method overriding occurs when a class declare a method having same types of signature as a method by one of its super class
Compunet Corporation 10
Class in Visual Basic
• The following class is a valid class although its empty
Public Class Person
End ClassEmpty Class, i.e.
no members
Compunet Corporation 11
Data in a Class
• A class containing Data (state)class Students
Dim ID As Integer
Dim Name As String
Dim Email As String
End class
Class with member data
Compunet Corporation 12
Data and Method in a Class
• A class containing Data (state) and Method (Behavior)class Students
Dim ID As Integer
Dim Name As String
Dim Email As String
Sub print()
System.Console.Write (ID)
System.Console.Write (Name)
System.Console.Write (Email)
End Sub
End class
Class with member data and member method
Compunet Corporation 13
Creating and Using Class
class PersonPublic Dim Name As StringSub print()
System.Console.Write (Name)End Sub
End class
Public Module M1 Sub Main()
Dim v As New Personv.Name = "Tariq Aziz"v.print()
End SubEnd Module
Class with member data and member method
Using class in MODULE program
Compunet Corporation 14
Creating and Using Class
class PersonPublic Dim Name As StringSub print()
System.Console.Write (Name)End Sub
End class
Class M1 Shared Sub Main()
Dim v As New Personv.Name = "Tariq Aziz"v.print()
End SubEnd Class
Class with member data and member method
Using class in another class
Compunet Corporation 15
Instance Variables in a Class
• Each object has its own copy of all instance variables defined by its class
Dim v As New Person
Public Dim Name As String
Sub print()
System.Console.Write (Name)
End Sub
v
Person class Object
Reference to Object Instance variable
& method
Compunet Corporation 16
Instance Variables in a Class
• Instance variables are initialized to defaults values during the creation of object. Variable of boolean set to false. Numbers are set to zeros. Any variable that acts as an object reference is set to null
class Person
Public ID As Integer
Public Name As String
Public Married As Boolean
End class
Dim As New Person
Public ID As Integer=0
Public Name As String=null
Public Married As Boolean=Falsev
Person class Object
Compunet Corporation 17
Instance Variables Example class A
Public Dim x As Double
Public Dim y As Boolean
Public Dim s As String
End Class
class B
shared Sub Main()
Dim p As New A
System.Console.WriteLine(" p.x = " & p.x)
System.Console.WriteLine(" p.y = " & p.y)
System.Console.WriteLine(" p.s = " & p.s)
End Sub
End class
Output:
p.x = 0
p.y = False
p.s =
Compunet Corporation 18
Instance method Example (Contd.)
Class A
Public Dim x As Double
Public Dim y As Boolean
Public Dim s As String
Sub Display()
System.Console.WriteLine( "x=" & x)
System.Console.WriteLine( "y=" & y)
System.Console.WriteLine( "s=" & s)
End Sub
End Class
Class B
shared Sub main()
Dim p As New A
p.Display()
End Sub
End class
Output:
x = 0
y = False
s =
Compunet Corporation 19
Shared Variables in a Class
• A Shared variable is shared by all objects of its class and thus relates to the class itself
• Shared var1 As Type• Shared variables are initialized to defaults values
when the class is loaded into memory. Variable of boolean set to false. Numbers are set to zeros. Any variable that acts as an object reference is set to null
Compunet Corporation 20
Shared Variables Example class A
Public Shared Dim x As Double
Public Shared Dim y As Boolean
Public Shared Dim s As String = "T"
End Class
class B
shared Sub main()
System.Console.WriteLine ("x = " & A.x)
System.Console.WriteLine ("y = " & A.y)
System.Console.WriteLine ("s = " & A.s)
End Sub
End class
Output:
x = 0
y = False
s = T
Compunet Corporation 21
Shared Method Example
class APublic Shared Dim x As Integer = 10Public Shared Dim y As Boolean = TruePublic Shared Dim s As String ="CA121"Shared Sub Display() System.Console.WriteLine(" x = " & x) System.Console.WriteLine(" y = " & y) System.Console.WriteLine(" s = " & s)End Sub
End Classclass B
shared Sub main() A.Display()End Sub
End class
Output:
x = 10
y = True
s = CA121
Compunet Corporation 22
Exercise (Classes)
• Write an application that declares a class named Person. It should have instance variable to record name, age, and salary. These should be of types String, integer, and Single. Use the new operator to create a Person object. Set and display its instance variable.
• Write an application that declares a class named Sphere. It should have instance variables to record its radius and the coordinates of its center. The should be of type Double. Use the new operator to create a Sphere object. Set and display its instance variable.
Compunet Corporation 23
Polymorphism
• A class may have several Method with same name, it is known as Method overloading/Polymorphism
• Each Method can have same name, but parameters list must be different
• The println() is a good example of this concept. It has several overloaded forms, Each of these accepts one argument of a different type. The type may be Boolean, Char, Integer, Long, Single, Double, String or Object. It is much convenient for programmer to remember one method name rather than several different ones
• Method overloading provides an easy way to handle default parameter values
Compunet Corporation 24
Polymorphism in Instance Method Example 1
Class Point3DPublic Dim x, y As Integer
Sub move(a As Integer)x = a
End Sub Sub move(a As Integer, b As Integer)
x = ay = b
End SubEnd Classclass Point3DConstructorShared Sub Main()
Dim p As new Point3Dp.move(100)
System.Console.WriteLine(p.x & " " & p.y) p.move(200,300) System.Console.WriteLine(p.x & " " & p.y)End SubEnd Class
OUTPUT:
100 0
200 300
Compunet Corporation 25
Polymorphism in Instance Method Example 2
class Point3DPublic Dim x, y As Doublesub Point3D(a as Double, b as Double) x=a y=b System.Console.WriteLine ("x=" & x & "y=" & y)End Subsub Point3D(a as Double) x=a y=200 System.Console.WriteLine ("x=" & x & "y=" & y)End Sub
Polymorphism Example
Compunet Corporation 26
Polymorphism in Instance Method Example 2
Shared Sub main()
Dim p As New Point3D
p.point3d(1.0,2.0)
p.point3d(100.0)
p.x = 1.1
p.y = 3.4
System.Console.WriteLine(" p.x = " & p.x)
System.Console.WriteLine(" p.y = " & p.y)
End Sub
End class
Compunet Corporation 27
Polymorphism in Constructor Method
class Point3DPublic Dim x, y As IntegerSub New()
x = 100End Sub Sub New( a As Integer, b As Integer )
x = ay = b
End SubEnd Classclass Point3DConstructor
Shared Sub Main()Dim p As new Point3DSystem.Console.WriteLine( p.x & " " & p.y)Dim q As New Point3D(200,300)System.Console.WriteLine( q.x & " " & q.y)
End SubEnd Class
Compunet Corporation 28
Exercise
• Write an application that defines a Circle class with two Method. The first form accepts a double value that represents the radius of the circle. This method assumes that the circle is centered at the origin. The second form accepts three double values. The first two arguments defines the coordinates of the center, and the third argument defines the radius