1 / 21

Chapter 5 Constructors

Chapter 5 Constructors. 1 Constructors. 2 Overloading. 3 . 4 Problem. 5 Solution. 6 Scope. 7 Object variables. Complex conjugate. 8 Example. public complexNumber conjugate() { complexNumber conj = new complexNumber(new cartesian(this.x,-this.y); return conj; }. add.

chad
Télécharger la présentation

Chapter 5 Constructors

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

  2. 1 Constructors Constructors

  3. 2 Overloading Constructors

  4. 3 ... Constructors

  5. 4 Problem Constructors

  6. 5 Solution Constructors

  7. 6 Scope Constructors

  8. 7 Object variables Constructors

  9. Complex conjugate 8 Example public complexNumber conjugate() { complexNumber conj = new complexNumber(new cartesian(this.x,-this.y); return conj; } add public complexNumber add (complexNumber operand) { double RealSum = this.x + operand.x; double ImSum = this.y + operand.y; return new complexNumber(new cartesian(RealSum, ImSum)); } Do I need to expose the internal state Constructors

  10. multiply 9 Private public complexNumber mult (complexNumber operand) { double RealProd = this.Real()*operand.Real() - this.Imaginary()+operand.Imaginary(); double ImProd = this.Real()*operand.Imaginary() + this.Imaginary()*operand.Real(); return new complexNumber(new cartesian(RealProd, ImProd)); } Here I am protecting one part of a class from changes in another part. multiply works if the storage of state change takes longer to execute add needs changing if the state changes executes fast Constructors

  11. 10 Storing state How to store state? public class complexNumber { private polar point; … } public class complexNumber { private cartesian point; … } How to decide? Constructors

  12. 11 Printing out an object public String toString() { String Cn = "(" + x + ", " + y + "i)"; return Cn; } The output from System.out.println(objectName); Is whatever is returned by toString; override toString in the Object class Constructors

  13. 12 Recap Constructors

  14. 13 Inheritance Constructors

  15. 14 ... Constructors

  16. 15 Extends Constructors

  17. 16 Core API Constructors

  18. 17 Pillage then burn Constructors

  19. 18 Evolution Constructors

  20. 19 Access Constructors

  21. 20 Object class Constructors

More Related