Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Smart Pascal wikipedia , lookup
Name mangling wikipedia , lookup
Resource management (computing) wikipedia , lookup
Class (computer programming) wikipedia , lookup
C Sharp (programming language) wikipedia , lookup
Design Patterns wikipedia , lookup
Function object wikipedia , lookup
C Sharp syntax wikipedia , lookup
CS413 Object Oriented Methodology Objects An object is a bundle of variables and related methods. A method is a procedure (code) to manipulate variables Procedural Languages: written with very little reference to the real world objects Object Oriented is simulation of real-world objects Object Oriented methodology: a program simulates the states and activities of real world objects An object variable is a single variable from an object's data structure A method is an operation which can modify an objects behaviour. Change an object by manipulating it's variables. Only an object's methods can modify it's variables cs413_OO.ppt CS413 Object Oriented Methodology encapsulation = information hiding: Cannot directly access variables from outside the object (other objects or modules) Only the objects methods (procedures) and can access the objects variables cs413_OO.ppt CS413 Object Oriented Methodology Classes: Ignore anything not directly concerning the behaviour or state of an object A class is a blueprint for an object A blueprint, or outline of an object Whether we have 1 or 1,000 such objects A class does not represent an object A class represents • all the information a typical object should have • all the methods it should have cs413_OO.ppt CS413 Object Oriented Methodology Java class definition for a counter example class Counter extends Object { private int MyCounter; Counter() { MyCounter = 0; } public void InitialiseCounter(int value) MyCounter = value; } public void IncrementCounter(void) { MyCounter++; } public int GetCounterValue(void) return (MyCounter); } } cs413_OO.ppt { { CS413 Object Oriented Methodology JAVA All new classes must be defined with the extension extends Object This defines the superclass There are no public or private sections All variables and methods are prefixed with the appropriate qualifier private : These define the state of the object They cannot be accessed from outside the class declaration. This is encapsulation public : Contains all the object's methods Can be accessed outside the class declaration Methods are the only means of communication with the object cs413_OO.ppt CS413 Object Oriented Methodology JAVA Class Constructor: • a method with the same name as the class definition • should contain any start-up code for the object Instantiating (creating) objects in Java Counter i; Variable ‘i’ is an instance of Counter object i = new Counter(); • define a variable to reference the object in the first line • create an instance of the object by a call to new in the second line cs413_OO.ppt CS413 Object Oriented Methodology Benefits of Object Oriented Programming: Encapsulation - cannot alter the value of the counter other than by incrementing it or setting it to a initial value. This reduces possible bugs. Modularity - different programmers or teams can work on different independent objects. Inheritance – objects can have the same variables as higher order objects cs413_OO.ppt CS413 Object Oriented Methodology Subclass - a class definition which takes functionality from a previous class definition Only define the additional information class ReverseCounter extends Counter { public void DecrementCounter(void) { MyCounter--; } } The new class ReverseCounter is a subclass of Counter Instantiate a subclass ReverseCounter object, we can use any method that the superclass Counter provides i.IncrementCounter(); Superclass Counter i.DecrementCounter (); Private int MyCounter; Subclasses cannot use MyCounter protected int MyCounter; cs413_OO.ppt Subclass ReverseCounter Allow subclasses to use MyCounter CS413 Object Oriented Methodology blueprint Superclass Variables Methods object object Subclass Subclass Variables Variables Methods Methods cs413_OO.ppt