Presentation is loading. Please wait.

Presentation is loading. Please wait.

More on Classes Pepper With help from rs.html.

Similar presentations


Presentation on theme: "More on Classes Pepper With help from rs.html."— Presentation transcript:

1 More on Classes Pepper With help from http://docs.oracle.com/javase/tutorial/java/javaOO/classva rs.html

2  Refers to the instance of the class that is running the code right now  Static methods have no “this” because they run the code from the Class, itself  http://docs.oracle.com/javase/tutorial/java/javaOO/thi skey.html http://docs.oracle.com/javase/tutorial/java/javaOO/thi skey.html This inside the class code that has an instance

3  First create a variable to hold an instance  MyClass x ;  Then fill it with an instance by calling constructor  x = new MyClass(1);  Access its public variables with :  x.myvar1  Access its methods:  x.myMethod(‘a’); Using Objects (such as in your main routine)

4  Just as arrays passed to methods are really just passing a pointer to the array, objects pass only pointers.  When your main method calls mymethod(player1), it is updating the Player object player1 inside the main method.  Note: Strings act like primitives and pass real copies (not under the covers, but they act as if they do) Passing Objects

5  Why? When other programmers use your class, you must not change anything public or you will break their code.  Ex: Fang – if we have a game using advance(), and Fang upgrades to insist on advance(int y), all our code breaks  How?  Public – everyone  Private – no one  Protected – extended can  Nothing - your package (your bluej panel) can  Method or Variable http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html Encapsulating

6  Static – just like method static – one per class  Ex: Class variable total # of bikes  static int numbBikes = 0; (defaulting to 0)  each bike instance points to that one variable # of bikes.  Can change if your program asks it to  numbBikes = numbBikes + 1;  Changes the total number for all the bike instances, not just the one you are accessing.  Without static – a separate value for each instance  Ex: speed, color of a bike  int speed = 0;  Constants  Make it unchangeable with the word final after static  Capitalize by convention  ex: maximum number of bikes:  static final int MAXBIKES = 15;  Cannot later have MAXBIKES = MAXBIKES+1;  http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html Class Variable

7  Arrays can hold objects  If a point has a x and y, an array of 3 points holds 3 point objects, each with their own x and y.  Point[] p = new Point[2]; // makes 2 player miniboxes  p[0] = new Point(1,2); // puts a point object into p[0]  p[1] = new Point(3,4); // puts a point object into p[1]  p[0].getDistance(p[1]); // asks p[0] for its distance from p[1]  See in debug Arrays holding objects

8  Do these exercises, but your deck of cards should only have the face cards so you have less coding. JQKA  http://docs.oracle.com/javase/tutorial/java/javaOO/Qa ndE/creating-questions.html http://docs.oracle.com/javase/tutorial/java/javaOO/Qa ndE/creating-questions.html Exercises

9  Do question #1 and Exercise 1 & 2. (Skip the garbage collection questions.)  http://docs.oracle.com/javase/tutorial/java/javaOO/Qa ndE/objects-questions.html http://docs.oracle.com/javase/tutorial/java/javaOO/Qa ndE/objects-questions.html Exercise 2

10  Shortcut for loop through an array:  for (variable to hold value : array name)  Ex: int[] arr= {1,2,3,4}; int tot = 0; for (int x : arr) { tot = tot + x;} System.out.println(tot); More on Arrays – For Each


Download ppt "More on Classes Pepper With help from rs.html."

Similar presentations


Ads by Google