240 likes | 342 Vues
Learn the fundamentals of object-oriented programming, including modeling objects in a port scenario, object interactions, and passing arguments efficiently. Understand classes, attributes, and object communication through examples. Discover how objects handle messages and replies to perform actions effectively.
E N D
Why So Much for So Little? • We want to use computers to solve complex tasks. • Complex problems require powerful programming languages. • Powerful programming languages have more rules than simpler languages. Creating and Using Objects
What are Object-Oriented Languages? • Support an O-O view of problem solving. • Objects categorized into classes. • Objects interact with each other. • Objects have behavior. • Objects maintain state. • Behavior and state are intimately related. • Software reuse is facilitated. Creating and Using Objects
What are pure Object-Oriented Languages? • Support an O-O view of problem solvingexclusively. Examples include Smalltalk, Emerald. • Hybrid OO languages allow mixing values and objects. Examples: Java, C++, Simula 68. • Hybrid languages are considered messy by the OO purists. Creating and Using Objects
Modeling the Operation of a Port • Classes and objects in the scenario. • The port. • Ships moving in and out. • Cargo and associated transportation. • Passengers, vehicles, car parks. • Customs officers, pilots. • Aspects of the natural environment. Creating and Using Objects
Interactions in the Port • Transfer of cargo between ship and dock. • Passengers embarking and disembarking. • A pilot joining or leaving a ship. • A ship requesting permission to enter. • The weather affecting ship movements. Creating and Using Objects
Classes and Objects • A class definition provides a description of a typical object within that class. • CargoShip, PassengerFerry. • An individual object is an instance of a class. • Definition of behavior and attributes. Creating and Using Objects
Attributes • Attributes store the state of an object. • The state stored in an attribute is often a reference to another object. • Simple state is, in hybrid languages, represented as values (non-object values). Attributes
Class and Instances • Liner might capture the common characteristics of ocean liners. • RMS Titanic and RMS Olympic might be two instances of that class. • Class definition is like a blueprint or template. • Color, size, pattern might vary, but instances of the same class come from the same mold. Creating and Using Objects
Creating an Object // An illustration of object creation. // (The ship class is assumed to preexist.) class ShipMain1 { public static void main(String[] args){ // Define a method variable to refer to // a Ship object. Ship argo; // Construct a new Ship object. argo = new Ship(); } } Creating and Using Objects
Object Interactions • Communication between people: • Information: “I am going to a party.” • Question: “What is the time?” • Order/Request: “Please buy me some gum.” • Objects communicate in similar ways. • Passing messages to each other. • Actor/Agent, Client/Server relationships. Creating and Using Objects
Sending a Message to a Ship // An illustration of message sending. class ShipMain2 { public static void main(String[] args){ // Define a method variable. // Make it refer to a new Ship object. Ship argo = new Ship(); // Ask the ship to report its position, // course and speed. argo.report(); } } Creating and Using Objects
Moving a Ship // An illustration of ship movement. class ShipMain3 { public static void main(String[] args){ Ship argo = new Ship(); // Ask the ship to move. argo.move(); } } Creating and Using Objects
Passing Arguments with Messages • The report and move messages convey implicit information. • The Ship uses its own internal state to respond. • Some messages need to be accompanied by further explicit information. • “Change course to ...” • “Reduce speed to ...” Creating and Using Objects
Arguments • Explicit information can be passed to an object in a message by adding arguments to the message. • Arguments are objects containing information. • In hybrid languages, such as Java, arguments can also be values. Creating and Using Objects
Passing Arguments class ShipMain4 { public static void main(String[] args){ Ship argo = new Ship(); argo.report(); argo.move(); // Ask it to change course, move and // report its new settings. argo.setCourse(90); argo.move(); argo.report(); } } Creating and Using Objects
Receiving Replies • Human communication often requires a reply. • “What is the time?” • “Are there any free berths in the port?” • “What is the square-root of 2?” • Similarly, we can request information from a Ship. • We often store the answer somewhere. Creating and Using Objects
Simple Example of Reply • Ship’s speed is returned as a value – a double which here merely is a number, e.g., 17.3 • The value, we may assume, is in knots. • The number is stored and used to request the ship to go one knot faster. Creating and Using Objects
Requesting a Ship’s Speed class ShipMain5 { public static void main(String[] args){ Ship argo = new Ship(); argo.report(); // Define a variable to hold the speed. double currentSpeed; // Ask the ship what its current speed is. currentSpeed = argo.getSpeed(); // Increase the ship's speed. argo.setSpeed(currentSpeed+1); argo.move(); argo.report(); } } Creating and Using Objects
Variables and Objects • Variables and objects are different. • A variable may refer to a single object. • Different variables may refer to the same object - aliases. • A variable may be switched from one object to another. • A variable may be uninitialized. Creating and Using Objects
Variables Contain References • A variable contains either a value or a reference to an object. • A reference can be thought of as a pointer to an object. • Illustrated graphically. Creating and Using Objects
The Main Method's Class • We (Barnes and Jul) choose never to create an object of the main method’s class. • This approach is not followed by many other authorities. • The main method is a static method. • Static methods have special properties that we shall explore in due course. • A messy hack—just live with it for now. Creating and Using Objects
Review • O-O problem solving involves identifying classes, objects and object interactions. • Objects maintain state and exhibit class-defined behavior. • Instances of the same class behave in similar ways. • Message passing illustrates object interaction. Creating and Using Objects
Review (cont.) • Messages may contain additional information: in the form of arguments. • A result may be returned as the result of a message. • Variables and objects are distinct. • Variables switch between different objects. • An object may be simultaneously referred to by different variables. Creating and Using Objects
Review (cont.) • Variables contain references. • In hybrid languages, variables may alternatively contain values. • References can be thought of as pointers. Creating and Using Objects