Chương 5: Understanding Object-Oriented Programming Polymorphism doc

47 167 0
Chương 5: Understanding Object-Oriented Programming Polymorphism doc

Đ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

Chapter Understanding Object-OrientedProgramming: Polymorphism Hoang Anh Viet VietHA@it-hut.edu.vn Hanoi University of Technology Objectives In this chapter you will learn:        The concept of polymorphism and how it enables you to "program in the general." To use overridden methods to effect polymorphism To distinguish between abstract and concrete classes To declare abstract methods to create abstract classes How polymorphism makes systems extensible and maintainable To determine an object's type at execution time To create sealed methods and classes Microsoft Roadmap        5.1 Introduction 5.2 Polymorphism Examples 5.3 Demonstrating Polymorphic Behavior 5.4 Abstract Classes and Methods 5.5 Case Study: Payroll System Using Polymorphism 5.6 sealed Classes and Methods 5.7 Operator Overloading Microsoft 5.1 Introduction  The third pillar of OOP is polymorphism   Let you treat related objects in a similar way Polymorphism allows programmers to write:   Microsoft Programs that handle a wide variety of related classes in a generic manner Systems that are easily extensible Roadmap        5.1 Introduction 5.2 Polymorphism Examples 5.3 Demonstrating Polymorphic Behavior 5.4 Abstract Classes and Methods 5.5 Case Study: Payroll System Using Polymorphism 5.6 sealed Classes and Methods 5.7 Operator Overloading Microsoft 5.2 Polymorphism Examples  Quadrilateral base-class       Microsoft Rectangle derived-class Square derived-class Parallelogram derived-class Trapezoid derived-class Method perimeter might need to be invoked on all classes With a Quadrilateral reference, C# polymorphically chooses the correct overriding method in the derived-class from which the object is instantiated 5.2 Polymorphism Examples  SpaceObject base-class – contains method DrawYourself      Martian derived-class (implements DrawYourself) Venutian derived-class (implements DrawYourself) Plutonian derived-class (implements DrawYourself) SpaceShip derived-class (implements DrawYourself) A screen-manager program may contain a SpaceObject array of references to objects of various classes that derive from SpaceObject Microsoft 5.2 Polymorphism Examples   To refresh the screen, the screen-manager calls DrawYourself on each object in the array The program polymorphically calls the appropriate version of DrawYourself on each object, based on the type of that object Microsoft Roadmap        5.1 Introduction 5.2 Polymorphism Examples 5.3 Demonstrating Polymorphic Behavior 5.4 Abstract Classes and Methods 5.5 Case Study: Payroll System Using Polymorphism 5.6 sealed Classes and Methods 5.7 Operator Overloading Microsoft 5.3 Demonstrating Polymorphic Behavior  The previous chapter discusses relationship between Base Classes and Derived Classes   It created a commission employee class hierarchy, in which class BasePlusCommissionEmployee inherited from class CommissionEmployee The example PolymorphismTest.cs demonstrates three ways to use base class and derived class variables to store references to base class and derived class objects    Microsoft assign a base class reference to a base class variable assign a derived class reference to a derived class variable assign a derived class reference to a base class variable 10 Roadmap 5.1 Introduction 5.2 Polymorphism Examples 5.3 Demonstrating Polymorphic Behavior 5.4 Abstract Classes and Methods 5.5 Case Study: Payroll System Using Polymorphism 5.6 sealed Classes and Methods 5.7 Operator Overloading Microsoft 33 5.6 sealed Classes and Methods   sealed is a keyword in C# sealed methods cannot be overridden in a derived class    Methods that are declared static and private, are implicitly sealed sealed classes cannot have any derived-classes Creating sealed classes can allow some runtime optimizations  Microsoft e.g., virtual method calls can be transformed into non-virtual method calls 34 Roadmap 5.1 Introduction 5.2 Polymorphism Examples 5.3 Demonstrating Polymorphic Behavior 5.4 Abstract Classes and Methods 5.5 Case Study: Payroll System Using Polymorphism 5.6 sealed Classes and Methods 5.7 Operator Overloading Microsoft 35 5.7 Operator Overloading     C# contains many operators (such as + and - ) that are defined for some primitive types It is often useful to use operators with user-defined types (e.g., a complex number class) Operator notation may often be more intuitive then method calls C# allows programmers to overload operators to make them sensitive to the context in which they are used Microsoft 36 5.7 Operator Overloading        Methods define the actions to be taken for the overloaded operator They are in the form: public static ReturnType operator operatorto-be-overloaded( arguments ) These methods must be declared public and static The return type is the type returned as the result of evaluating the operation The keyword operator follows the return type to specify that this method defines an operator overload The last piece of information is the operator to be overloaded (such as +, -, *, etc.) If the operator is unary, one argument must be specified, if the operator is binary, then two, etc Microsoft 37 ComplexNumber.cs 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 // ComplexNumber.cs // Class that overloads operators for adding, subtracting // and multiplying complex numbers public class ComplexNumber { Property Real provides access to private int real; real part of the complex number private int imaginary; the // default constructor public ComplexNumber() {} // constructor public ComplexNumber( int a, int b ) { Real = a; Imaginary = b; } Class ComplexNumber definition // return string representation of ComplexNumber public override string ToString() { return "( " + real + ( imaginary < ? " - " + ( imaginary * -1 ) : " + " + imaginary ) + "i )"; } // property Real public int Real { get { return real; } 38 ComplexNumber.cs 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 set { real = value; } } // end property Real Overload the addition (+) operator for ComplexNumbers // property Imaginary public int Imaginary { get { return imaginary; } set { imaginary = value; } Property Imaginary provides access to the imaginary part of a complex number } // end property Imaginary // overload the addition operator public static ComplexNumber operator + ( ComplexNumber x, ComplexNumber y ) { return new ComplexNumber( x.Real + y.Real, x.Imaginary + y.Imaginary ); } 39 ComplexNumber.cs 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 // provide alternative to overloaded + operator // for addition public static ComplexNumber Add( ComplexNumber x, Method Subtract ComplexNumber y ) { alternative to the return x + y; } – provides an subtraction operator Overloads the multiplication (*) for ComplexNumbers // overload the subtraction operator operator public static ComplexNumber operator - ( ComplexNumber x, ComplexNumber y ) { return new ComplexNumber( x.Real - y.Real, x.Imaginary - y.Imaginary ); } // provide alternative to overloaded - operator // for subtraction public static ComplexNumber Subtract( ComplexNumber x,– provides an Method Add ComplexNumber y ) alternative to the addition operator { return x - y; } Overload the subtraction (-) operator for ComplexNumbers // overload the multiplication operator public static ComplexNumber operator * ( ComplexNumber x, ComplexNumber y ) { return new ComplexNumber( x.Real * y.Real - x.Imaginary * y.Imaginary, x.Real * y.Imaginary + y.Real * x.Imaginary ); } 40 ComplexNumber.cs 98 99 100 101 102 103 104 105 106 107 // provide alternative to overloaded * operator // for multiplication public static ComplexNumber Multiply( ComplexNumber x, ComplexNumber y ) { return x * y; } } // end class ComplexNumber Method Multiply – provides an alternative to the multiplication operator 41 OperatorOverloading.cs 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 // Fig 10.27: OperatorOverloading.cs // An example that uses operator overloading using using using using using using System; System.Drawing; System.Collections; System.ComponentModel; System.Windows.Forms; System.Data; public class ComplexTest : System.Windows.Forms.Form { private System.Windows.Forms.Label realLabel; private System.Windows.Forms.Label imaginaryLabel; private System.Windows.Forms.Label statusLabel; private System.Windows.Forms.TextBox realTextBox; private System.Windows.Forms.TextBox imaginaryTextBox; private private private private private System.Windows.Forms.Button System.Windows.Forms.Button System.Windows.Forms.Button System.Windows.Forms.Button System.Windows.Forms.Button firstButton; secondButton; addButton; subtractButton; multiplyButton; private ComplexNumber x = new ComplexNumber(); private ComplexNumber y = new ComplexNumber(); [STAThread] static void Main() { Application.Run( new ComplexTest() ); } 42 OperatorOverloading.cs 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 private void firstButton_Click( object sender, System.EventArgs e ) { x.Real = Int32.Parse( realTextBox.Text ); x.Imaginary = Int32.Parse( imaginaryTextBox.Text ); realTextBox.Clear(); Use addition operator to add imaginaryTextBox.Clear(); statusLabel.Text = two ComplexNumbersis: " + x; "First Complex Number } private void secondButton_Click( object sender, System.EventArgs e ) { y.Real = Int32.Parse( realTextBox.Text ); Use subtraction operator to y.Imaginary = Int32.Parse( imaginaryTextBox.Text ); realTextBox.Clear(); subtract two ComplexNumbers imaginaryTextBox.Clear(); statusLabel.Text = "Second Complex Number is: " + y; } // add complex numbers private void addButton_Click( object sender, System.EventArgs e ) { statusLabel.Text = x + " + " + y + " = " + ( x + y ); } // subtract complex numbers private void subtractButton_Click( object sender, System.EventArgs e ) { statusLabel.Text = x + " - " + y + " = " + ( x - y ); } 43 68 69 70 71 72 73 74 75 // multiply complex numbers private void multiplyButton_Click( object sender, System.EventArgs e ) Use multiplication operator to { statusLabel.Text = x + " two ComplexNumbers * y ); multiply * " + y + " = " + ( x } Program Output } // end class ComplexTest 44 OperatorOverloading.cs Program Output 45 Summary  Polymorphism   Abstract class     The ability to process objects that share the same base class in a class hierarchy as if they are all objects of the base class Allows you to provide an appropriate base class from which other classes can inherit An abstract class can declare abstract methods that each derived class must implement to become a concrete class, An application can use variables of an abstract class to invoke derived class implementations of abstract methods polymorphically sealed methods and classes Microsoft 46 References  Visual C#® 2005: How to Program, Second Edition By H M Deitel - Deitel & Associates, Inc., P J Deitel Deitel & Associates, Inc  Microsoft Visual C# 2008 How to Program, 3/E Outline 47 ... class PolymorphismTest 12 Roadmap 5.1 Introduction 5.2 Polymorphism Examples 5.3 Demonstrating Polymorphic Behavior 5.4 Abstract Classes and Methods 5.5 Case Study: Payroll System Using Polymorphism. ..      5.1 Introduction 5.2 Polymorphism Examples 5.3 Demonstrating Polymorphic Behavior 5.4 Abstract Classes and Methods 5.5 Case Study: Payroll System Using Polymorphism 5.6 sealed Classes... Overloading Microsoft 5.1 Introduction  The third pillar of OOP is polymorphism   Let you treat related objects in a similar way Polymorphism allows programmers to write:   Microsoft Programs

Ngày đăng: 02/08/2014, 09:20

Mục lục

  • Chapter 5. Understanding Object-OrientedProgramming: Polymorphism

  • Objectives

  • Roadmap

  • 5.1 Introduction

  • Slide 5

  • 5.2 Polymorphism Examples

  • Slide 7

  • Slide 8

  • Slide 9

  • 5.3 Demonstrating Polymorphic Behavior

  • PolymorphismTest.cs (1/2)

  • PolymorphismTest.cs (2/2)

  • Slide 13

  • 5.4 Abstract Classes and Methods

  • Slide 15

  • Slide 16

  • Slide 17

  • 5.5 Case Study: Payroll System Using Polymorphism

  • Employee.cs (1/2)

  • Employee.cs (2/2)

Tài liệu cùng người dùng

Tài liệu liên quan