Bài giảng điện tử môn tin học: Object-Oriented Programming ppsx

92 570 0
Bài giảng điện tử môn tin học: Object-Oriented Programming ppsx

Đ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

08/13/14 Võ Phương Bình - ITFAC - DLU 1 Part II: Object-Oriented Programming  Chapter 6: Object-Oriented Programming  Chapter 7: Strings 08/13/14 Võ Phương Bình - ITFAC - DLU 2 Chapter 6: Object-Oriented Programming  Introduction  Superclasses and Subclasses  protected Members  Relationship between Superclass Objects and Subclass Objects  Implicit Subclass-Object-to-Superclass-Object Conversion  Composition vs. Inheritance  Introduction to Polymorphism 08/13/14 Võ Phương Bình - ITFAC - DLU 3 Chapter 6: Object-Oriented Programming  Dynamic Method Binding  final Methods and Classes  Abstract Superclasses and Concrete Classes  Polymorphism Example  New Classes and Dynamic Binding  Inheriting Interface and Implementation  Creating and Using Interfaces  Inner Class Definitions  Notes on Inner Class Definitions 08/13/14 Võ Phương Bình - ITFAC - DLU 4 Introduction  Object-Oriented Programming (OOP)  Inheritance - form of software reusability  New classes created from existing ones  Absorb attributes and behaviors, and add in their own  Override methods - redefine inherited methods  Subclass inherits from superclass  Direct superclass - subclass explicitly inherits  Indirect superclass - subclass inherits from two or more levels up the class hierarchy  Polymorphism  Write programs in a general fashion to handle a wide variety of classes  Abstraction - seeing the big picture 08/13/14 Võ Phương Bình - ITFAC - DLU 5 Introduction, cont  Object-Oriented Programming  Introduce protected member access  Relationships  "is a" - inheritance  Object of subclass "is a" object of the superclass  "has a" - composition  Object "has a" object of another class as a member  Class libraries  New classes can inherit from them  Someday software may be constructed from standardized, reusable components (like hardware)  Create more powerful software 08/13/14 Võ Phương Bình - ITFAC - DLU 6 Superclasses and Subclasses  Inheritance example  A rectangle "is a" quadrilateral  Rectangle is a specific type of quadrilateral  Quadrilateral is the superclass, rectangle is the subclass  Incorrect to say quadrilateral "is a" rectangle  Naming can be confusing because subclass has more features than superclass  Subclass more specific than superclass  Every subclass "is an" object of its superclass, but not vice- versa  Form tree-like hierarchal structures  Create a hierarchy for class Shape (next slide) 08/13/14 Võ Phương Bình - ITFAC - DLU 7 Superclasses and Subclasses, cont  Using inheritance  Use keyword extends class TwoDimensionalShape extends Shape{ }  private members of superclass not directly accessible to subclass  All other variables keep their member access Shape TwoDimensionalShape ThreeDimensionalShape Circle Square Triangle Sphere Cube Tetrahedron 08/13/14 Võ Phương Bình - ITFAC - DLU 8 protected Members  In a superclass  public members  Accessible anywhere program has a reference to a superclass or subclass type  private members  Accessible only in methods of the superclass  protected members  Intermediate protection between private and public  Only accessible by methods of superclass, of subclass, or classes in the same package  Subclass methods  Can refer to public or protected members by name  Overridden methods accessible with super.methodName 08/13/14 Võ Phương Bình - ITFAC - DLU 9 Relationship between Superclass Objects and Subclass Objects  Object of subclass  Can be treated as object of superclass  Reverse not true  Suppose many classes inherit from one superclass  Can make an array of superclass references  Treat all objects like superclass objects  Explicit cast  Convert superclass reference to a subclass reference (downcasting)  Can only be done when superclass reference actually referring to a subclass object  instanceof operator  if (p instanceof Circle)  Returns true if the object to which p points "is a" Circle 08/13/14 Võ Phương Bình - ITFAC - DLU 10 Relationship between Superclass Objects and Subclass Objects, cont  Overriding methods  Subclass can redefine superclass method  When method mentioned in subclass, subclass version used  Access original superclass method with super.methodName  To invoke superclass constructor explicitly (called implicitly by default)  super(); //can pass arguments if needed  If called explicitly, must be first statement  Every Applet has used these techniques  Inheritance concept formalized  Java implicitly uses class Object as superclass for all classes  We have overridden init and paint when we extended JApplet [...]... use the "is a" relationship to refer to a Circle // with a Point reference pointRef = c; // assign Circle to pointRef output += "\n\nCircle c (via pointRef): " + pointRef.toString(); // Use downcasting (casting a superclass reference to a // subclass data type) to assign pointRef to circleRef circleRef = (Circle) pointRef; output += "\n\nCircleBình - ITFAC - DLU Võ Phương c (via circleRef): " + circleRef.toString();... polymorphic programming   08/13/14 Walk through an array of superclass references Call draw method for each reference Võ Phương Bình - ITFAC 25 New Classes and Dynamic Binding  Dynamic binding (late binding)    Accommodates new classes Object's type does not need to be known at compile time At execution time, method call matched with object 08/13/14 Võ Phương Bình - ITFAC 26 Inheriting Interface... (Circle) p; 115 output += "\n\ncast successful"; 116 } 117 // line 40 in Test.java else 118 output += "\n\np does not refer to a Circle"; 119 120 JOptionPane.showMessageDialog( null, output, 121 "Demonstrating the \"is a\" relationship", 122 JOptionPane.INFORMATION_MESSAGE ); 123 124 System.exit( 0 ); 125 126 08/13/14 } } Võ Phương Bình - ITFAC - DLU 15 Program Output 08/13/14 Võ Phương Bình - ITFAC - DLU... Circle, and Square all subclasses of Shape  Each has an overridden draw method  Call draw using superclass references  At execution time, program determines to which class the reference is actually pointing  Calls appropriate draw method 08/13/14 Võ Phương Bình - ITFAC 20 final Methods and Classes    Declaring variables final  Indicates they cannot be modified after declaration  Must be initialized... 74 08/13/14 } } Võ Phương Bình - ITFAC - DLU 13 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 08/13/14 105 // Fig 27.3: InheritanceTest.java // Demonstrating the "is a" relationship import java.text.DecimalFormat; import javax.swing.JOptionPane; public class InheritanceTest { public static void main( String args[] ) { Point pointRef, p; Circle circleRef,... ].area() ) + 163 "\nVolume = " + 164 precision2.format( arrayOfShapes[ i ].volume() ); 165 } 166 167 JOptionPane.showMessageDialog( null, output, 2.1 Call methods using array of references 168 "Demonstrating Polymorphism", 169 JOptionPane.INFORMATION_MESSAGE ); 170 171 System.exit( 0 ); 172 173 } } 08/13/14 Võ Phương Bình - ITFAC - DLU 35 Program Output 08/13/14 Võ Phương Bình - ITFAC - DLU 36 . DLU 1 Part II: Object-Oriented Programming  Chapter 6: Object-Oriented Programming  Chapter 7: Strings 08/13/14 Võ Phương Bình - ITFAC - DLU 2 Chapter 6: Object-Oriented Programming  . Inheriting Interface and Implementation  Creating and Using Interfaces  Inner Class Definitions  Notes on Inner Class Definitions 08/13/14 Võ Phương Bình - ITFAC - DLU 4 Introduction  Object-Oriented. Inheritance  Introduction to Polymorphism 08/13/14 Võ Phương Bình - ITFAC - DLU 3 Chapter 6: Object-Oriented Programming  Dynamic Method Binding  final Methods and Classes  Abstract Superclasses

Ngày đăng: 11/08/2014, 22:23

Mục lục

  • Part II: Object-Oriented Programming

  • Superclasses and Subclasses, cont

  • Relationship between Superclass Objects and Subclass Objects

  • Relationship between Superclass Objects and Subclass Objects, cont

  • final Methods and Classes

  • Abstract Superclasses and Concrete Classes

  • New Classes and Dynamic Binding

  • Inheriting Interface and Implementation

  • Creating and Using Interfaces

  • Creating and Using Interfaces, cont

  • Inner Class Definitions, cont

  • Notes on Inner Class Definitions

  • Notes on Inner Class Definitions, cont

  • Java API and Core Java classes

  • Retrieving Individual Characters in a String

  • Finding a Character or a Substring in a String

  • Convert Character and Numbers to Strings

  • Appending New Contents into a String Buffer

  • The StringTokenizer Class Constructors

  • The StringTokenizer Class Methods

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

  • Đang cập nhật ...

Tài liệu liên quan