programming with microsoft visual basic 2012 chapter 11: classes and objects

Download Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects

If you can't read please download the document

Upload: dora-carson

Post on 13-Dec-2015

217 views

Category:

Documents


2 download

TRANSCRIPT

  • Slide 1

Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects Slide 2 Previewing the Woods Manufacturing Application Programming with Microsoft Visual Basic 20122 Figure 11-1 Interface showing Charikas gross pay and informationFigure 11-2 Interface showing Chriss gross pay and information Calculates a salary for either an hourly worker or a salaried worker Slide 3 Lesson A Objectives After studying Lesson A, you should be able to: Explain the terminology used in object-oriented programming Create a class Instantiate an object Add Property procedures to a class Include data validation in a class Create a default constructor Create a parameterized constructor Include methods other than constructors in a class Programming with Microsoft Visual Basic 20123 Slide 4 Object-oriented programming language Uses objects to accomplish a programs goal Class A pattern or blueprint for an object Instance An object created from a class Instantiated The process of creating an object from a class Attributes Characteristics that describe an object Programming with Microsoft Visual Basic 20124 Object-Oriented Programming Terminology Slide 5 Behaviors Methods and events that define how the object will act or react Methods Operations (actions) that an object is capable of performing Events Actions to which an object can respond Encapsulates To enclose in a capsule A class encapsulates all attributes and behaviors of an object it instantiates Programming with Microsoft Visual Basic 20125 Object-Oriented Programming Terminology (cont.) Slide 6 Programming with Microsoft Visual Basic 20126 Creating a Class Two types of classes used in VB applications: Built-in classes, such as the TextBox class Programmer-defined classes Class statement Used to define a class Defines attributes and behaviors of objects created from the class After a class has been defined, it can be used to instantiate objects Slide 7 Programming with Microsoft Visual Basic 20127 Figure 11-4 Class statement entered in the TimeCard.vb class file Figure 11-3 Syntax of the Class statement Creating a Class (cont.) Slide 8 Programming with Microsoft Visual Basic 20128 Creating a Class (cont.) Figure 11-5 Syntax and examples of instantiating an object Slide 9 Norbert Pool & Spa Depot application from Chapter 10 Input: Length, width, and depth of a pool Calculates the volume of water required This program was coded with a structure in Chapter 10 The structure will be replaced with a class Programming with Microsoft Visual Basic 20129 Example 1A Class that Contains Public Variables Only Slide 10 Programming with Microsoft Visual Basic 201210 Figure 11-6 Code for the Norbert Pool & Spa Depot application (with a structure) Example 1A Class that Contains Public Variables Only (cont.) Slide 11 Programming with Microsoft Visual Basic 201211 Figure 11-7 Comments and Option statements entered in the class file Figure 11-8 Public variables included in the IntelliSense list Example 1A Class that Contains Public Variables Only (cont.) Slide 12 Programming with Microsoft Visual Basic 201212 Figure 11-9 Class statement, GetGallons function, and btnCalc_Click procedure Example 1A Class that Contains Public Variables Only (cont.) Slide 13 Programming with Microsoft Visual Basic 201213 Figure 11-10 Interface showing the number of gallons Example 1A Class that Contains Public Variables Only (cont.) Slide 14 Disadvantages of using Public variables in a class: A class cannot control the values assigned to its Public variables This violates the concept of encapsulation, in which class behaviors control a classs data Programming with Microsoft Visual Basic 201214 Example 2A Class that Contains Private Variables, Public Properties, and Methods Slide 15 Private Variables and Property Procedures Private class variables Can only be used within the class Not visible to the rest of the application Public property Used to refer to (expose) the Private variable for use by other parts of the application Property procedure Creates a Public property Programming with Microsoft Visual Basic 201215 Example 2A Class that Contains Private Variables, Public Properties, and Methods (cont.) Slide 16 Private Variables and Property Procedures (cont.) ReadOnly keyword Indicates an application can read the propertys value Cannot set the value WriteOnly keyword Indicates an application can set the propertys value But it cannot retrieve the value Property procedures contain blocks of code: Get block: Retrieves the contents of the Private variable Set block: Used to assign a value to the Private variable Blocks may be used individually or together Programming with Microsoft Visual Basic 201216 Example 2A Class that Contains Private Variables, Public Properties, and Methods (cont.) Slide 17 Programming with Microsoft Visual Basic 201217 Figure 11-13 Syntax and examples of a Property procedure Example 2A Class that Contains Private Variables, Public Properties, and Methods (cont.) Private Variables and Property Procedures (cont.) Slide 18 Programming with Microsoft Visual Basic 201218 Figure 11-14 Length Property procedure entered in the class Example 2A Class that Contains Private Variables, Public Properties, and Methods (cont.) Slide 19 Constructors Constructor A class method whose purpose is to initialize the classs Private variables Processed each time an object is created Must be coded as a Sub procedure named New A class can have more than one constructor The names are the same, but the parameterLists must differ Default constructor A constructor without parameters Programming with Microsoft Visual Basic 201219 Example 2A Class that Contains Private Variables, Public Properties, and Methods (cont.) Slide 20 Constructors (cont.) Parameterized constructor A constructor containing one or more parameters Methods signature A method name combined with an optional parameterList Programming with Microsoft Visual Basic 201220 Example 2A Class that Contains Private Variables, Public Properties, and Methods (cont.) Slide 21 Programming with Microsoft Visual Basic 201221 Figure 11-16 Statements that invoke the constructors shown in Figure 11-15 Figure 11-15 Syntax and examples of a constructor Example 2A Class that Contains Private Variables, Public Properties, and Methods (cont.) Constructors (cont.) Slide 22 Programming with Microsoft Visual Basic 201222 Example 2A Class that Contains Private Variables, Public Properties, and Methods (cont.) Methods Other than Constructors May be either Sub or Function procedures Functions return a value; Sub procedures do not Rules for naming methods: The name should be entered using Pascal case The first word in a name should be a verb Subsequent words should be nouns and adjectives Slide 23 Programming with Microsoft Visual Basic 201223 Figure 11-17 Syntax and examples of a method that is not a constructor Example 2A Class that Contains Private Variables, Public Properties, and Methods (cont.) Methods Other than Constructors (cont.) Slide 24 Programming with Microsoft Visual Basic 201224 Figure 11-18 Pseudocode for the Calculate buttons Click event procedure Coding the Carpets Galore Application Figure 11-19 TryParse methods entered in the procedure Example 2A Class that Contains Private Variables, Public Properties, and Methods (cont.) Slide 25 Programming with Microsoft Visual Basic 201225 Figure 11-20 Rectangle class definition and btnCalc_Click procedure (continues) Example 2A Class that Contains Private Variables, Public Properties, and Methods (cont.) Coding the Carpets Galore Application (cont.) Slide 26 Programming with Microsoft Visual Basic 201226 Figure 11-20 Rectangle class definition and btnCalc_Click procedure Example 2A Class that Contains Private Variables, Public Properties, and Methods (cont.) Coding the Carpets Galore Application (cont.) Slide 27 Programming with Microsoft Visual Basic 201227 Figure 11-21 Interface showing the square yards and cost Example 2A Class that Contains Private Variables, Public Properties, and Methods (cont.) Coding the Carpets Galore Application (cont.) Slide 28 A parameterized constructor is simply a constructor that has parameters When a Rectangle object is created, a parameterized constructor allows an application to specify the objects initial values Programming with Microsoft Visual Basic 201228 Example 3A Class that Contains a Parameterized Constructor Figure 11-22 Default and parameterized constructors Slide 29 Programming with Microsoft Visual Basic 201229 Figure 11-23 Modified Rectangle class definition and btnCalc_Click procedure (continues) Example 3A Class that Contains a Parameterized Constructor (cont.) Slide 30 Programming with Microsoft Visual Basic 201230 Figure 11-23 Modified Rectangle class definition and btnCalc_Click procedure Figure 11-24 Square yards and cost shown in the interface Example 3A Class that Contains a Parameterized Constructor (cont.) Slide 31 Rectangle class from Examples 2 and 3: Reused here to represent a square pizza A square is a rectangle with four equal sides Using an object for more than one purpose saves programming time and money An advantage of object-oriented programming Programming with Microsoft Visual Basic 201231 Example 4Reusing a Class Figure 11-25 Interface for the Petes Pizzeria application Slide 32 Programming with Microsoft Visual Basic 201232 Figure 11-26 Pseudocode for the Calculate buttons Click event procedure Example 4Reusing a Class (cont.) Slide 33 Programming with Microsoft Visual Basic 201233 Figure 11-27 btnCalc_Click procedure Example 4Reusing a Class (cont.) Figure 11-28 Number of pizza slices shown in the interface Slide 34 Lesson A Summary The Class statement is used to define a class Defined classes are added to a PROJECT with a.vb extension Objects are instantiated from a defined class The Get block allows an application to retrieve the contents of the Private variable associated with the Property procedure The Set block allows an application to assign a value to the Private variable associated with the Property procedure Programming with Microsoft Visual Basic 201234 Slide 35 Lesson A Summary (cont.) A constructor initializes the variables of a class The constructor method must be named New The default constructor has no parameters A class may have many parameterized constructors A class can have methods other than constructors Programming with Microsoft Visual Basic 201235 Slide 36 Lesson B Objectives After studying Lesson B, you should be able to: Include a ReadOnly property in a class Create an auto-implemented property Overload a method in a class Programming with Microsoft Visual Basic 201236 Slide 37 Programming with Microsoft Visual Basic 201237 Example 5A Class that Contains a ReadOnly Property ReadOnly keyword Indicates that the propertys value can be retrieved (read) but not set (written) The ReadOnly property gets its value from the class instead of from the application Grade Calculator application Returns a letter grade based on a numeric grade Figure 11-33 Interface for the Grade Calculator application Slide 38 Programming with Microsoft Visual Basic 201238 Figure 11-34 ReadOnly property message Figure 11-35 CourseGrade class definition and btnDisplay_Click procedure (continues) Example 5A Class that Contains a ReadOnly Property (cont.) Slide 39 Programming with Microsoft Visual Basic 201239 Figure 11-35 CourseGrade class definition and btnDisplay_Click procedure Figure 11-36 Grade shown in the interface (continued) Example 5A Class that Contains a ReadOnly Property (cont.) Slide 40 Programming with Microsoft Visual Basic 201240 Example 6A Class that Contains Auto-Implemented Properties Auto-implemented properties feature Enables you to specify the property of a class in one line of code Visual Basic automatically creates a hidden Private variable that it associates with the property It also automatically creates hidden Get and Set blocks It provides a shorter syntax to use when creating a class You dont need to create the Private variable associated with a property, nor do you need to enter the propertys Get and Set blocks of code Slide 41 Programming with Microsoft Visual Basic 201241 Figure 11-37 Syntax and examples of creating an auto-implemented property Example 6A Class that Contains Auto-Implemented Properties (cont.) Slide 42 Programming with Microsoft Visual Basic 201242 Figure 11-38 Modified CourseGrade class definition Example 6A Class that Contains Auto-Implemented Properties (cont.) Slide 43 Overloaded methods Methods with the same name but different parameters Programming with Microsoft Visual Basic 201243 Example 7A Class that Contains Overloaded Methods Figure 11-39 Attributes and behaviors of an Employee object Slide 44 Programming with Microsoft Visual Basic 201244 Figure 11-40 Employee class definition Example 7A Class that Contains Overloaded Methods (cont.) Slide 45 Programming with Microsoft Visual Basic 201245 Figure 11-42 Interface for the Woods Manufacturing application Example 7A Class that Contains Overloaded Methods (cont.) Slide 46 Programming with Microsoft Visual Basic 201246 Figure 11-43 Pseudocode for the Calculate buttons Click event procedure Example 7A Class that Contains Overloaded Methods (cont.) Slide 47 Programming with Microsoft Visual Basic 201247 Figure 11-45 btnCalc_Click procedure Example 7A Class that Contains Overloaded Methods (cont.) Slide 48 Programming with Microsoft Visual Basic 201248 Figure 11-46 Jakes gross pay and information shown in the interface Figure 11-47 Sherris gross pay and information shown in the interface Example 7A Class that Contains Overloaded Methods (cont.) Slide 49 Programming with Microsoft Visual Basic 201249 Lesson B Summary Use the ReadOnly keyword to create a property whose value an application can only retrieve To specify the property of a class in one line: Create an auto-implemented property using the syntax Public Property propertyName As dataType To include a parameterized method in a class: Enter the parameters between the parentheses that follow the methods name To create two or more methods that perform the same task but require different parameters: Overload the methods by giving them the same name but different parameterLists Slide 50 Lesson C Objectives After studying Lesson C, you should be able to: Create a derived class Refer to the base class using the MyBase keyword Override a method in the base class Programming with Microsoft Visual Basic 201250 Slide 51 Programming with Microsoft Visual Basic 201251 Example 8Using a Base Class and a Derived Class Inheritance Using one class to create another Base class The original class providing behaviors and attributes Derived class The new class that inherits attributes and behaviors from the base class Inherits clause Enables a derived class to inherit from a base class Included in the derived classs Class statement Slide 52 Programming with Microsoft Visual Basic 201252 Example 8Using a Base Class and a Derived Class (cont.) Overriding Allows a derived class to replace a method inherited from its base class The base class method header must include the Overridable keyword The derived class method header must include the Overrides keyword MyBase keyword Used to refer to the base class MyBase.New ( [parameterList] ) Used in a derived class to call its base classs constructor Slide 53 Programming with Microsoft Visual Basic 201253 Figure 11-51 Contents of the Shapes.vb file Example 8Using a Base Class and a Derived Class (cont.) Slide 54 Programming with Microsoft Visual Basic 201254 Figure 11-52 Modified Square and Cube class definitions Example 8Using a Base Class and a Derived Class (cont.) Slide 55 Programming with Microsoft Visual Basic 201255 Figure 11-54 btnSquare_Click and btnCube_Click procedures Figure 11-53 Interface showing the squares area Example 8Using a Base Class and a Derived Class (cont.) Slide 56 Programming with Microsoft Visual Basic 201256 Lesson C Summary To allow a derived class to inherit the attributes and behaviors of a base class: Enter the Inherits clause immediately below the Public Class clause in the derived class The Inherits clause is the keyword Inherits followed by the name of the base class Use the MyBase keyword to refer to the base class To indicate that a method in the base class can be overridden (replaced) in the derived class: Use the Overridable keyword in the methods header in the base class Slide 57 Programming with Microsoft Visual Basic 201257 Lesson C Summary (cont.) To indicate that a method in the derived class overrides (replaces) a method in the base class: Use the Overrides keyword in the methods header in the derived class