OBJECT ORIENTED PROGRAMMING (lập TRÌNH NÂNG CAO SLIDE)

155 23 0
OBJECT ORIENTED PROGRAMMING (lập TRÌNH NÂNG CAO SLIDE)

Đ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

ADVANCED PROGRAMMING OBJECT ORIENTED PROGRAMMING CLASSES AND OBJECT Outline    Working with Classes and Objects  Defining Classes  Creating Objects  Writing and Invoking Constructors Using Methods  Defining a Method  The Static Methods and Variables  Methods with a Variable Number of Parameters  JavaBeans Naming Standard for Methods Method Overriding and Overloading Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 2/155 Outline        Inheritance Abstract Classes Writing and Using Interfaces Object-Oriented Relationships  The is-a Relationship  The has-a Relationship Polymorphism Conversion of Data Types Understanding Garbage Collection Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 3/155 Programming?  OOP  – –   Student Class Map your problem in the real - studentNo - courseId name world: Real world objects and birthday - lecturer + serLecturer() actions match program objects + enroll() + getStudents() … and actions Define “things” (objects) which can something Create a “type” (class) for these objects so that you don’t have to redo all the work in defining an objects properties and behavior An OO program: “a bunch of objects telling each other what to by sending messages” (Smalltalk) A strong reflection of software engineering   Abstract data types Information hiding (encapsulation) Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 4/155 Goals of Object Technology  To create a software: – Robustness: capable of handling unexpected inputs that are not explicitly defined for its application – Adaptability: evolve over time in response to changing conditions in its environment – Reusability: the same code should be usable as a component of different systems in various applications Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 5/155 Important OO concepts      Abstraction Encapsulation Objects & Class  Object state and behavior "P.I.E triangle  Object identity Abstraction  Messages Inheritance Polymorphism Encapsulation  Information/implementation hiding Inheritance Polymorphism Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 6/155 Benefits of Object Technology 1) Faster application development at a lower cost 2) Decreased maintenance time 3) Less complicated and faster customization 4) Higher quality code Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 7/155 Objects  Objects are:   Are building blocks for systems Contain data that can be used or modified   Bundle of variables and related methods An object possesses:  Identity: Định danh   State: Trạng thái   What the object remembers Interface: Giao tiếp   A means of distinguishing it from other objects Messages the object responds to Behavior: Ứng xử  What the object can Student - studentNo - name - birthday + getName() + enroll() Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 8/155 Object Example  The car shown in the figure can be considered an object  It has an ID ("1"),  state (its color, for instance, and other characteristics),  an interface (a steering wheel and brakes, for example)  and behavior (the way it responds when the steering Many texts regard an wheel is turned or the brakes are applied) object as possessing only two characteristics – state and behavior When considered this way, identity is part of the state, and the interface is included in the behavior Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 9/155 Classes  A class   Objects of the same class are similar with respect to:      Defines the characteristics and variables common to all objects of that class Interface Behavior (method) State (variable) Used to instantiate (create an instance of) specific objects Provide the ability of reusability  Car manufacturers use the same blueprint to build many cars over and over Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 10/155 Typing and Interfaces  A variable's type can be an interface  Stipulations    Only objects whose class implements that interface can be bound to that variable Only messages defined by the interface can be used Interfaces cannot appear in a new expression File File File File r f = = = = new new new new File(); File(); // // Error Error TextFile(); TextFile(); // // OK! OK! Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 141 Subinterfaces  Interfaces can be extended   Interface hierarchy is independent of the class hierarchy The interface which extends another interface inherits all of its method declarations interface interface File File {{ public public void void open(String open(String name); name); public public void void close(); close(); }} interface interface ReadableFile ReadableFile extends extends File File {{ public public byte byte readByte(); readByte(); }} interface interface WritableFile WritableFile extends extends File File {{ public public void void writeByte(byte writeByte(byte b); b); }} interface interface ReadWriteFile ReadWriteFile extends extends ReadableFile, ReadableFile, WritableFile WritableFile {{ public public void void seek(int seek(int position); position); }} Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 142 Using Interfaces  Using interfaces allows     Cross-hierarchy polymorphism Access to methods in separate class trees Substitution of an object for another object which is not related via the class hierarchy Classes that implement the same interface understand the same messages  Regardless of their location in the class hierarchy Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 143 Interfaces  Allows more control over how objects are used  The programmer can define a method's parameters as interfaces    This restricts the use of those parameters The programmer knows which message the objects will respond to Improves reusability of code Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 144 Interfaces • Make "able" interfaces – • Name interfaces using proper nouns and provide "Impl" implementation of your interfaces – – • Cloneable, Serializable, Bank, BankImpl, BankAccount, BankAccountImpl With this convention, the interface typically contains a definition for all (or most) of the implementation class' public methods Prefix interface names with "I" and use proper nouns for your classes – IBank, Bank, IBankAccount Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 145 The Interface Comparable interface Comparable{ // compare this object with the given object // produce negative result if this is 'smaller' than the given object // produce zero if this object is 'the same' as the given object // produce positive result if this is 'greater' than the given object int compareTo(Object obj); } Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 146 Case study: Comparable Interface public class Employee implements Comparable{ private String name; private double salary; public Employee(String n, double s) { name = n; salary = s; } @Override public int compareTo(Object o) { Employee emp = (Employee)o; return this.getName().compareTo(emp.getName()); } } Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 147 Sort Array of Comparable Objects public class EmployeeSortTest { public static void main(String[] args) { Employee[] staffs = new Employee[3]; staffs[0] 35000); staffs[1] 75000); staffs[2] 38000); = new Employee("Harry Hacker", = new Employee("Carl Cracker", = new Employee("Tony Tester", Arrays.sort(staffs); // print out information about all Employee for (Employee e : staffs) System.out.println("Name = " + e.getName() Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 148 Interface public public class class Employee Employee {{ private private String String name; name; private private double double salary; salary; public public Employee(String Employee(String n, n, double double s) s) {{ name salary name == n; n; salary == s; s; }} public public class class EmployeeSortTest EmployeeSortTest {{ }} public public static static void void main(String[] main(String[] args) args) {{ Employee[] Employee[] staffs staffs == new new Employee[3]; Employee[3]; staffs[0] staffs[0] == new new Employee("Harry Employee("Harry Hacker", Hacker", 35000); 35000); staffs[1] staffs[1] == new new Employee("Carl Employee("Carl Cracker", Cracker", 75000); 75000); staffs[2] staffs[2] == new new Employee("Tony Employee("Tony Tester", Tester", 38000); 38000); Arrays.sort(staffs, Arrays.sort(staffs, new new Comparator() Comparator() {{ public public int int compare(Employee compare(Employee o1, o1, Employee Employee o2) o2) {{ return return o1.getName().compareTo(o2.getName()); o1.getName().compareTo(o2.getName()); }} Khoa 149 }); CNTT – ĐH Nông Lâm TP HCM 01/2007 Conversion of Data Types   Two kinds of Conversion of Data Types  Conversion of Primitive Data Types  Conversion of Reference Data Types Conversion of Data Types  Implicit type conversion: The programmer does not make any attempt to convert the type  Explicit type conversion: Conversion is initiated by the programmer by making an explicit request for conversion (type casting) Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 150 Conversion of Data Types Assignment Conversion s = new (); t = s; // Implicit conversion of to  Method Call Conversion  Arithmetic Conversion  Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 151 Implicit Primitive Type Conversion  Two general rules for implicit primitive type conversion are the following:  There is no conversion between boolean and non-boolean types  A non-boolean type can be converted into another non-boolean type only if the conversion is not narrowing—that is, the size of the target type is greater than or equal to the size of the source type Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 152 Implicit Primitive Type Conversion public class ConversionToWider{ public static void main(String[] args) { int i = 15; short s = 10; Error: Illegal s= i; conversion System.out.println("Value of i: " + i ); } } Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 153 Implicit Primitive Type Conversion Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 154 Explicit Primitive Type Conversion     In a narrowing conversion, casting is mandatory However, casting to a narrower type runs the risk of losing information and generating inaccurate results You cannot cast a boolean to a nonboolean type You cannot cast a non-boolean type to a boolean type Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 155 ... (objects) which can something Create a “type” (class) for these objects so that you don’t have to redo all the work in defining an objects properties and behavior An OO program: “a bunch of objects... Trạng thái   What the object remembers Interface: Giao tiếp   A means of distinguishing it from other objects Messages the object responds to Behavior: Ứng xử  What the object can Student -... are two objects which provide concrete installations of the class 2 Khoa CNTT – ĐH Nông Lâm TP HCM 01/2007 11/155 Working with Classes     The class is the basis for object- oriented programming

Ngày đăng: 29/03/2021, 10:53

Mục lục

    What is Object-Oriented Programming?

    Goals of Object Technology

    Benefits of Object Technology

    Message and Object Communication

    The Elements of a class

    Writing and Invoking Constructors

    Writing and Invoking Constructors

    More on Constructor Chaining

    Declaring Member Variables - Fields

    Controlling Access to Members of a Class

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

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