1 / 37

Object-Oriented Software Engineering

Object-Oriented Software Engineering. CS288. Java OO Fundamentals. Contents Classes and Objects Making new objects Method declarations Encapsulating fields Manipulating object field values Static Declarations. Classes and Objects. Car bigFlashJag. numDoors = 5 engineSize = 3.5l

Télécharger la présentation

Object-Oriented Software Engineering

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. Object-Oriented Software Engineering CS288

  2. Java OO Fundamentals Contents • Classes and Objects • Making new objects • Method declarations • Encapsulating fields • Manipulating object field values • Static Declarations

  3. Classes and Objects Car bigFlashJag numDoors = 5 engineSize = 3.5l make = Jaguar start ( ) stop ( ) Class Object Object Car ecoCar numDoors = 2 engineSize = 0.5l make = Smart start ( ) stop ( ) Class: Template that defines how to make an object. Object: Is an instance of a class. Car int numDoors Double engineSize String make void start ( ) { code } void stop ( ) { code }

  4. Building Objects Class Definition (acts like template for new objects) Object1 Object2 Object3 Object4 Computer Memory Each object has copies of fields and methods defined by class template, but initialised differently each time

  5. Very Simple Class Example Main method executed first. This is where objects can first be setup. Constructor, defines how new objectsare initialised. public class SimpleClass { public SimpleClass(String newFieldVal) { uselessField = newFieldVal; } private String uselessField; public static void main(String[ ] args) { /* Code Goes Here */ } }

  6. Syntax for defining new object object name object type new keyword semicolon terminatesALL statements(just like C++) class constructor to set up object SimpleClass ob1 = new SimpleClass("Fluffy");

  7. Very Simple Class Example public class SimpleClass { public SimpleClass(String newFieldVal) { uselessField = newFieldVal; } private String uselessField; public static void main(String[ ] args) { SimpleClass ob1 = new SimpleClass("Fluffy"); SimpleClass ob2 = new SimpleClass("I like chocolate"); SimpleClass ob3 = new SimpleClass("Why cant I go home"); SimpleClass ob4 = new SimpleClass("Flobber Worms Taste Nice"); SimpleClass ob5 = new SimpleClass("Guildford"); } }

  8. Executing the Code Five new objects are created in memory, each with their owncopy of ‘uselessField’, and each copy has a different value. Slide shows view of data from NetBeans debugging console.

  9. Setter and Getter Methods public class SimpleClass { /* declare class field */ /* define class constructor */ public String getUselessField () { return uselessField; } public void setUselessField (String newUselessField) { uselessField = newUselessField; } /* main method */ }

  10. Getter method for field type of object returned by method public keywordallows this method to be called from other classes name and parameters of method body of method,what it does public String getUselessField () { return uselessField; }

  11. Setter method for field has String object parameter does not return value changes value of field public void setUselessField (String newUselessField) { uselessField = newUselessField; }

  12. Extending main method public static void main(String[ ] args) { SimpleClass ob1 = new SimpleClass("Fluffy"); SimpleClass ob2 = new SimpleClass("I like chocolate"); SimpleClass ob3 = new SimpleClass("Why cant I go home"); SimpleClass ob4 = new SimpleClass("Flobber Worms Taste Nice"); SimpleClass ob5 = new SimpleClass("Guildford"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = ob2.getUselessField (); ob1.setUselessField (aStrVar); } Example shows use of setting and getting methods for uselessField

  13. Execution of main method When execution gets to hereobjects have these values public static void main(String[ ] args) { SimpleClass ob1 = new SimpleClass("Fluffy"); SimpleClass ob2 = new SimpleClass("I like chocolate"); SimpleClass ob3 = new SimpleClass("Why cant I go home"); SimpleClass ob4 = new SimpleClass("Flobber Worms Taste Nice"); SimpleClass ob5 = new SimpleClass("Guildford"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = ob2.getUselessField (); ob1.setUselessField (aStrVar); }

  14. Execution of main method When execution gets to hereobjects have these values public static void main(String[] args) { SimpleClass ob1 = new SimpleClass("Fluffy"); SimpleClass ob2 = new SimpleClass("I like chocolate"); SimpleClass ob3 = new SimpleClass("Why cant I go home"); SimpleClass ob4 = new SimpleClass("Flobber Worms Taste Nice"); SimpleClass ob5 = new SimpleClass("Guildford"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = ob2.getUselessField (); ob1.setUselessField (aStrVar); }

  15. Execution of main method When execution finishes hereobjects have these values public static void main(String[ ] args) { SimpleClass ob1 = new SimpleClass("Fluffy"); SimpleClass ob2 = new SimpleClass("I like chocolate"); SimpleClass ob3 = new SimpleClass("Why cant I go home"); SimpleClass ob4 = new SimpleClass("Flobber Worms Taste Nice"); SimpleClass ob5 = new SimpleClass("Guildford"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = ob2.getUselessField (); ob1.setUselessField (aStrVar); }

  16. Backup Method for Field Value Modify SimpleClass to include new field and methods: private String previousVal; public void setUselessField (String newUselessField) { previousVal = uselessField; uselessField = newUselessField; } public void restoreField () { uselessField = previousVal; }

  17. Experiment with main method When execution gets to hereobjects have these values public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass ("Fluffy"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = "Eat Cabbage"; ob1.setUselessField (aStrVar); ob1.restoreField (); }

  18. Experiment with main method When execution gets to hereobjects have these values public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass ("Fluffy"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = "Eat Cabbage"; ob1.setUselessField (aStrVar); ob1.restoreField (); }

  19. Experiment with main method When execution gets to hereobjects have these values public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass ("Fluffy"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = "Eat Cabbage"; ob1.setUselessField (aStrVar); ob1.restoreField (); }

  20. Experiment with main method When execution gets to hereobjects have these values public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass ("Fluffy"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = "Eat Cabbage"; ob1.setUselessField (aStrVar); ob1.restoreField (); }

  21. Is there a problem with the restore method? When execution gets to hereobjects have these values public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass ("Fluffy"); SimpleClass ob2 = new SimpleClass("I like chocolate"); ob1.setUselessField ("I wish I'd done C instead"); ob2.restoreField (); }

  22. Static Declarations • Not all methods and fields belong to Objects • Static methods and fields stay inside the class • There is only ever one copy of a static method or field • Static methods and fields are `shared’ between all the objects of that class

  23. Static Declarations • When we declare a new variable with a statement we create a reference to the thing we want the variable to name • E.g SimpleClass ob1 = new SimpleClass("Fluffy");SimpleClass ob1 = ob2; ob2 ob1 Object:SimpleClass("Fluffy")

  24. Static Declarations • Static fields stay with the class not the object • E.g change declaration in SimpleClassprivate static String uselessField = "oooo"; • Then in main method create two objects:SimpleClass ob1 = new SimpleClass("Fluffy");SimpleClass ob1 = new SimpleClass("Fluffy"); ob2 ob1 Object:SimpleClass("Fluffy")field: Object:SimpleClass("Fluffy")field: Class: SimpleClassfield: uselessField

  25. Static Declarations Changing the value of the field within one object now affects all the otherobjects of that class: SimpleClass ob1 = new SimpleClass("Fluffy"); SimpleClass ob2 = new SimpleClass("Fluffy"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = ob2.getUselessField (); NetBeans Debugger Output

  26. Static Methods • Static methods are more complex than static fields • This is NOT allowed:public class SimpleClass { private String uselessField = "oooo"; private String initialVal = ""; public static void setUselessField(String newUselessField) { uselessField = newUselessField; } /* and so on */ If we attempt to compile this we get error: SimpleClass.java: non-static variable uselessField cannot be referenced from a static context uselessField = newUselessField; 1 error BUILD FAILED (total time: 0 seconds)

  27. Static Methods • Static methods are more complex than static fields • E.G. this is NOT allowed:public class SimpleClass { private String uselessField = "oooo"; private String initialVal = ""; public static void setUselessField(String newUselessField) { uselessField = newUselessField; } /* and so on */ Static methods may only refer to static fields.Whereas here uselessField is NOT declared static.WHY NOT?

  28. Static Methods • Static methods are more complex than static fields • E.G. this is NOT allowed:public class SimpleClass { private static String uselessField = "oooo"; private String initialVal = ""; public static void setUselessField(String newUselessField) { uselessField = newUselessField; } /* and so on */ Now uselessField is declared static. Code compiles without error.

  29. Static Methods • Can reference static methods and fields directly from the class,do not need to create an object first. • E.G. public static void main(String[ ] args) { • SimpleClass ob1 = new SimpleClass("Fluffy"); • SimpleClass ob2 = new SimpleClass("Fluffy"); • SimpleClass.setUselessField ("I wish I'd done C instead"); } To use the static method here we can access it directly fromthe class. (Notice that the main method is always declared static. WHY?)

  30. Static Methods Consider adding a new method to store previous value used for uselessField: Consider adding a new method to store previous value used for uselessField, which needs a new field to hold that value in: private String lastValueField; public void storeLastValue () { lastValueField = uselessField; } Then could use this whenever set value of uselessField so that we can track the last value

  31. Static Methods Then could use this whenever set value of uselessField so that we can track the last value. public static void setUselessField(String newUselessField) { uselessField = newUselessField; /* this keeps track of the last value used for uselessField */ storeLastValue(); } Problem: Wont Compile, get error: non-static method storeLastValue() cannot be referenced from a static context: storeLastValue();

  32. Static Methods • Problem: • We must have setUselessField declared static for some reason. • For some other reason we must have that storeLastValue is not declared static. • But we cant access storeLastValue in method body setUselessField of as it is not static.

  33. Static Methods Solution: public static void setUselessField(String newUselessField, SimpleClass obj) { uselessField = newUselessField; obj.storeLastValue(); } public void storeLastValue ( ) { lastValueField = uselessField; } Adds new argument Now refer to method via objectsname.

  34. `this’ keyword New Problem, the constructor method. public SimpleClass( ) { setUselessField("Some String", ????????); initialVal = uselessField; } What value goes here? Solution: keyword `this’`this’ is a reference to the current object.The object whose method or constructor is being called.

  35. `this’ keyword Reference with `this’ must then be introduced into othermethods that call setUselessField: public SimpleClass( ) { setUselessField("Some String", this); initialVal = uselessField; } public void resetField() { setUselessField(initialVal, this); } public SimpleClass(String newUslessField) { setUselessField(newUslessField, this); }

  36. `this’ keyword `this’ can also be used to make methods more readable.When a method argument is to be used to update an existingfield, then it is often the case that the argument is given the same name as the field name. This leads to a conflict thatcan be resolved by using `this’. For example instead of: public static void setUselessField(String newUselessField, SimpleClass obj) { uselessField = newUselessField; obj.storeLastValue(); } we can have: public static void setUselessField(String uselessField, SimpleClass obj) { this.uselessField = uselessField; obj.storeLastValue(); }

  37. Summing Up • Every object has copies of the class fieldsand methods. • We have seen example field values for multiple objects. • Seen examples of methods for accessing and changing field values. • We have seen how objects are created and manipulated during execution of the main method. • Seen how static methods and fields are only created once within the class and are not copied to new objects.

More Related