Lecture 9 - Introduction To Objects (vb 2008)

  • Uploaded by: curlicue
  • 0
  • 0
  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Lecture 9 - Introduction To Objects (vb 2008) as PDF for free.

More details

  • Words: 1,110
  • Pages: 15
Lecture 9: Introduction to Objects

Outline 

Introduction: 

Procedural Programming (Review / Overview) 



Object-Oriented Programming (Overview / Comparison)   





Members and Methods Properties Visibility

Defining Objects: Classes

Console Applications 



Ideas and Problems

When to abandon a GUI-based Approach.

Example: The J_Train Object 

Creating the J_Train Class

Procedural Programming 

Procedural Programming 

Idea: Program is a simple automaton that executes an algorithm: 



Essential Tools:     





A step-by-step method for solving instances of a well-defined problem. Linear Structures (Commands) Decision Structures (If…Then 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…)

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:  



Idea: Let’s compartmentalize! Develop large projects in logical units  





Characteristics: Expected appearance Behaviors: Expected input, output, and functionality 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!

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 of instantiation… 



i.e., creation of an instance.

Objects are reference types: 

To instantiate (make) a new instance , the New keyword is required. 





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. 





Actually, via a 2-step process of definition + instantiation and assignment.

Ex: We can use the Dim keyword 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.

Our First Object: The J-Train 

Let’s demonstrate by creating a simple Object… 



The JTrain, a simple Train Model

First, let’s 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)

Console vs. GUI-based Applications 

Thus far, we have created GUI-based Applications… 



Using WinForms (the Form1 Object).

However, a GUI is not always the most appropriate interface: 

If a GUI is not necessary… 

A GUI can be computationally expensive. 



What if we are developing an Object for a larger project ? 





e.g., Embedded programs Also: applications that will be used via Remote Access. 

GUI would be useless ( not be seen by the Remote User ).

In such cases, you may instead choose a Console Application… 

Which runs directly from a Command Line Interface. 



i.e., plain-text display and typing.

In this case, we code using a Module instead of a Form Object. 

This module will contain a Main() subroutine which runs automatically… 



A separate GUI may not be appropriate, for testing or use.

If the Target Platform is not capable of displaying a GUI… 



Can be a problem, if the application requires maximum speed .

Thus, we will add our executable code to the Main subroutine.

To demonstrate, let’s choose a Console Application… 

For our first Object Example: The JTrain!

J-Train Ex.: Creating a Console App.

J-Train Ex.: Creating the JTrain Class

J-Train Ex.: Coding the Interface •

Now, we code the basic User Interface (UI)…

J-Train Ex.: Test Function

Properties (Review from VB.NET 1) 

As we noted, Classes are defined to have ‘Members’ … 

Class variables that express structure characteristics 

We may define and access Public Members directly… 



We can access Private Members using Public Functions/Subroutines… 



Via the dot ( . ) operator, on a one-to-one basis…with no abstraction. As we did for the JTrain’s 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. Public Property prop_Name() As Get_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



The Get and Set methods are both accessed via the DOT OPERATOR.

J-Train Ex.: JTrain Speed

J-Train Ex.: Expand and Test

Conclusion / Forward 

In this lecture, we introduced Objects: 

And discussed Object-Oriented Programming 





Defining Objects: Classes

We also introduced Console Applications:  



Members, Methods, Properties, etc

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

Related Documents


More Documents from "curlicue"