Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 20 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
20
Dung lượng
383,57 KB
Nội dung
public void add(Test tt) { tt.x += tt.y; System.out.println(tt.x); } } Now the Test class defines two integer variables, x and y. In main(), you declare a Test object, t, and set t.x to 1 and t.y to 2. Next, you call the add() method, only this time, it accepts a Test object reference instead of two primitive data types as its argument. The reference to t is named tt within the method. You can see that it does the same thing as in the previous example. It adds up two vari- ables’ values and stores the result in the first variable. The difference here is that these variables belong to the Test object, which is ref- erenced by both t (outside the method) and tt (inside the method). When you reassign the tt.x variable, the value becomes 3, and is printed. When control returns to main(), the value of t.x will reflect this change and will be 3, too. It’s like when you call the plumber to come and fix your toilet. You don’t send him a clone of your toilet, you tell him where it is so he can work his magic and when he’s done, your toilet is fixed. It’s the same concept here, you tell the method where your object is and then it goes and operates on it, and when it’s done, your object might have changed. Method Overloading Another important parameter-passing concept for you to learn is method over- loading. When two methods have the same name, but have different types or numbers of arguments, the method is overloaded. Methods names are not unique, you can have two different methods with the same name that do two dif- ferent things if their signatures are different. Consider these three methods: //method accepts two int arguments public void add(int a, int b) { a += b; System.out.println(a); } //method accepts three int arguments public void add(int a, int b, int c) { a += b + c; System.out.println(a); } //method accepts two double arguments public void add(double a, double b) { a += b; System.out.println(a); } 138 J a v a P r o g r am m i n g f o r t h e A b s o l ut e B e gi n n e r JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 138 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. They are all named add(), but they are three different methods. Obviously, because they are all named the same thing, they should do something similar. The first one adds the two int arguments and prints the result. The second one adds the three int arguments and prints the result, and the last one adds the two double arguments and prints the result. The one that is called depends on the number and type of arguments you pass in. The following snippet of code calls these three different methods: add(1, 2); add(1, 2, 3); add(1.5, 2.5); Returning Values Sick of seeing the void keyword in front of every single method? As you know, when you declare a method using the void keyword, you are signifying that the method does not return any values. You can, however, return a value from a method. When a method returns a value, it passes it back to the invoker. A method returns a value by using the return keyword. The type of value returned is part of the method signature. For example, the following high() method accepts two int arguments and returns the value that is the higher of the two (or the second value if they are the same): public int high(int a, int b) { int higher; if (a > b) higher = a; else higher = b; return higher; } Every method that returns a value must use the return keyword followed by a value, variable, or expression of the type specified in the method signature. In this example, the following line returns the value of the higher variable which is assigned the value of either a or b (the higher of the two): return higher; You can use a call to a method that returns a value in an assignment statement to capture the return value. Take the high() method as an example. To call this method and assign its value to the max variable, do this: int max = higher(2, 4); The high() method does its thing and then returns the value 4, which is assigned to the max variable. There’s a bit more to learn about the return keyword. It abruptly exits the method and returns control back to the method invoker, ignoring any statements 139 C h a p t e r 5 B l a c k j a c k: O b j e c t- O r i e n t e d P r o g r a m m i n g JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 139 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. that follow the return keyword within the method. You can rewrite the high() method example as follows: public int high(int a, int b) { if (a > b) return a; return b; } Knowing this, you don’t have to declare a local higher variable to temporarily store the return value. Notice that I didn’t include an else? If the condition a > b evaluates true, return a; will be executed, taking control out of the method, never to reach the return b; statement. If it is false, return a; is never evalu- ated, bringing control to the return b; statement. You can also return from a method that does not return a value by using the return keyword not followed by an expression. For example, say the processOdd method example that follows is a method that prints only odd numbers that are passed to it. public void processOdd(int oddNumber) { if (oddNumber % 2 == 0) return; … doStuffWithOddNumbers; … } If you know right off the bat that you don’t want to process even numbers, but you don’t want to stick the bulk of your method’s statements inside of an if - else condition, you can just exit the method using the return statement. Understanding Static Methods Just like static variables, static methods do not operate on specific instances of the class, but apply to the class as a whole. A great example that demonstrates this concept is the String.valueOf(int) method. The signature for this method is as follows: public static String valueOf(int i) It returns the String representation of the integer argument passed to it. You don’t need a specific instance of String to perform this task, so the method is a static method. You don’t have to create a String object just to get a String rep- resentation of an int. To invoke this method, because it is a static method, you only need to reference the String class, the method name, and pass it an int: int myIntValue = String.valueOf(5); The main() method is a static method that is called by the Java interpreter when you run an application, but you can also explicitly call this method like any other static method. The MainTest program, listed next, calls the SimpleCardDeckTest’s static main() method. As you can see in Figure 5.6, the same results shown in Fig- ure 5.2 are output. 140 J a v a P r o g r am m i n g f o r t h e A b s o l ut e B e gi n n e r JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 140 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 141 C h a p t e r 5 B l a c k j a c k: O b j e c t- O r i e n t e d P r o g r a m m i n g FIGURE 5.6 The SimpleCard- DeckTest ’s main() method is directly called by the MainTest application. /* * MainTest * Shows how main() can be treated like any other * static method. */ public class MainTest { public static void main(String args[]) { SimpleCardDeckTest.main(null); } } Defining Constructor Methods Another type of method is the constructor method. Constructor methods initialize an object of a class when the class is instantiated. Constructor methods are sim- ilar to other methods except that they must have the same name as the class they are defined in and they never have a return type defined in their method signa- tures. Take a look at the Automobile class again. There are two different con- structors, one that accepts no arguments, and another that accepts three: a Boolean, a string, and an int. The constructor is overloaded and the constructor that’s called depends on the arguments that are passed to it: public Automobile() { this(false, DEFAULT_COLOR, 0); } public Automobile(boolean running, String color, int numMiles) { this.running = running; this.color = color; this.numMiles = numMiles; name = null; } Looking back at the AutomobileTest class, both of these constructors were called: Automobile auto1 = new Automobile(); JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 141 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. and… Automobile auto2 = new Automobile(true, “green”, 37000); You’ve seen the new keyword before. Anytime you create a new object, you use the new keyword to allocate space in memory for the object and call its constructor. You’ve seen this syntax before, now you know what it means: ClassName objectName = new ClassName(); The parentheses are there because it is actually making a call to the constructor method. You do not have to define a constructor for your class, although in most cases, you will. Defining constructors allows you to have more control over the initial state of your objects when they are created. If you don’t define a con- structor, an empty do-nothing constructor is created for you by the compiler, which accepts no arguments and doesn’t have a method body. When you create a new object using the new keyword, your system allocates memory to store the object and initializes its instance variables. Variables that hold objects are initialized to null, numbers are initialized to 0, Boolean values are initialized to false, and character values are initialized to \u0000. If you assign values to instance variables where they are declared (remember, instance variables are not declared in any method), the variables are assigned those values first. If your constructor reassigns values to the variables, they get those values when the object is created. If you do not define any constructor methods, you create a new object using this syntax: MyClass objectName = new MyClass(); This is because the constructor, MyClass(), is created by the compiler. The com- piler does this only if you don’t define a constructor yourself. So if you do define at least one constructor that accepts any number of arguments, the compiler won’t create the no-argument MyClass() constructor. For example, if you define a constructor with a signature similar to: public MyClass(int myNumber) but you don’t explicitly define a constructor with no arguments, such as MyClass(), you can no longer create an object without passing in an int argu- ment. In other words, MyClass objectName = new MyClass(); will not work anymore, unless you define MyClass(), but the following construc- tion will work: MyClass objectName = new MyClass(1); TRAP HINT 142 J a v a P r o g r am m i n g f o r t h e A b s o l ut e B e gi n n e r JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 142 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. If at least one constructor method is defined in your Java source file and you try to compile it, you will get a compiler error if any of those methods call an unde- fined constructor method. This is why it’s best to use constructor overloading. By overloading the constructor, the program can either pass in an int argument or choose to not pass in any arguments. Learning another Keyword: this Sometimes it is beneficial to be able to refer to the current object in the body of a method. The this keyword specifies the current instance of the class. Which instance? This one. Most of the time it is omitted, but you do need it to specify a variable as belonging to the instance of the class if there is another variable of the same name declared locally within the method. The this keyword can only be used in instance methods and constructors. It cannot be used in class ( static) methods because they do not operate on specific instances of the class. Take the setColor() method from the Automobile class as an example. public void setColor(String color) { this.color = color; } The color variable is declared locally in this method as its only parameter. A vari- able of the same name is also declared within the class definition as a member variable. The statement color = color; reassigns the local variable’s value to itself. You need a way to specify that you are assigning the value of the parame- ter to the member variable. Using the this keyword makes it possible. The this.color = color; statement is like saying, “Take the parameter given and assign it to this instance’s color variable.” Don’t forget, because this is an instance method, it must be called through an instance of the Automobile class. In the AutomobileTest program, the line: auto1.setColor(“red”); assigns the auto1 Automobile object’s color variable to red. this can also call a constructor. Again, you can look to the Automobile class for clarification. You see there are two constructors. As is typically done, the grunt work is done in one of the constructors, the one that accepts the most arguments. The constructor that does not accept any arguments simply calls the other constructor, using default values for the parameters, to do the work for it: this(false, DEFAULT_COLOR, 0); This statement calls the workhorse constructor, with the default values, false, the static variable DEFAULT_COLOR, and 0. Then you can see in the Automobile(boolean, String, int) constructor, this specifies the current object’s variables because the parameters are named the same as the instance variables. That’s all I have to say about that…er, this. 143 C h a p t e r 5 B l a c k j a c k: O b j e c t- O r i e n t e d P r o g r a m m i n g JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 143 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Understanding Access Modifiers As you know, objects interact with each other by referencing fields and methods of other objects. Access modifiers define accessibility of an object’s fields and meth- ods. The access modifiers are public (you’ve seen this one before), protected, and private. Field and Method Access Modifiers You can access public variables and methods anywhere. They are directly acces- sible from instance methods, which assume the current object if the this key- word is missing. If a public instance variable is referenced from a static method, it must be accessed through a specific instance of the class. Public member vari- ables are also available from subclasses of the class they are defined in. You learn about subclasses a bit later in this chapter. public class Foo { public int myNumber; public static int three = 3; public void myMethod() { //can access both fields here myNumber = three; } public static void main(String args[]) { Foo manChu = new Foo(); // can’t access myNumber without the actual manChu object // no-no: myNumber = three; // but CAN access three because it is static, the same for all instances manChu.myNumber = three; // - or - the same thing… manChu.myNumber = Foo.three; }4 } Outside of the class definition itself, public variables and methods are accessible, given an instance of the class, within any other class that can reference the class itself. The following FooTest class references a Foo object’s public myNumber vari- able, as well as Foo’s static three variable. public class FooTest { public static void main(String args[]) { Foo manChu = new Foo(); ManChu.myNumber = Foo.three; } } 144 J a v a P r o g r am m i n g f o r t h e A b s o l ut e B e gi n n e r JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 144 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. The protected keyword specifies that a variable or method is accessible only within a class definition or a subclass of the class it is defined in, or from other classes in the same package. Trying to access a protected from any other class will not work. The same goes for private variables and methods, except they are further restricted. private variables and methods are only accessible in the class file they are defined in—they are not accessible even to subclasses. A quick exam- ple of a compiler error you can get is shown in the AccessErrorTest application. First, here is a listing of AccessError.java: /* * AccessError * Has a private method */ public class AccessError { public AccessError() { //Can access it from within this class getPrivateStuff(); } private void getPrivateStuff() { System.out.println(“private stuff”); } } The method getPrivateStuff() in this class is declared private. It is called from its single constructor. You can write and compile this program without any errors because the private method is accessible anywhere in this class. Now, here is a listing of AccessErrorTest.java: /* * AccessErrorTest * Demonstrates a method access error */ public class AccessErrorTest { public static void main(String args[]) { AccessError ae = new AccessError(); //can’t access private method here ae.getPrivateStuff(); } } If you try to compile this program, you will get the compiler error message shown in Figure 5.7. AccessErrorTest is not AccessError and therefore does not have access to its getprivateStuff() method. 145 C h a p t e r 5 B l a c k j a c k: O b j e c t- O r i e n t e d P r o g r a m m i n g JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 145 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Encapsulation Why do access modifiers exist? Using access modifiers allows you to hide inter- nal values and operations from the outside world, allowing for encapsulation. Encapsulation is one of the benefits of object-oriented programming. With encapsulation, types are accessed through their exposed, external values and operations and not by their internal implementations. This is beneficial because you can change the way your classes are implemented internally, but as long as you keep the external operations the same, you don’t have to go back and fix the programs that use it. For an example of encapsulation, take a look at the Prima- ryColor class: /* * PrimaryColor * Demonstrates encapsulation and get and set conventions */ public class PrimaryColor { public final static String BLUE = “blue”, RED = “red”, YELLOW = “yellow”, DEFAULT = “yellow”; private String color; public PrimaryColor(String c) { setColor(c); } public void setColor(String c) { if (c == BLUE || c == RED || c == YELLOW) { color = c; } else { color = DEFAULT; } } 146 J a v a P r o g r am m i n g f o r t h e A b s o l ut e B e gi n n e r FIGURE 5.7 You can’t access a private member or method from outside of the class definition. JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 146 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. public String getColor() { return color; } } The color variable is declared to be private, which means it can’t be referenced from outside of the class definition. However, access is granted to set its value through the public setColor() method or through the constructor, which actu- ally just calls the setColor method. This allows you to filter the value before assigning it to your precious private variable. Remember, this is only a small example. What if there were other operations in this class that depended on the fact that the color variable was, red, blue, or yellow? What if your program crashed horribly when it was any other value or even null? You don’t have to worry about this because of encapsulation. You filter the value and make sure a valid value is passed in. If it’s not, you set it to some valid value yourself. The Pri- maryColorTest application tests this encapsulation stuff: /* * PrimaryColorTest * Demonstrates the concept of encapsulation in the * PrimaryColor class */ public class PrimaryColorTest { public static void main(String args[]) { PrimaryColor red = new PrimaryColor(“red”); System.out.println(“red is “ + red.getColor()); PrimaryColor pink = new PrimaryColor(“pink”); System.out.println(“pink is “ + pink.getColor()); PrimaryColor blue = new PrimaryColor(PrimaryColor.BLUE); System.out.println(“blue is “ + blue.getColor()); } } The output of the PrimaryColorTest application is shown in Figure 5.8. The Pri- maryColorTest application creates three PrimaryColor objects. The first one, red, is constructed using the parameter “ red”, which is fine, because red is a valid argument defined in the implementation of the PrimaryColor class. The second one, pink, passes “pink” to the constructor. Well, pink isn’t a valid argument, but that’s okay too because you just need to set it to the default value PrimaryColor.DEFAULT, and there won’t be any trouble down the line. You can see in the output that the setColor() method caught the fact that an invalid argu- ment was sent and set it to the default value “ yellow”. The last object, blue, 147 C h a p t e r 5 B l a c k j a c k: O b j e c t- O r i e n t e d P r o g r a m m i n g JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 147 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... to either 2 through 10, for playing cards of the same face value, or 11 for a Jack, 12 for a Queen, 13 for a King, and 14 for an Ace The Card class provides corresponding int constants to make this easier to implement: JACK, QUEEN, KING, and ACE The suit variable is a char representation of four possible suits of a playing card: C for Clubs, D for Diamonds, H for Hearts, and S for Spades The Boolean... JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 154 Java Programming for the Absolute Beginner 154 objects due to inheritance, which you learn about next For all objects, it is intended to return the String representation of itself for situations just as this If you look at the API, documentation for this method, you will see that it is actually recommended that this method be overridden for every class Extending a Class... method is therefore known to exist for all TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Blackjack: Object-Oriented Programming public class CardDeckTest { Chapter 5 prints the top card’s index and also the last card in the deck Here is a listing of the source code Take a look at Figure 5.9 for the output JavaProgAbsBeg-05.qxd...JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 148 Java Programming for the Absolute Beginner 148 FIGURE 5.8 The PrimaryColorTest application demonstrates the concept of encapsulation demonstrates a better, more typical way to write such an object creation If a class constant is available to be passed in, it is typically used for readability’s sake The Card and CardDeck... Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Blackjack: Object-Oriented Programming // inherits all of Automobile’s members & methods // and adds more protected boolean trailer; Chapter 5 /* * BigTruck * Extends the Automobile class - is a subclass of Automobile */ JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 156 Java Programming for. .. The constructors ensure that no invalid values are set Notice that all but the constants are protected public methods exist to either get or set the values as needed JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 152 Java Programming for the Absolute Beginner 152 public int getTopIndex() { return top; } /* returns the card at top index and moves the index */ public Card deal() { Card dealt = cards[top];... public and protected member variables and methods, you can use the javap utility By default (it has other options), javap lists the public and protected variables and methods The syntax for this utility is: javap ClassName Here is another quick example that lists all of this chapter’s project’s variables and methods in a text file named t.txt: javap BigTruck –private >t.txt One drawback, though, is that... restricted from 2 to ACE (14) but actualValue can be any integer */ protected int faceValue; protected int actualValue; protected char suit; protected boolean visible; JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 150 150 Java Programming for the Absolute Beginner case ACE: face = “A”; break; default: face = “2”; } } face += suit; return face; } public void setValue(int av) { actualValue = av; } public int... drawback, though, is that it doesn’t list any variables or methods that were inherited TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 155 155 Here is the source listing for BigTruck .java: public class BigTruck extends Automobile { public BigTruck() { this(false, DEFAULT_COLOR, 0);... defines an individual card That is what the Card class is for Here, you define the Card class, and then you will define the CardDeck class that groups together a bunch of Cards Not only will writing the Card class make it easier to write the BlackJack project, it will force you to go deeper into the world of OOP Here is the full source code for Card .java: /* * Card * A class that defines a playing card . values are restricted to either 2 through 10, for playing cards of the same face value, or 11 for a Jack, 12 for a Queen, 13 for a King, and 14 for an Ace. The Card class provides corresponding. is a char rep- resentation of four possible suits of a playing card: C for Clubs, D for Diamonds, H for Hearts, and S for Spades. The Boolean visible variable specifies whether a card is visible. objects are subclasses of. This method is therefore known to exist for all TRICK JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 153 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase