head first java second edition phần 4 pot

68 481 0
head first java second edition phần 4 pot

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

inheritance and polymorphism The important point is that the reference type AND the object type are the same In this example, both are Dog But with polymorphism, the reference and the object can be different Animal myDog - new ~ () ; ~/' two nest: i~e NOT the sd",e t)'Pe ne re-ftyel'lu I/ariable t)'Pt is detlarta as APli aI, b~t tht objtt+' is tytaud as Pltw D~O you are here ~ 185 polymorphism In action With polymorphism, the reference type can be a superclass of the actual object type When you declare a reference variable, any object that passes the IS-Atest for the declared type of the reference variable can be assigned to that reference In other words , anything that extends the declared reference variable type can be assigned to the reference variable This lets you thing! like make polyrruwphic arrays animals [0] = new Dog(); animals [1] animals [2) animals [3) 186 chapter = new cat ; = new Wolf() ; = new Hippo () ; tt lIariable, and tht A"~,,a IIdr-iable is as.si~"ed a l\tw A"u",."a objet+" public class CellPhone ( private Antenna ant = new Antenna (l ; private Antenna ant :: new Antenna () ; you are here ~ 239 object creation fhe tttiracle of object creatlott Now that you know where variables and objects live, we can dive into the mysterious world of object creation Remember the three steps of object declaration and assignment: declare a reference variable, create an object, and assign the object to the reference But until now, step two where a miracle occurs and the new object is "boron-has remained a Big Mystery Prepare to learn the facts of object life Hope you're not squeamish Review the steps of object declaration, creation and assignment: Declare O variable a reference Duck myDuck = e ~t.\t ~ e ot.t.v.V'~ ~ yV\\V' gI new Duck () : Create an object Duck myDuck = new Duck () ; "tv' e Link the object and the reference Duck myDuck @)new Duck () ; _ Duck reference 240 chapter rWL constructors and gc Are we calling a method named DuckO? Because it sure looks like it Duck myDuck = No We're calling the Duck constructor A constructor does look and feel a lot like a method, but it's not a method It's got the code that runs when you say new In other words, the code that runs when you instantiate an object The only way to invoke a constructor is with the keyword new followed by the class name, ThejVM finds that class and invokes the constructor in that class (OK, technically this isn't the only way to invoke a constructor, But it's the only way to it from outsidea constructor You ca'n call a constructor from within another constructor, with restrictions, but we'll get into all that later in the chapter.) But where Is the constructor? If we didn't write It, who did? You can write a constructor for your class (we're about to that), but if you don't, the compilerwrites one for you! Here's what the compiler's default constructor looks like: public Duck () { } Notice something missing? How Is this different from a method? ~ ~Duck() ~trejs the retllm t I this Wtre a 1, ype? rheOlod you d heed d 'ret m t // Iv I'd'" t.\ass e 'IS ~t ~ 1t~'-I T\\at s , a"da\,Pl , sa",t as y,d",t· ( constructor code goes here ) j j betweeh l'PlI.b/' ,, " yPe llDlAlkO", It arid you are here 241 constructing a new Duc k CottStruct a Puck The key feature of a constructor is that it runs before the object can be assigned to a reference That means you get a chance to step in and things to get me object ready for use In other words, before anyone can use the remote contra] for an object, the object has a chance to help construct itself In our Duck constructor, we're not doing anything useful, but it still demonstrates the sequence of events public class Duck { public Duck () { System.out.println("Quack~) ; The constructor gives you a chance to step into the middle of new ~ public class UseADuck { % java UseADuck public static void main (String[] args) { Duck d = new Duck() ; f - - !h;s ~lJs in } (.orutr-lAl!or t: A constructor lets you Jump Into the middle of the object creation step-into the middle of new Can you imagine conditions where that would be useful? Which of these might be usefulin a Car classconstructor, if the Car is part of a Racing Game?Check off the ones that you came up with a scenario for 242 chapter o o o o o o o Quack D lI.lk Increment a counter to track how many objects ofthis class type have been made Assign runtime-specific state (data about what's happening NOW) Assign values tothe object's important instance variables Get and save a reference 10 Ihe object that's creating the new object Add the object to an ArrayList Create HAS-A objects (your idea here) constructors and gc Initializing the state of a new Puck there]lU'H? Dumo ~uest19ns Most people use constructors to initialize the state of an object In other words, to make and assign values to the object's instance variables Q.: Why you need to write a constructor if the compiler writes one for you? public Duck ( ) { size = 34; A: If you need code to help initialize your object and get it ready for use,you'll have to write your own constructor You might, for example, be dependent on input from the user before you can finish making the object ready.There's another reason you might have to write a constructor, even if you don't need any constructor code yourself It has to with your superclass constructor, and we'll talk about that in a few minutes } That's all well and good when the Duck class developer knows how big the Duck object should be But what if we want the programmer who is using Duck to decide how big a particular Duck should be? Imagine the Duck has a size instance variable, and you want the programmer using your Duck class to set the size of the new Duck How could you it? Well, you could add a setSize() setter method to the class But that leaves the Duck temporarily without a size*, and forces the Duck user to write two statements-one to create the Duck, and one to call the setSize () method The code below uses a setter method to set the initial size of the new Duck public class Duck { int size; 'f iY\Sta\'lt e Q.: How can you tell a constructor from a method? Can you also have a method that's the same name as the class? ~aYiah\e public Duck() { System out.println ("Quack") ; 'f t.oY\ShvLto'r A: Java lets you declare a method with the same name as your class That doesn't make it a constructor, though.The thing that separates a method from a constructor is the return type Methods must have a return type, but constructors cannot have a return type } public void setSize (int newSize) { ~ set t,eY ",et.'nod size = newSize; } } public class UseADuck { public static void main (String[] args){ Duck d = new Duck () ; T t i ~~r~'~ a bad thih h d setSize (42) ; Ah~ iPO/ht ;1'1 ih~ l~ ~b~'iThe DlAtk is aI' 1'1 } hel'l "OlA' I~' lA with t D:t a /V~ to KNOW ~hatD~ yihS 01'1 ih~ Si~!*, protess: Ol'le i lAtk lr~atioh is k-fAS~r to tall th~ ~all ih~ tohSirlAtkiwo-pari Stt1 Ul- *Instance variables have a default value or 0.0 for numeric primitives, false for booleans, and null for references ahd Oh~ 0: Are constructors inherai it~(h Ifyou don't provide a constructor but your superclass does, you get the superclass constructor instead of the default? A.: Nope Constructors are not inherited We'll look at that in just a few pages you are here), 243 initializing object state Ushtg the eenstruetor to it1itialize itMportat1t Pock state" If an object shouldn't be used until one or more parts of its state (instance variables) have been initialized, don 't let anyone get ahold of a Duck object until you're finished initializing! It's usually way too risky to let someone makeand get a reference to-a new Duck object that isn 't quite ready for use until that someone rums around and calls the setSize() method How will the Duck-user even know that he's required to call the setter method after making the new Duck? Let the user make a new Duck and set the Duck's size all in one call The call to new The call to the Duck constructor o The best place to put initialization code is in the constructor And all you need to is make a constructor with arguments public class Duck { int size; \,,~ ~a'

Ngày đăng: 12/08/2014, 19:20

Từ khóa liên quan

Mục lục

  • 8 Serious Polymorphism: exploiting abstract classes and interfaces

  • 9 Life and Death of an Object: constructors and memory management

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan