Скачать презентацию Why So Much for So Little We Скачать презентацию Why So Much for So Little We

be07af68e259543f107df5410e0e6955.ppt

  • Количество слайдов: 24

Why So Much for So Little? • We want to use computers to solve 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. OOP with Java, David J. Barnes Creating and Using Objects 1

What are Object-Oriented Languages? • Support an O-O view of problem solving. • 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. OOP with Java, David J. Barnes Creating and Using Objects 2

What are pure Object-Oriented Languages? • Support an O-O view of problem solving exclusively. What are pure Object-Oriented Languages? • Support an O-O view of problem solving exclusively. 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. OOP with Java, Eric Jul Creating and Using Objects 3

Modeling the Operation of a Port • Classes and objects in the scenario. – 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. OOP with Java, David J. Barnes Creating and Using Objects 4

Interactions in the Port • • • Transfer of cargo between ship and dock. 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. OOP with Java, David J. Barnes Creating and Using Objects 5

Classes and Objects • A class definition provides a description of a typical object Classes and Objects • A class definition provides a description of a typical object within that class. – Cargo. Ship, Passenger. Ferry. • An individual object is an instance of a class. • Definition of behavior and attributes. OOP with Java, David J. Barnes Creating and Using Objects 6

Attributes • Attributes store the state of an object. • The state stored in 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). OOP with Java, Eric Jul Attributes 7

Class and Instances • Liner might capture the common characteristics of ocean liners. – 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. OOP with Java, David J. Barnes Creating and Using Objects 8

Creating an Object // An illustration of object creation. // (The ship class is Creating an Object // An illustration of object creation. // (The ship class is assumed to preexist. ) class Ship. Main 1 { 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(); } } OOP with Java, David J. Barnes Creating and Using Objects 9

Object Interactions • Communication between people: – Information: “I am going to a party. 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. OOP with Java, David J. Barnes Creating and Using Objects 10

Sending a Message to a Ship // An illustration of message sending. class Ship. Sending a Message to a Ship // An illustration of message sending. class Ship. Main 2 { 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(); } } OOP with Java, David J. Barnes Creating and Using Objects 11

Moving a Ship // An illustration of ship movement. class Ship. Main 3 { Moving a Ship // An illustration of ship movement. class Ship. Main 3 { public static void main(String[] args){ Ship argo = new Ship(); // Ask the ship to move. argo. move(); } } OOP with Java, David J. Barnes Creating and Using Objects 12

Passing Arguments with Messages • The report and move messages convey implicit information. – 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. . . ” OOP with Java, David J. Barnes Creating and Using Objects 13

Arguments • Explicit information can be passed to an object in a message by 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. OOP with Java, David J. Barnes Creating and Using Objects 14

Passing Arguments class Ship. Main 4 { public static void main(String[] args){ Ship argo Passing Arguments class Ship. Main 4 { 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. set. Course(90); argo. move(); argo. report(); } } OOP with Java, David J. Barnes Creating and Using Objects 15

Receiving Replies • Human communication often requires a reply. – “What is the time? 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. OOP with Java, David J. Barnes Creating and Using Objects 16

Simple Example of Reply • Ship’s speed is returned as a value – a 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. OOP with Java, David J. Barnes Creating and Using Objects 17

Requesting a Ship’s Speed class Ship. Main 5 { public static void main(String[] args){ Requesting a Ship’s Speed class Ship. Main 5 { public static void main(String[] args){ Ship argo = new Ship(); argo. report(); // Define a variable to hold the speed. double current. Speed; // Ask the ship what its current speed is. current. Speed = argo. get. Speed(); // Increase the ship's speed. argo. set. Speed(current. Speed+1); argo. move(); argo. report(); } } OOP with Java, David J. Barnes Creating and Using Objects 18

Variables and Objects • Variables and objects are different. • A variable may refer 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. OOP with Java, David J. Barnes Creating and Using Objects 19

Variables Contain References • A variable contains either a value or a reference to 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. OOP with Java, Eric Jul Creating and Using Objects 20

The Main Method's Class • We (Barnes and Jul) choose never to create an 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. OOP with Java, David J. Barnes/Eric Jul Creating and Using Objects 21

Review • O-O problem solving involves identifying classes, objects and object interactions. • Objects Review • O-O problem solving involves identifying classes, objects and object interactions. • Objects maintain state and exhibit classdefined behavior. • Instances of the same class behave in similar ways. • Message passing illustrates object interaction. OOP with Java, David J. Barnes Creating and Using Objects 22

Review (cont. ) • Messages may contain additional information: in the form of arguments. 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. OOP with Java, David J. Barnes Creating and Using Objects 23

Review (cont. ) • Variables contain references. • In hybrid languages, variables may alternatively Review (cont. ) • Variables contain references. • In hybrid languages, variables may alternatively contain values. • References can be thought of as pointers. OOP with Java, David J. Barnes & Jul Creating and Using Objects 24