c368162778d248ee61f0761f6b778e6d.ppt
- Количество слайдов: 42
Object-Oriented Programming Concepts
Good Questions What are Objects? v What are Classes? v What are Messages? v What is Inheritance? v
What Are Objects? v Software objects model read-world objects or abstract concepts • dog, bicycle, queue v Real-world objects have states and behaviors • Dogs' states: name, color, breed, hungry • Dogs' behaviors: barking fetching v How do Software objects implement real-world objects? • Use variables to implement states • Use methods to implement behaviors v An object is a software bundle of variables and related methods
Visual Representation of A Software Object Method Variable
Software Bicycle a Instance Variables and Instance Method
Encapsulation The objects' variables make up the center of the object. v Methods surround and hide the object's center from other objects. v Benefit of encapsulation v • Modularity • Information hiding v For implementation or efficiency reasons, an object may wish to expose some of its variables or hide some of its methods.
What Are Classes? A class is a blueprint or prototype defining the variables and methods common to all objects of a certain kind. v An object is an instance of a certain class. v After you have created a class, you must create an instance of it before you can use. v Class variables and class methods v The benefit of Classes: Reusability v
Visual Representation of A Software Class
What Are Messages? a Software objects interact and communicate with each other by sending messages to each other.
More on Messages v Components of a Message • The object to whom the message is addressed (Your Bicycle) • The name of the method to perform(change. Gears) • Any parameters needed by the method (low gear) v The Benefits of Messages • Messages passing supports all possible interactions between objects (aside from direct variable access) • Objects don’t need to be in the same process or even on the same machine to send and receive messages.
What is Inheritance? v Inheritance allows classes to be defined in terms of other classes • superclass and subclass Each subclass inherits variables and methods from its superclass. v Subclasses can add variables and methods to the ones they inherit from the superclass. v Subclasses can also override inherited method and provide specialized implementations for those methods. v Inheritance or class hierarchy v
Benefits of Inheritance Programmers can reuse the code in the superclass many times. v Programmers can implement superclasses called abstract classes v • Abstract class defines “generic” behaviors • Define and may partially implement the behavior but much of the class is undefined and unimplemented. Other programmers fill in the details with specialized subclasses.
Message Example
Inheritance Example
Object-Oriented Development in Java
Agenda Java Class and Object Declaration v The Life Cycle of an Object v Controlling Access to Members of a Class v
Java Classes and Objects Class: templates for specifying the state and behavior of an object at runtime Object: instances of a class The concepts of class/object provide a mechanism for encapsulation
Basic Structures of a Class Declaration v Variable v • Instance Variable • Class Variable Constructor v Method v • Instance Method • Class Method Cleanup v Rectangle 2. java v
Point and Rectangle 2. java
The Class Declaration
Constructor v A method in a class that initialize an instance of an object before it's used. • The same name as the class and have no return type v Multiple Constructors: the name but a different number of arguments or different typed arguments • Method Overloading Java Provides default constructors. v The special variable, this, can be used inside a method to refer to the object instance. v Rectangle. java v
Member Variables Declaration
Instance and Local Variable v v Local variable is defined inside a block of code or a method. Example (First. Class. java) • Instance Variable: first. Variable • Local Variable: half v public int get. Half() { int half; // local variable half = first. Variable / 2; return half; }
Methods
Method Declaration
Return a Value from a Method Use return operator in the method to return the value. v Methods can return a primitive type or a reference type. v The class of the returned object must be either a subclass of or the exact class of the return type. v
Method Overload Signature of a Method: return value, name, parameter list v Method Overloading: Use the same method name with different arguments to group together related methods. v Constructors can also be overloaded. v • this(parameters) : Call another constructor within a constructor v Example: First. Class. java, Second. Class. java, Rectangle. java
Passing Information into a Method v Argument types • primitive and reference data type: Yes • method: No v Argument Names • Can have the same name as one of the class's member variable u Use this to refer to the member variable Primitive arguments are passed by value. v Reference arguments are passed by reference. v First. Class. java v
The Life Cycle of an Object Creating Objects Using Objects Cleaning Up Unused Objects
Creating Objects v Rectangle r = new Rectangle(5, 5, 100, 200); • Declaration: Rectangle r (Type name) • Instantiation: new u Allocate memory for the object u Initialize instance variables u Call a constructor • Initialization by Calling a Constructor u Rectangle(5, 5, 100, 200)
Using Objects v Manipulate or inspect its variables u object. Reference. variable - r. x = 50 - r. y = 80 v Call its methods u object. Reference. method. Name(argument. List) - r. move(20, 30) v Java provides an access control mechanism whereby classes can restrict or allow access to its variables and methods.
Clean Up v When all references to an object are dropped, the object is no longer required, and become eligible for garbage collection. • Call finalize() to release system resources such as open files or open sockets before the object is collected. • Release references to other objects • protected void finalize() throws Throwable u Rectangle 2. java
Controlling Access to Members of a Class
Private class Alpha { private int iamprivate; private void private. Method() { System. out. println ("private. Method"); } } class Beta { void access. Method() { Alpha a = new Alpha(); a. iamprivate = 10; a. private. Method(); } } How About one Alpha object access the private member of another Alpha object?
Protected package Greek; class Alpha { protected int iamprotected; protected void protected. Method() { System. out. println ("protected. Method"); } } package Greek; class Gamma { void access. Method() { Alpha a = new Alpha(); a. iamprotected = 10; a. protected. Method(); } }
Protected (II) import Greek. *; package Latin; class Delta extends Alpha { void access. Method(Alpha a, Delta d) { a. iamprotected = 10; d. iamprotected = 10; a. protected. Method(); d. protected. Method(); } }
Public package Greek; public class Alpha { public int iampublic; public void public. Method() { System. out. println("publi c. Method"); } } import Greek. *; package Roman; class Beta { void access. Method() { Alpha a = new Alpha(); a. iampublic = 10; a. public. Method(); } }
Package package Greek; class Alpha { int iampackage; void package. Method() { System. out. println("pack age. Method"); } } package Greek; class Beta { void access. Method() { Alpha a = new Alpha(); a. iampackage = 10; a. package. Method(); } }
Inhereitance in Java A mechanism you can use to create a new class by extending the definition of another class Increase the reusability of codes Single Inheritance
Simple Class Hierarchy Object Vehicle Car Van Building Bike House Office Truck Superclass and Subclass
More About Inheritance Use the extends keyword to create a subclass. v Method Override v • Use method overriding when you need a subclass to replace a method of its superclass. • Define a new method that replaces the superclass method that has the same signature. v Calling Superclass Methods • super. <method>(parameters) • super(parameters) (Calling superclass constructors) v Example: Graphics. Program. java
c368162778d248ee61f0761f6b778e6d.ppt