Download presentation
Presentation is loading. Please wait.
Published byBrent Dean Modified over 9 years ago
1
Chapter 10: Writing Class Definitions Visual Basic.NET Programming: From Problem Analysis to Program Design
2
Visual Basic.NET Programming: From Problem Analysis to Program Design2 Objectives Examine the three-tier design model Write a class definition Design and write overloaded constructors Create polymorphic methods Write properties Create shared attributes and methods
3
Visual Basic.NET Programming: From Problem Analysis to Program Design3 Examining the Three-Tier Design Model Three-tier design –Places objects in OO system into three categories of classes: Problem domain GUI Data access –Developers define categories of classes when designing and building system
4
Visual Basic.NET Programming: From Problem Analysis to Program Design4
5
5 Writing a Class Definition Class definition –VB.NET code representing a class Attributes –Variables populated with data describing object Methods –Model object’s behavior
6
Visual Basic.NET Programming: From Problem Analysis to Program Design6 Drawing a Class Diagram Class diagram –UML diagram –Represents class –Rectangular shape –Contains: Class name Attributes Methods
7
Visual Basic.NET Programming: From Problem Analysis to Program Design7
8
8 Class Definition Syntax Class header Attribute definitions Method code End Class keyword
9
Visual Basic.NET Programming: From Problem Analysis to Program Design9
10
10 Defining Attributes Define attributes by declaring variables Similar to variable declaration –Replace Dim keyword with access keyword: Public Private –Example: Public name As String
11
Visual Basic.NET Programming: From Problem Analysis to Program Design11 Example 10-1: Class Definition for Customer 1. Public Class Customer 2. Public name As String 3. Public address As String 4. Public phoneNo As String 5. End Class
12
Visual Basic.NET Programming: From Problem Analysis to Program Design12 Naming Conventions Class names –Begin with capital letter –Examples: Customer Student
13
Visual Basic.NET Programming: From Problem Analysis to Program Design13 Naming Conventions (continued) Attribute names –Begin with lowercase character –Subsequent words in start with capital letter –Examples: address phoneNo
14
Visual Basic.NET Programming: From Problem Analysis to Program Design14 Naming Conventions (continued) Method names –Begin with capital letter –Subsequent words also start with capital letter –Usually contain imperative verb describing what method does followed by noun –Examples: GetPhoneNo SetAddress ComputeLease
15
Visual Basic.NET Programming: From Problem Analysis to Program Design15 Instantiating a Class Create instances of class –Each instance represents specific occurrence of class Use New keyword: –aCustomer = New Customer
16
Visual Basic.NET Programming: From Problem Analysis to Program Design16 Example 10-2: Client Module to Instantiate Customer (excerpt) 4. Sub Main() 5. Dim aCustomer As Customer 6. ' create a Customer instance 7. aCustomer = New Customer 8. ' populate the attributes 9. aCustomer.name = “Eleanor” 10. aCustomer.address = “Atlanta” 11. aCustomer.phoneNo = “123-4567”
17
Visual Basic.NET Programming: From Problem Analysis to Program Design17
18
Visual Basic.NET Programming: From Problem Analysis to Program Design18
19
Visual Basic.NET Programming: From Problem Analysis to Program Design19 Writing Accessor Methods Encapsulation –Requires that internal data and structure of class be hidden from outside objects –Requires that class have methods providing access to attributes Attributes should be Private
20
Visual Basic.NET Programming: From Problem Analysis to Program Design20 Writing Accessor Methods (continued) Accessor methods –Provide access to attributes –Often called standard methods –Not shown on class diagram Assumed to exist Custom methods –Perform functions for class –Shown on class diagram
21
Visual Basic.NET Programming: From Problem Analysis to Program Design21 Writing Accessor Methods (continued) Accessor method types: –Get Also called Getter Named Get + attribute name Function procedure –Set Also called Setter Named Set + attribute name Sub procedure
22
Visual Basic.NET Programming: From Problem Analysis to Program Design22
23
Visual Basic.NET Programming: From Problem Analysis to Program Design23 Example 10-3: Customer Class Definition with Accessor Methods (excerpt) 7. 'get accessor methods 8. Public Function GetName() As String 9. Return name 10. End Function 'set accessor methods 21. Public Sub SetName(ByVal aName As String) 22. name = aName 23. End Sub
24
Visual Basic.NET Programming: From Problem Analysis to Program Design24 Writing a Parameterized Constructor Constructor –Special method –Automatically invoked whenever you create an instance of class using keyword New –Always named New –Written as Sub procedure Cannot return value VB.NET creates default constructor –Invoked when you instantiate class
25
Visual Basic.NET Programming: From Problem Analysis to Program Design25 Writing a Parameterized Constructor (continued) Parameterized constructor –Can contain parameter list –Arguments populate instance attributes
26
Visual Basic.NET Programming: From Problem Analysis to Program Design26 Example 10-5: Customer Class Definition with Parameterized Constructor (excerpt) 5. ' parameterized constructor 6. Public Sub New(ByVal aName As String, ByVal anAddress As String, ByVal aPhoneNo As String) 7. SetName(aName) 8. SetAddress(anAddress) 9. SetPhoneNo(aPhoneNo) 10. End Sub
27
Visual Basic.NET Programming: From Problem Analysis to Program Design27 Example 10-6: Client invoking Parameterized Constructor in Customer (excerpt) 5. Dim aCustomer As Customer 6. ' instantiate Customer 7. aCustomer = New Customer(“Eleanor”, “Atlanta”, “123-4567”)
28
Visual Basic.NET Programming: From Problem Analysis to Program Design28 Example 10-7: Creating Multiple Customer Instances (excerpt) 5. Dim customer1, customer2, customer3 As Customer 6. ' create 3 instances of Customer 7. customer1 = New Customer(“Eleanor”, “Atlanta”, “123-4567”) 8. customer2 = New Customer(“Emily”, “St. Louis”, “467-1234”)
29
Visual Basic.NET Programming: From Problem Analysis to Program Design29
30
Visual Basic.NET Programming: From Problem Analysis to Program Design30 Designing and Writing Overloaded Constructors Procedure signature: –Name –Parameter list –Used to identify procedure Can overload constructor –Must use different parameter list –Can use Me keyword to call another constructor
31
Visual Basic.NET Programming: From Problem Analysis to Program Design31 Creating Polymorphic Methods Polymorphism –Means “many forms” –Different methods in different classes with same signature do different things
32
Visual Basic.NET Programming: From Problem Analysis to Program Design32
33
Visual Basic.NET Programming: From Problem Analysis to Program Design33 Writing Properties Property –Similar to accessor methods –Appears as public attribute to client objects –Retrieve contents of property as if it were a variable –Begins with header –Ends with End Property
34
Visual Basic.NET Programming: From Problem Analysis to Program Design34 Example 10-16: Customer Class Definition with Properties (excerpt) 11. ' property named CustomerName 12. Public Property CustomerName() As String 13. Get 14. Return name 15. End Get
35
Visual Basic.NET Programming: From Problem Analysis to Program Design35 Example 10-16: Customer Class Definition with Properties (continued) 16. Set(ByVal aName As String) 17. name = aName 18. End Set 19. End Property
36
Visual Basic.NET Programming: From Problem Analysis to Program Design36 Creating Shared Attributes and Methods Attributes and methods can be shared by all instances of class Called class methods and attributes Non-shared methods and attributes: –Called instance methods and attributes
37
Visual Basic.NET Programming: From Problem Analysis to Program Design37 Example 10-18: Class Definition for Circle with Shared Attribute and Method (excerpt) 4. ' shared attribute 5. Private Shared PI As Double = 3.14159 21. ' shared method 22. Public Shared Function GetPI() As Double 23. Return PI 24. End Function
38
Visual Basic.NET Programming: From Problem Analysis to Program Design38 Programming Example: Electricity Billing Redesign of Electricity Billing Programming Example at end of Chapter 9 –Includes class named ElectricBill to represent electricity bills
39
Visual Basic.NET Programming: From Problem Analysis to Program Design39 Programming Example: Electricity Billing (continued) Invokes Client methods to: –Compute: Electricity charge Sales tax –Display: Electricity consumed Electricity charge Sales tax Total bill
40
Visual Basic.NET Programming: From Problem Analysis to Program Design40
41
Visual Basic.NET Programming: From Problem Analysis to Program Design41 Summary Three-tier design places objects into three categories of classes: –Problem domain –Graphical user interface (GUI) –Data access Draw class diagram to model classes Create instances of class using keyword New
42
Visual Basic.NET Programming: From Problem Analysis to Program Design42 Summary (continued) Accessor methods –Methods that provide access to attributes –Get –Set Constructor –Invoked whenever you create an instance of class using keyword New
43
Visual Basic.NET Programming: From Problem Analysis to Program Design43 Summary (continued) Polymorphism –Different methods in different classes with same signature that do different things Property –Similar to accessor method –Appears as a public attribute Shared attributes and methods –Shared among all class instances
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.