1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Module 7: Essentials of Object-Oriented Programming pdf

68 481 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 68
Dung lượng 1,03 MB

Nội dung

Module 7: Essentials of Object-Oriented Programming Contents Overview Classes and Objects Using Encapsulation 10 C# and Object Orientation 21 Lab 7.1: Creating and Using Classes 39 Defining Object-Oriented Systems 52 Review 61 Information in this document, including URL and other Internet Web site references, is subject to change without notice Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, e-mail address, logo, person, places or events is intended or should be inferred Complying with all applicable copyright laws is the responsibility of the user Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property  2001−2002 Microsoft Corporation All rights reserved Microsoft, MS-DOS, Windows, Windows NT, ActiveX, BizTalk, IntelliSense, JScript, MSDN, PowerPoint, SQL Server, Visual Basic, Visual C++, Visual C#, Visual J#, Visual Studio, and Win32 are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A and/or other countries The names of actual companies and products mentioned herein may be the trademarks of their respective owners Module 7: Essentials of Object-Oriented Programming iii Instructor Notes Presentation: 60 Minutes Lab: 45 Minutes This module provides students with the basic theory, concepts, and terminology of object-oriented programming It also provides a minimal amount of C# syntax, specifically for implementing encapsulation After completing this module, students will be able to:  Define the terms object and class in the context of object-oriented programming  Define the three core aspects of an object: identity, state, and behavior  Describe abstraction and how it helps you to create reusable classes that are easy to maintain  Use encapsulation to combine methods and data in a single class and enforce abstraction  Explain the concepts of inheritance and polymorphism  Create and use classes in C# Materials and Preparation This section provides the materials and preparation tasks that you need to teach this module Required Materials To teach this module, you need the following materials:  Microsoft® PowerPoint® file 2124C_07.ppt  Module 7, “Essentials of Object-Oriented Programming”  Lab 7, Creating and Using Classes Preparation Tasks To prepare for this module, you should:  Read all of the materials for this module  Complete the lab iv Module 7: Essentials of Object-Oriented Programming Module Strategy Use the following strategy to present this module:  Classes and Objects In teaching this course, sometimes you will use a particular word to mean a class (all examples), and sometimes you will use the same word to mean an object (a specific example) Make sure that students understand the difference Correct them if they misuse the terminology Object identity can be difficult to teach A useful analogy involves a scenario of a being in a restaurant and not being able to read the menu (Perhaps it is in French.) However, you see another diner at a nearby table eating a dish that looks and smells great You decide that you want the same dish You capture the attention of a passing waiter and say, “I’ll have what they’re having.” There are two outcomes In the “with-identity” outcome, the waiter goes to the other table, takes the diner’s plate away, and gives it to you! In the “without-identity” outcome, the waiter goes to the kitchen and tells the chef the name of the dish, and the chef then cooks another copy of that dish by using another set of the same ingredients The concept of identity can be summed up in a question: Do you need the actual object, or is a copy good enough?  Using Encapsulation This section provides the motivation for combining data and methods It provides the answer to the question, “Why encapsulate?” This section also provides a small amount of C# syntax for creating encapsulated classes A top-level class cannot be declared private It is best not to spend too much time on top-level visibility: this topic is covered in a later module Nested classes are not covered in the lab exercises  C# and Object Orientation This section explains the terminology and concepts of object-oriented programming  Defining Object-Oriented Systems The word system in this section is used to emphasize the higher-level structure that inheritance provides Class hierarchy design is essentially framework design and requires thought and experience Do not spend too much time on detailed explanations Remember that the primary aim of this module is to teach the theory In keeping with this aim, the slides use Unified Modeling Language (UML) notation Be sure to read the notes in the Inheritance topic and explain why the man, woman, and child relationship is not a good example of object-oriented inheritance Inheritance is misunderstood and misused in object-oriented programming, so it is a good idea to dispel a few misunderstandings right away Do not spend too much time on single and multiple inheritance at this time Remember, you have not yet covered interfaces Module 7: Essentials of Object-Oriented Programming v Polymorphism is a long word with a simple meaning It is something that all human beings instinctively It is a natural part of classification You might want to use a simple example Ask a student what time it is Ask another student the same question, and then ask another student You will get a variety of different implementations and responses Some students will look at their watches Some will look at a clock Some will be clever and just repeat the previous answer (caching!) The section does not over-emphasize that derived classes can add their own methods Most textbooks over-emphasize this The major benefit of class hierarchies is polymorphism In the Early and Late Binding topic, not spend too much time explaining how late binding works Module 7: Essentials of Object-Oriented Programming Overview Topic Objective To provide an overview of the module topics and objectives Lead-in In this module, you will learn more details about classes and objects  Classes and Objects  Using Encapsulation  C# and Object Orientation  Defining Object-Oriented Systems *****************************ILLEGAL FOR NON-TRAINER USE****************************** C# is an object-oriented programming language In this lesson, you will learn the terminology and concepts required to create and use classes in C# After completing this module, you will be able to:  Define the terms object and class in the context of object-oriented programming  Define the three core aspects of an object: identity, state, and behavior  Describe abstraction and how it helps you to create reusable classes that are easy to maintain  Use encapsulation to combine methods and data in a single class and enforce abstraction  Explain the concepts of inheritance and polymorphism  Create and use classes in C# Module 7: Essentials of Object-Oriented Programming  Classes and Objects Topic Objective To provide an overview of the topics covered in this section Lead-in You will often hear the terms class and object In this lesson, you will learn exactly what these terms mean  What Is a Class?  What Is an Object?  Comparing Classes to Structs  Abstraction *****************************ILLEGAL FOR NON-TRAINER USE****************************** The whole structure of C# is based on the object-oriented programming model To make the most effective use of C# as a language, you need to understand the nature of object-oriented programming After completing this lesson, you will be able to:  Define the terms object and class in the context of object-oriented programming  Apply the concept of abstraction Module 7: Essentials of Object-Oriented Programming What Is a Class? Topic Objective To explain the concept of a class CAR? CAR?  Lead-in The C# language is primarily concerned with defining classes and specifying their behavior  For the philosopher…  An artifact of human classification!  Classify based on common behavior or attributes  Agree on descriptions and names of useful classes  Create vocabulary; we communicate; we think! For the object-oriented programmer…  A named syntactic construct that describes common behavior and attributes  A data structure that includes both data and functions *****************************ILLEGAL FOR NON-TRAINER USE****************************** The root word of classification is class Forming classes is an act of classification, and it is something that all human beings (not just programmers) For example, all cars share common behavior (they can be steered, stopped, and so on) and common attributes (they have four wheels, an engine, and so on) You use the word car to refer to all of these common behaviors and properties Imagine what it would be like if you were not able to classify common behaviors and properties into named concepts! Instead of saying car, you would have to say all the things that car means Sentences would be long and cumbersome In fact, communication would probably not be possible at all As long as everyone agrees what a word means, that is, as long as we all speak the same language, communication works well—we can express complex but precise ideas in a compact form We then use these named concepts to form higher-level concepts and to increase the expressive power of communication All programming languages can describe common data and common functions This ability to describe common features helps to avoid duplication A key motto in programming is “Don’t repeat yourself.” Duplicate code is troublesome because it is more difficult to maintain Code that does not repeat itself is easier to maintain, partly because there is just less of it! Object-oriented languages take this concept to the next level by allowing descriptions of classes (sets of objects) that share structure and behavior If done properly, this paradigm works extremely well and fits naturally into the way people think and communicate Classes are not restricted to classifying concrete objects (such as cars); they can also be used to classify abstract concepts (such as time) However, when you are classifying abstract concepts, the boundaries are less clear, and good design becomes more important The only real requirement for a class is that it helps people communicate Module 7: Essentials of Object-Oriented Programming What Is an Object? Topic Objective To define the term object Lead-in It is a common mistake to use the terms class and object interchangeably They do, in fact, refer to different concepts  An object is an instance of a class  Objects exhibit:  Identity: Objects are distinguishable from one another  Behavior: Objects can perform tasks  State: Objects store information *****************************ILLEGAL FOR NON-TRAINER USE****************************** Delivery Tip You can use the car analogy throughout the explanation of identity, behavior, and state For example, you can ask students if this statement refers to cars as objects or as a class: "The carpool lane, which is the rightmost lane, is for cars with more than three passengers." This statement uses "car" as an object—an instance of a class Objects can be anonymous, but they are still objects The fact that a car has three people in it does not make it a different sort of object from a car that has two people in it, or four people In fact, the number of people in the car is an example of the car’s state Explain that state refers to the values of the internal attributes of an object that might vary over time, such as the number of passengers Contrast this to values that are probably fixed and that not vary after the car is created, such as the number of doors The word car means different things in different contexts Sometimes we use the word car to refer to the general concept of a car: we speak of car as a class, meaning the set of all cars, and not have a specific car in mind At other times we use the word car to mean a specific car Programmers use the term object or instance to refer to a specific car It is important to understand this difference The three characteristics of identity, behavior, and state form a useful way to think about and understand objects Identity Identity is the characteristic that distinguishes one object from all other objects of the same class For example, imagine that two neighbors own a car of exactly the same make, model, and color Despite the obvious similarities, the registration numbers are guaranteed to be unique and are an outward reflection that cars exhibit identity The law determines that it is necessary to distinguish one car object from another (How would car insurance work without car identity?) ... currently built 12 Module 7: Essentials of Object-Oriented Programming Object-Oriented Programming Object-oriented programming arose to alleviate these problems Object-oriented programming, if understood... context of object-oriented programming  Apply the concept of abstraction Module 7: Essentials of Object-Oriented Programming What Is a Class? Topic Objective To explain the concept of a class... Tasks To prepare for this module, you should:  Read all of the materials for this module  Complete the lab iv Module 7: Essentials of Object-Oriented Programming Module Strategy Use the following

Ngày đăng: 10/12/2013, 16:16

TỪ KHÓA LIÊN QUAN