Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes & Objects A deeper Look Chapter 10 & 11

Similar presentations


Presentation on theme: "Classes & Objects A deeper Look Chapter 10 & 11"— Presentation transcript:

1 Classes & Objects A deeper Look Chapter 10 & 11
Dr. John P. Abraham Professor UTPA

2 UML Class Diagram Unified Modeling Language
A graphical language that allows people to use industry standard notations. See an example of UML class dagram on page 131, 136,143 of your textbook.

3 Initializing objects with constructors
The constructor class is called by the class name followed by parenthesis. Constructor subroutine can be declared with the ‘new’ key word. Public sub New(ByVal name as string)

4 Constructors If programmer did not write a constructor, the compiler provides a default parameterless constructor. You can also write a parameterless constructor Overloaded constructors You can provide multiple constructors.

5 Overloaded constructor example
Public Sub New() setTime(12) ‘initialize to noon End sub Public Sub New(byVal hh as integer) setTime(hh) ‘initialize the hour to whatever Public sub new (byval hh as integer, byval mm as integer) setTime(hh,mm) End Sub

6 Modules, Classes and Methods
Both classes and Modules contain methods (subs). Related classes are grouped into namespaces and compiled into library files. A module contains declarations and procedures that are used by other files in a project. Module is not associated with a class or form and contain no event procedures. Good to use in case you are using multiple forms.

7 Partial classes Vb allows a class declaration to span multiple source-code files. This allows programming teams to work on a single class! If a class is stored in different files, each file is a partial class.

8 Composition When a class reference objects of other classes as members of self. Sometimes referred to has has-a relationship. Example A class employee may use an object of day for birthday or hireDate.

9 Shared Class Members Each time an object is created, it gets a portion of the memory. Multiple variables of the same class will have multiple memory portions. Assigning just one portion of memory and sharing it by many objects allows class-wide sharing of information. Declare such members with keyword shared. Shared members can be accessed through the class members name using the dot separator.

10 Const and Read only Members
Const: Once a value is given it can’t be changed. Const must be given a value at declaration Read only can be assigned a value at declaration or at constructor execution. It can only assigned a value once (can’t be changed)

11 Delegates A delegate is an object that holds a reference to a method.
Delegates allow you to treat methods as data. Meaning you can assign methods to variables and pass methods to and from methods.

12 Protected members A base class’s private members are not inherited by derived classes. Protected access offers an intermediate level of access between Public and Private. A base class’s protected members can be accessed only by members of that base class and by members of its derived classes.

13 Friend Members Friend access is another intermediate level access.
If a program uses multiple classes from the same assembly, these classes can access each other’s friend members. Unlike public access, programs that are declared outside the assembly cannot access these Friend Members.

14 Inheritance New class is created from an existing one by adding new or modified capabilities. The new class is a derived class or subclass. A derived class can become a base class for a future derived class. Is-a relationship –represents inheritance (a car is a vehicle) Has-a relationship represents a composition. A car has a steering wheel.

15 Base and derived classes
A rectangle, square, parallelogram, and a trapezoid is a quadrilateral. Thus a (derived) class Rectangle can be said to inherit from (base) class Quadrilateral. An inheritance hierarchy can be drawn to show relationships.

16 Extension Methods You can’t modify code for classes you did not create. In VB 2008 you can use extension methods to add functionality to an existing class without modifying the class’s source code.

17 Time Class Private hourValue As Integer ' 0 - 23
Private minuteValue As Integer ' Private secondValue As Integer ' 0 – 59 Public Sub New() SetTime(12, 0, 0) ' initialize hour to noon; minute, second to 0 End Sub

18 Set & Get Time Public Sub SetTime(ByVal hh As Integer, _
ByVal mm As Integer, ByVal ss As Integer) Hour = hh Minute = mm Second = ss End Sub ' SetTime Public Property Hour() As Integer Get Return hourValue End Get Public Property Minute() As Integer Return minuteValue --do the same for second-

19 24 hour clock Public Function ToUniversalString() As String
Return String.Format("{0}:{1:D2}:{2:D2}", Hour, Minute, Second) End Function

20 Standard 12 hour time Public Overrides Function ToString() As String
Dim suffix As String ' AM or PM suffix Dim standardHour As Integer If Hour < 12 Then suffix = "AM" Else suffix = "PM" End If If (Hour = 12 OrElse Hour = 0) Then standardHour = 12 standardHour = Hour Mod 12 End If Return String.Format("{0}:{1:D2}:{2:D2} {3}", _ standardHour, Minute, Second, suffix) End Function


Download ppt "Classes & Objects A deeper Look Chapter 10 & 11"

Similar presentations


Ads by Google