Upload
achilles-tocci
View
46
Download
1
Tags:
Embed Size (px)
DESCRIPTION
Introduction to Classes & Objects. Chapter 4. Quotes for Today. You will see something new. Two things. And I call them Thing One and Thing Two. Dr. Theordor Seuss Geisel Nothing can have value without being an object of utility. Karl Marx. Classes. Properties, Methods, & Constructors. - PowerPoint PPT Presentation
Citation preview
Introduction to Classes & Objects
Chapter 4
04/19/2023 VBN2008-04 1
Quotes for Today
You will see something new. Two things. And I call them Thing One and Thing Two.
Dr. Theordor Seuss Geisel
Nothing can have value without being an object of utility.
Karl Marx
04/19/2023 VBN2008-04 2
Classes
Properties, Methods, & Constructors
04/19/2023 VBN2008-04 3
Methods & Attributes
• Method represents task in a program– Describes the mechanisms that actually
perform its tasks– Hides from its user the complex tasks
that it performs– Method same as a Procedure
• Classes contain one or more attributes– Specified by instance variables– Carried with the object as it is used
04/19/2023 VBN2008-04 4
Class
• Is a blueprint or template for one or more objects
• Defines what the object can do• Plenty of pre-defined classes
available in Visual Basic– Ex. System.Math
System.Windows.Forms.Form
04/19/2023 VBN2008-04 5
Object
• Is a user interface element• Can be created on VB form using
control in Toolbox• Forms are objects• Inherit functionality from their class
04/19/2023 VBN2008-04 6
Method
• Is a special statement that performs an action or a service for a particular object– Syntax Object.Method(Value)– Example Math.Min(3,5)
Math.Min(Math.Min(3,5),4)
04/19/2023 VBN2008-04 7
Methods cont
• Any class or module that contains a Main method can be used to execute an application
• Visual Basic is extensible– Programmers can create new classes
• Class instance creation expression– Keyword New– Then name of class to create and add parentheses
• Calling a method (Class.Method)– Object’s name, then dot separator (.)– Then method’s name and parentheses
04/19/2023 VBN2008-04 8
Example of Method
' Fig. 4.1: GradeBook.vb
' Class declaration with one method.Public Class GradeBook ‘ Method Header ' display a welcome message to the GradeBook user Public Sub DisplayMessage()
Console.WriteLine("Welcome to the Grade Book!") End Sub ' DisplayMessageEnd Class ' GradeBook
04/19/2023 VBN2008-04 9
When Method is called, it is output to
screen
UML Diagramming
• Unified Modeling Language– Developed by the “three amigos”:
• Grady Booch – Booch Method• James Rumbaugh – OMT (Object Modeling
Technique) • Ivar Jacobson – OOSE (Object-Oriented
Software Engineering)
04/19/2023 VBN2008-04 10
UML Two Goals
• Provide consistency in giving feedback to project sponsor that the problem domain is well understood.
• Provide a consistent model for proper software implementation.
• Based on Synergy Model
04/19/2023 VBN2008-04 11
UML 9 Diagrams
04/19/2023 VBN2008-04 12
Activity Class Collaboration
Component
Deployment
Use Case
Sequence State
Some also Include
Class
Use Case
Sequence
UML Class Diagrams
• Top compartment – name of class
• Middle compartment – class’s attributes or instance variables
• Bottom compartment– Class’s operations or methods– Plus sign indicates Public modifier
04/19/2023 VBN2008-04 13
Example of UML Diagram
• Indicates that class GradeBook has a public DisplayMessage operation.
04/19/2023 VBN2008-04 14
Declaring Methods
• Method parameters– Information passed to method– Called arguments– Supplied in the method call
• Input method– Reads line of input - Console.ReadLine– Dim nameOfCourse As String = _
Console.ReadLine()
04/19/2023 VBN2008-04 15
Method with Parameter
• Parameters specified in method’s parameter list– Part of method header– Uses a comma-separated list– Keyword ByVal
• The argument is passed by value
04/19/2023 VBN2008-04 16
More about ByVal &
ByRef later
UML Example
• indicates that class GradeBook has a DisplayMessage operation with a courseName parameter of type String.
04/19/2023 VBN2008-04 17
Instance Variables & Properties
• Variables declared in the body of method– Called local variables– Can only be used within that method
• Variables declared in a class declaration– Called fields or instance variables– Each object of the class has a separate
instance of the variable04/19/2023 VBN2008-04 18
' Fig. 4.7: GradeBook.vb' GradeBook class that contains instance variable courseNameValue' and a property to get and set its value.Public Class GradeBook Private courseNameValue As String ' course name for this GradeBook ' property CourseName Public Property CourseName() As String Get ' retrieve courseNameValue Return courseNameValue End Get Set(ByVal value As String) ' set courseNameValue courseNameValue = value ' store the course name in the object End Set End Property ' CourseName ' display a welcome message to the GradeBook user Public Sub DisplayMessage() ' use property CourseName to display the ' name of the course this GradeBook represents Console.WriteLine("Welcome to the grade book for " _ & vbCrLf & CourseName & "!") End Sub ' DisplayMessageEnd Class ' GradeBook
Example
04/19/2023 VBN2008-04 19
Instance variable courseNameValue
Get accessor for property courseNameValue
Set accessor for property courseNameValue
Calls the Get accessor of property CourseName
Helpful Console Hints
04/19/2023 VBN2008-04 20
Predefined Constant Identifiers
• vbCrLf– Represents a combination of carriage
return and linefeed character– Outputting this constant’s value causes
subsequent text to display at the beginning of the next line
• vbTab– A constant that represents a Tab
character
04/19/2023 VBN2008-04 21
Private keyword
• Used for most instance variables• Private variables and methods are
accessible only to methods of the class in which they are declared
• Declaring instance variables Private is known as information hiding
04/19/2023 VBN2008-04 22
Instance Variables & Properties continued
• Property Declaration– Declaration consist of an access modifier,
keyword Property, name with parentheses, and type
– Get and Set allows you to access and modify private variables outside of the class, respectively
– Contain a Get accessor, Set accessor, or both
– After defining a property, you can use it like a variable
( object_Name.Property_Name )04/19/2023 VBN2008-04 23
Instance Variables & Properties continued
• Get and Set Accessors– Get accessor contains a Return
statement – Set accessor assigns a value to its
corresponding instance variable
04/19/2023 VBN2008-04 24
Instance Variables & Properties continued
• Default initial value– Provided for all fields not initialized
• 0 for numeric/value type variables• Nothing for Strings and reference types
04/19/2023 VBN2008-04 25
04/19/2023 VBN2008-04 26
' display initial value of property CourseName (invokes Get)Console.WriteLine( "Initial course name is: " _
& gradeBook.CourseName & vbCrLf)‘ prompt for course name
Console.WriteLine("Please enter the course name:")‘ read course name
Dim theName As String = Console.ReadLine()gradeBook.CourseName = theName ' set the CourseName (invokes Set)Console.WriteLine() ' output a blank line
‘display welcome message including the course name (invokes Get)gradeBook.DisplayMessage()End Sub ' Main
End Module ' GradeBookTest
Calls the Get accessor of property CourseName
Calls the Get accessor of property CourseName
Calls the Set accessor of property
CourseName
UML Diagram
– Model properties in the UML as attributes:• Public is indicated by the “+” sign• <<Property>>• Property’s name “:” property’s type• If the property only contains a Get accessor,
then place “{ReadOnly}” after the property’s type
– Modeling Private instance variables that are not properties:• Attribute’s name “:” attribute’s type• Private is indicated by the “-” sign04/19/2023 VBN2008-04 27
UML Class Diagram
04/19/2023 VBN2008-04 28
Indicates:• class GradeBook has a courseNameValue
attribute of type String• one public property and • one method
Instance Variables & Properties continued
• Public variable – can be read or written by any property
or method
04/19/2023 VBN2008-04 29
Instance Variables & Properties continued
• Private variables – can only be accessed indirectly through
the class’s non-Private properties – Class able to control how the data is set
or returned– Allows for data validation– Properties of a class should use class’s
own methods to manipulate the class’s Private instance variables• Creates more robust class
04/19/2023 VBN2008-04 30
Value Types vs. Reference Types
Types in Visual Basic
04/19/2023 VBN2008-04 31
Types
– Value (primitive types except String)• Contains a value of that type• List of Primitive Types in Appendix L
– Reference (sometimes called non-primitive types)• Objects• Default value of Nothing• Used to invoke an object’s methods and
properties
04/19/2023 VBN2008-04 32
Value Type Variable
04/19/2023 VBN2008-04 33
Reference Type Variable
04/19/2023 VBN2008-04 34
Constructors
04/19/2023 VBN2008-04 35
Constructors
– Initialize an object of a class– Required for every class– Provides a default no-argument
constructor ONLY when none is provided– Called when keyword New is followed by
the class name and parentheses– Can also take arguments– Header similar to Sub method header
except name is replaced with keyword New
04/19/2023 VBN2008-04 36
Constructor Example
' Fig. 4.12: GradeBook.vb' GradeBook class with a constructor to initialize the course name.Public Class GradeBook Private courseNameValue As String ' course name for this GradeBook ' constructor initializes course name with String supplied as argument Public Sub New(ByVal name As String) CourseName = name
' initialize courseNameValue via property End Sub ' New
Sub Main() ' create GradeBook object Dim gradeBook1 As New GradeBook( _ "CS101 Introduction to Visual Basic Programming") Dim gradeBook2 As New GradeBook( _ "CS102 Data Structures in Visual Basic")
04/19/2023 VBN2008-04 37
Constructor to initialize courseNameValue variable
Create second grade book object
Call constructor to create first grade book object
UML Class Diagram
– Constructors go in third compartment– Place “<<constructor>>” before New
and its arguments– By convention, place constructors first in
their compartment
04/19/2023 VBN2008-04 38
UML class diagram
04/19/2023 VBN2008-04 39
Indicates that class GradeBook has a constructor that has a name parameter of type String.
Validating Data with Set Accessors in Properties
04/19/2023 VBN2008-04 40
• Validations should be made in the Set accessor to check if the data is valid
• By default, the Get and Set accessor has the same access as the property, however they can vary.
• String– Length property returns the number of characters in the String
– Substring returns a new String object created by copying part of an existing String object
– To display a double quote, use two double quotes in a row
41
UML Class Diagrams
• Allows suppression of class attributes and operations– Called an elided diagram
• Solid line that connects two classes represents an association– numbers near end of each line are
multiplicity values
04/19/2023 VBN2008-04
Multiplicity Types
Symbol Meaning
0 None
1 One
m An integer value
0..1 Zero or one
m, n m or n
m..n At least m, but not more than n
* Any nonnegative integer (zero or more)
0..* Zero or more (identical to *)
1..* One or more
4204/19/2023 VBN2008-04
43
UML Class Diagrams
• Solid diamonds attached to association lines indicate a composition relationship
• Hollow diamonds indicate aggregation – a weaker form of composition
04/19/2023 VBN2008-04
Class Diagram showing Composition Relationships
4404/19/2023 VBN2008-04
Class Diagram for the ATM Model
4504/19/2023 VBN2008-04
Composition Relationships of a class Car.
46
Class Diagram
04/19/2023 VBN2008-04
ATM System Model including Class Deposit.
47
Class Diagram
04/19/2023 VBN2008-04
Next?
04/19/2023 VBN2008-04 48