1 / 13

How O bjects Behave

How O bjects Behave. Overview. Methods use object state Arguments and return types in methods Java passes by value Getters and Setters Encapsulation Using references in an Array. Methods use object state. In a non-OOP language, functions need to have lots of parameters

dolf
Télécharger la présentation

How O bjects Behave

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. How Objects Behave

  2. Overview • Methods use object state • Arguments and return types in methods • Java passes by value • Getters and Setters • Encapsulation • Using references in an Array

  3. Methods use object state • In a non-OOP language, functions need to have lots of parameters • Alternatively, you can have lots of functions • Using methods, many objects contain instance variables that can be used to change methods.

  4. Example if (dog_a_weight < 14){ small_bark(); } else { large_bark(); } • We had to have more functions for each ‘state’ of dog. We would also need to do this for each dog!

  5. Java Example • The Java example on the webpage shows how a dog’s barks changes with size

  6. Arguments and return types in methods • Just like in C functions, Java methods can take in arguments and return values • The syntax is identical to C • E.g. int somefunction(int somevalue){ //Some code that uses the variable somevalue return 45; // Returns 45 }

  7. Arguments and return types in methods • You can send multiple arguments as well • The order of the arguments matters void someotherfunction(int x, int y)

  8. Java passes by value • When calling a method, a copy of each argument is made for the function • In C, you can pass pointers where the function manipulates the original value • If you want a function to manipulate a value in Java, the value needs to be returned, or be an instance variable

  9. Getters and Setters • Getters = Accessors • Setters = Mutators • Getters and Setters are simple methods that let programs manipulate an objects instance variables • Why does this matter? Cant we access these values already with the dot operator?

  10. Encapsulation • This is the concept of keeping our data safe so that only valid value can be put in instance variables • We set instance variables to private • We set setters and getters to public

  11. Example • Lets modify our previous example to use the concept of encapsulation • We will even add some simple error checking

  12. Instance Variables vs. Local Variables • Just like in C, where variables are defined sets their ‘scope’ • Instance variables are accessible by all methods and are declared in the class outside of any method • Local variables only apply to one method and are defined at the top of the method they are used in

  13. Comparing Objects • Simple comparison of primitives can be done with ‘==‘ • How can we do this with objects? • To see if two object reference variables are the same, still use ‘==‘ • To see of the instance values in two objects are the same, write an ‘equals()’ method and use it

More Related