1 / 24

Why So Much for So Little?

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. What are Object-Oriented Languages?. Support an O-O view of problem solving.

danyl
Télécharger la présentation

Why So Much for So Little?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. Review (cont.) • Variables contain references. • In hybrid languages, variables may alternatively contain values. • References can be thought of as pointers. Creating and Using Objects

More Related