head first java second edition phần 5 pptx

68 360 0
head first java second edition phần 5 pptx

Đ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

constructors and gc How you lnveke a superclass cOMstructot1 You might think that somewhere in, say, a Duck constructor, if Duck extends Animal you'd call Anirnalj) But that's not how it works: public class Duck exttmds Animal { int size; public Duck(int newSize) 1(,~t>~ 4- Animal () i { ~ NOI ti: size = newSize; hl~ 's lIot Ie II ~ } } The only way to call a super constructor is by calling superi), That's right-super() calls the super consIn.tctor And how is it that we've gotten away without doing it? You probably figured that out, Our good friend the compiler puts in a call to super() If you don't So the complier gets involved in constructor-making in two ways: What are the odds? public class Duck extends Animal { 'j '4'otl t [It U1~ ~II to I 'T.Jy pc-I: 1, lIoP~) belo'o.l / 'ClllythiM eI5e .J constructors and gc Superclass eoastrueters with argUtMettfs What if the superclass constructor has arguments? Can you pass something in to the super() call? Of course If you couldn't you'd never be abl e to extend a class that didn 't have a no-arg constructor, Imagine this scenario: all animals have a name There's a gelName() method in class Animal that returns the value of the name instance variable The instance variable is marked private, but the subclass (in this case, Hippo) inherits the getName() method So here's the problem: Hippo has a getName() method (through inheritance), but does not have the name instance variable Hippo has to depend on the Animal part of himself to keep the name instance variable, and return it when someone calls geLNa1T/~() on a Hippo object But how does the Animal part get the name? The only reference Hippo has to the Animal part of himself is through supert), so that's the place where Hippo sends the Hippo's name up to the Animal pan of himself, so that the Animal part can store it in the private n~me instance variable public abstract class Animal { private String name; ~ public String getName () return name; slobdas.ses) hall' a "a",e Illppo illherib private String name Animal(StTing n) String getNameO T Hippo Hlppo(String n) All al'li",al s (i"dwii,,~ (~~ 9~k "'rl::~od Animal [other Hippo-specific methods] ihai public Animal(String name = theName; public class Rippo extends Animal ( ~ public class MakeHippo ( %j a v a MakeHippo public static void main(StJ:ing[] args) ( NJak e d ~ Hippo h = new Bippo("Buffy"); System out println (h getName () ) ; C" \~ f: Il't Nlte Y6 a sonlj ' have to asK t'ke .v-selves e sc,\\OLnt.fS exception handling What happet1s whet1 a tMethod you warttto call (probably it' a class you didt1~t write) is risky? " Let's say you want to call a method In a class that you didn't write _n, _ _ II , ~- , - 111 o _ I" • ,.u ~_ Q l your coc e you • That method does something risky, something that might not work at runtime _ , t_ _ f1, void moo () class you didn't write ( if (serverDown) axplode()i class you didn't write ) } ~ You need to know that the method you're calling is risky ~.- ' ~= I crass you didn't write you @ You then write code that can handle the failure if It does happen You need to be prepared, just in case your co e you you ar e her e ~ 319 When things mIght go wrong Methods iN Java ose except/om: to tell the ca/liM9 code, ·Sotttethlftg Bad HappeHed I failed;' mechan~m", Java's exception.handliog a dean, well·lighted way to handle "exceptional shuations' that pop up at runtime;;t lets}'OuPOl all your error.handling code in one easy-to-readplace It's based On you knowingthat the method you're calhng;, risky (i.e that the method mighl gene,",e an exception), SO thar you can write code '0 deal w;th that pos,,;bmty l£}'Oo k_ you might get an exception when you calla ParUcuJar the exception method, you can be fmP- for-poss;bly even '''-from-the problem thaI caused So, how do}'Ou know if a method throws an eXception! You find a throw clause;n the risky method's declaration The getSequencer () lIIethOd takes a risk It Can fail at runtlllle So It lIIust 'declare' the risk you take When you call it eee @ MldlSvslem (Java PIa((orm SE vl.4.01 • Sll>p h ck t.~ Rdrt:Sh It f Hl>rrIC _ i_ A~Ic>RlI - -~ getSe-quencer L~~~I ~ pUb lic ~ t a t i c '~C'all(l n s" r t ac.~ _ _ 9·ts~ h r o~ ( Mldll1o Av:ll h l~ Ex£~Rt l Qn l Oblains the default sequencer ~ Rtturns: / g ~~:\':tOlJrq ~ VOiceSlalys 5' ExcepUons ,,1 320 chapte r 11 I cer the defaultsequcn I availabledue Throws:d HJo.) y a I J b• l"ExcC p rl on _ f the seq I

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

Từ khóa liên quan

Mục lục

  • 10 Numbers Matter: math, formatting, wrappers, and statics

  • 11 Risky Behavior: exception handling

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

Tài liệu liên quan