tailieunhanh - Lecture Software construction - Lecture 2: Introduction to object-orientation

This chapter presents the following content: The OO approach is based on modeling the real world using interacting objects; the statements in a class define what its objects remember and what they can do (the messages they can understand), that is, they define; the hardest concept in this set of lecture slides; a UML class diagram shows the “bare bones” of an OO system design. | Lecture 2 Introduction to Object-Orientation 1 Message Passing 2 In a method call, a message is passed to a receiver object. The receiver’s response to the message is determined by its class. (50, 100); Ball b = new Ball(10, 20, ); b: Ball xPos =10 yPos = 20 Color = Red b: Ball xPos = 10 60 yPos = 20120 Color = Red receiver message arguments public class Ball { . public void move(int deltaX, int deltaY) { xPos += deltaX; yPos += deltaY; } } Instance & Class Variables 3 Class variables are statically allocated, so they are shared by an entire Class of objects. The runtime system allocates class variables once per class, regardless of the number of instances created of that class. Static storage is allocated when the class is loaded. All instances share the same copy of the class variables. Instance variables are dynamically allocated, so they may have different values in each instance of an object. When an object is instantiated, the runtime system allocates some . | Lecture 2 Introduction to Object-Orientation 1 Message Passing 2 In a method call, a message is passed to a receiver object. The receiver’s response to the message is determined by its class. (50, 100); Ball b = new Ball(10, 20, ); b: Ball xPos =10 yPos = 20 Color = Red b: Ball xPos = 10 60 yPos = 20120 Color = Red receiver message arguments public class Ball { . public void move(int deltaX, int deltaY) { xPos += deltaX; yPos += deltaY; } } Instance & Class Variables 3 Class variables are statically allocated, so they are shared by an entire Class of objects. The runtime system allocates class variables once per class, regardless of the number of instances created of that class. Static storage is allocated when the class is loaded. All instances share the same copy of the class variables. Instance variables are dynamically allocated, so they may have different values in each instance of an object. When an object is instantiated, the runtime system allocates some memory to this instance – so that it can “remember” the values it stores in instance variables. b2: Ball xPos =10 yPos = 10 Color = Blue : Class name = “Ball” size = 10 b1: Ball xPos=10 yPos = 20 Color = Red 3 Instance & Class Methods 4 Instance methods operate on this object's instance variables. They also have read & write access to class variables. Class methods are static. Class methods cannot access instance variables. Class methods are handled by the “class object” – they can be called even if there are no instances of this class. (Example on the next slide.) public class Class1 { private int x; public int increment() { ++x; } } Class1App 5 public class Class1App { public static void main( String[] args ) { Class1 x = new Class1(); ( "Without initialisation, ++x = " + () ); ( "After another incrementation, ++x = " + () ); } } BallApp 6 import .*; import .*; public class BallApp extends Frame{ Ball b = new