220 likes | 237 Views
Clearly Visual Basic: Programming with Visual Basic 2008. Chapter 27 I Love this Class. Objectives. Define a class Instantiate an object from a class that you define Add Property procedures to a class Include data validation in a class Create a default constructor
E N D
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class
Objectives • Define a class • Instantiate an object from a class that you define • Add Property procedures to a class • Include data validation in a class • Create a default constructor • Include methods in a class Clearly Visual Basic: Programming with Visual Basic 2008
That’s a Real Classy Object • Object-oriented programming language • Allows the programmer to use objects to accomplish a program’s goal • Class • Pattern or blueprint that the computer follows when creating an object • Object • Instantiated (created) from a class • Referred to as an instance of a class Clearly Visual Basic: Programming with Visual Basic 2008
That’s a Real Classy Object (continued) • Attributes • Characteristics that describe an object • Behaviors • Include methods and events • Methods • Operations (actions) that the object is capable of performing • Events • Actions to which an object can respond Clearly Visual Basic: Programming with Visual Basic 2008 4
That’s a Real Classy Object (continued) • A class • Encapsulates all of the attributes and behaviors of the object it instantiates • Class statement • Used to define a class • Pascal case • Capitalize the first letter in the name and the first letter in any subsequent words in the name Clearly Visual Basic: Programming with Visual Basic 2008 5
Revisiting the Willow Pools Application • Willow Pools application • Calculates and displays both the volume of a rectangular pool and the amount of water required to fill the pool • Volume • Calculated by multiplying the pool’s length by its width and then multiplying the result by the pool’s depth Clearly Visual Basic: Programming with Visual Basic 2008 7
Who Owns that Property? • When an application instantiates an object: • Only the Public members of the object’s class are made available to the application • Property procedure • Used to create a Public property • Figure 27-4 • Code contained in the Get block allows application to retrieve the contents of the Private variable • Code in the Set block allows an application to assign a value to the Private variable Clearly Visual Basic: Programming with Visual Basic 2008 9
Who Owns that Property? (continued) • Get statement • Begins with the keyword Get and ends with the keywords End Get • Set statement • Begins with the keyword Set and ends with the keywords End Set Clearly Visual Basic: Programming with Visual Basic 2008 12
Constructive Behavior is the Key to Success • Constructor • Class method whose instructions are processed by the computer when an object is instantiated • Responsible for creating the object and initializing the class’s Private variables • Default constructor • Constructor that has no parameters • A class can have only one Clearly Visual Basic: Programming with Visual Basic 2008 13
Methods Other than Constructors • Methods included in a class • Can be either Sub procedures or Function procedures • Method name • Should be entered using Pascal case • First word should be a verb, and any subsequent words should be nouns and adjectives Clearly Visual Basic: Programming with Visual Basic 2008 15
Using the Pattern to Create an Object • After defining a class: • It can then be used to instantiate one or more objects • Figure 27-9 • Shows two versions of the basic syntax to instantiate an object Clearly Visual Basic: Programming with Visual Basic 2008 17
Pool Supplies Application • Pool Supplies • Sells a water clarifier designed to combat a common problem in swimming pools: cloudy water • Recommends one ounce of SoClear clarifier per 5000 gallons of water • Manager wants an application that calculates the: • Number of gallons of water contained in a pool • Required number of ounces of clarifier Clearly Visual Basic: Programming with Visual Basic 2008 20
Summary • Objects used in an object-oriented program • Instantiated (created) from classes • Class statement • Used to define a class • When naming the Private variables in a class: • Begin the name with the underscore character • When an object is instantiated in an application: • Public members of the class are exposed to the application Clearly Visual Basic: Programming with Visual Basic 2008
Summary (continued) • Property procedure • Used to create a Public property • Get block allows an application to access the contents of the Private variable • Class • Can have one or more constructors • Names of methods in a class • Should be entered using Pascal case Clearly Visual Basic: Programming with Visual Basic 2008 22