lecture 9 - introduction to objects (vb 2008)

Upload: curlicue

Post on 30-May-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 Lecture 9 - Introduction to Objects (VB 2008)

    1/15

    Lecture 9: Introduction to Objects

  • 8/14/2019 Lecture 9 - Introduction to Objects (VB 2008)

    2/15

    Outline Introduction:

    Procedural Programming (Review / Overview) Ideas and Problems

    Object-Oriented Programming (Overview / Comparison) Members and Methods Properties Visibility

    Defining Objects: Classes

    Console Applications When to abandon a GUI-based Approach.

    Example: The J_Train Object Creating the J_Train Class

  • 8/14/2019 Lecture 9 - Introduction to Objects (VB 2008)

    3/15

    Procedural Programming

    Procedural Programming Idea: Program is a simple automaton that executes an algorithm:

    A step-by-step method for solving instances of a well-defined problem.

    Essential Tools: Linear Structures (Commands)

    Decision Structures (IfThen Blocks, Select Case Blocks)

    Iteration (Loops) and Recursion (not yet covered)

    Compartmentalized Procedures (Functions, Subroutines) Data Structures (Arrays, Structures)

    Advantage: Simple; easy to implement for smaller systems.

    Problems:

    Weak Coherence: Logical program sub-units not fully modular Difficult to fully Re-use larger program elements, as a group.

    Tight Coupling: Program behaviors and variables not localized Code changes can cause subtle, non-local effects to program behavior.

    Changes can have strong non-local effects (effects the whole code).

    Hard to Maintain (update, etc)

  • 8/14/2019 Lecture 9 - Introduction to Objects (VB 2008)

    4/15

    Object Oriented Programming

    Procedural Programming

    Problems: Complexity, Poor Maintenance, Poor Reusability Solution: Programming using Reusable Black Box Modules (Objects)

    Object-Oriented Programming Strong Coherence: Objects should be similar to real-world things!

    Real-world things appear as logically-related units, with: Characteristics: Expected appearance

    Behaviors: Expected input, output, and functionality

    Idea: Lets compartmentalize! Develop large projects in logical units Proper behavior of each unit can be tested separately.

    Properly working units can then all be added (like snap-together blocks)

    These logical units can easily be re-used, in later projects!

    Weak Coupling: Objects should appear as Black Boxes With an expected interface to interact with clients (in the code!).

    But with other details hidden from clients.

    Changes to the inner working of each object can be done independently Easy maintenance!

  • 8/14/2019 Lecture 9 - Introduction to Objects (VB 2008)

    5/15

    Objects vs. Structures

    With Objects, we may combine Public members, methods, and properties

    Note that members may be of different data types Private members and methods.

    This is a bit similar to Structures, but Objects are a bit different: First of all, they use a different method ofinstantiation

    i.e., creation of an instance. Objects are reference types:

    To instantiate (make) a new instance , the New keyword is required. Actually, via a 2-step process of definition + instantiation and assignment.

    As we have seen, the Form1 object is made for us, automatically.

    Structures are value types: To make a new Structure instance, New is not required.

    Ex: We can use the Dimkeyword to declare an instance... This is just like primitive data types (Integer, Double, etc).

    Structures and Classes are built in a similar manner Using a separate Class File (*.vb), using the Class Keyword.

    And making a new instance with the New keyword.

  • 8/14/2019 Lecture 9 - Introduction to Objects (VB 2008)

    6/15

    Our First Object: The J-Train

    Lets demonstrate by creating a simple Object

    The JTrain, a simple Train Model

    First, lets think about possible JTrain characteristics/behaviors Possible Characteristics (Members or Properties):

    Color (Perhaps an Enumeration)

    Length = Number of Train Cars (Integer) Speed = km/hr (Single; positive values mean forward)

    MainEngine = current identity of the Forward Engine (Integer, etc).

    TrainState = stopped, waiting, backing, out_of_service (new Enumeration)

    Possible Behaviors (Methods):

    Accelerate/Decelerate (Subroutine) Door Open/Close (Subroutine)

    Add/Detach a Train Car (Function; returns T/F = successful or unsuccessful)

    Add/Detach a Train Engine (Function; returns successful or unsuccessful)

  • 8/14/2019 Lecture 9 - Introduction to Objects (VB 2008)

    7/15

  • 8/14/2019 Lecture 9 - Introduction to Objects (VB 2008)

    8/15

    J-Train Ex.: Creating a Console App.

  • 8/14/2019 Lecture 9 - Introduction to Objects (VB 2008)

    9/15

    J-Train Ex.: Creating the JTrain Class

  • 8/14/2019 Lecture 9 - Introduction to Objects (VB 2008)

    10/15

    J-Train Ex.: Coding the Interface

    Now, we code the basic User Interface (UI)

  • 8/14/2019 Lecture 9 - Introduction to Objects (VB 2008)

    11/15

    J-Train Ex.: Test Function

  • 8/14/2019 Lecture 9 - Introduction to Objects (VB 2008)

    12/15

    As we noted, Classes are defined to have Members Class variables that express structure characteristics

    We may define and access Public Members directly Via the dot ( . ) operator, on a one-to-one basiswith no abstraction.

    We can access Private Members using Public Functions/Subroutines As we did for the JTrains State (using the GetStatus() Function)

    However, characteristics may also be handled via Properties. These can be used with Members to ABSTRACT-AWAY details.

    Or to create composite characteristics and/or secondary effects... Properties generally come with Get and Set methods.

    PublicPropertyprop_Name()AsGet_DataType Get method to return the value of Property

    Statements

    End Get Set method to set the value of Property

    Statements

    End Set

    End Property

    TheGet and Set methods are both accessed via the DOT OPERATOR.

    Properties (Review from VB.NET 1)

  • 8/14/2019 Lecture 9 - Introduction to Objects (VB 2008)

    13/15

    J-Train Ex.: JTrain Speed

  • 8/14/2019 Lecture 9 - Introduction to Objects (VB 2008)

    14/15

    J-Train Ex.: Expand and Test

  • 8/14/2019 Lecture 9 - Introduction to Objects (VB 2008)

    15/15

    Conclusion / Forward In this lecture, we introduced Objects:

    And discussed Object-Oriented Programming Members, Methods, Properties, etc

    Defining Objects: Classes

    We also introduced Console Applications: And discussed when to abandon a GUI-based Approach.

    In the context of an Example: The J_Train Class

    Next Lecture, we continue our discussion of Objects: Defining Constructors, which allow:

    Convenient encapsulation of default characteristics. Clients to create new Object instances with specified characteristics.

    A more advanced topic: Inheritance We will learn to create derived Classes...

    Which inherit, and expand on the characteristics of a Base Class

    Example: The JFreightTrain Class Which inherits from JTrain