— Scott McNealy, Sun Microsystems Chairman, President and ceo


public abstract class Animal {



Download 40,57 Mb.
Pdf ko'rish
bet250/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   246   247   248   249   250   251   252   253   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

public abstract class Animal {
private String name;
public String getName() {
return name;
}
public Animal(String theName) {
name = theName;
}
}
A getter method that 
Hippo inherits
The constructor that 
takes the name and assigns 
it the name instance 
variable 
All animals (including 
subclasses) have a name
public class Hippo extends Animal {
public Hippo(String name) {
super(name);
}
}
public class MakeHippo {
public static void main(String[] args) {
Hippo h = new Hippo(“Buffy”);
System.out.println(h.getName());
}
}
Hippo constructor takes a name
it sends the name up the Stack 
to 
the Animal constructor
The Animal part of 
me needs to know my name, 
so I take a name in my own 
Hippo constructor, then pass 
the name to super()
Make a Hippo, passing the 
name “Buffy” to the Hippo 
constructor. Then call the 
Hippo’s inherited getName()
File Edit Window Help Hide
%java MakeHippo
Buffy


256
 
chapter 9
Invoking one overloaded constructor 
from another
What if you have overloaded constructors that, with 
the exception of handling different argument types, 
all do the same thing? You know that you don’t want 
duplicate code sitting in each of the constructors (pain 
to maintain, etc.), so you’d like to put the bulk of the 
constructor code (including the call to super()) in only 
one of the overloaded constructors. You want whichever 
constructor is fi rst invoked to call The Real Constructor 
and let The Real Constructor fi nish the job of 
construction. It’s simple: just say this(). Or this(aString)
Or this(27, x). In other words, just imagine that the 
keyword this is a reference to the current object
You can say this() only within a constructor, and it must 
be the fi rst statement in the constructor!
But that’s a problem, isn’t it? Earlier we said that 
super() must be the fi rst statement in the constructor.
Well, that means you get a choice. 
Every constructor can have a call to super() 
or this(), but never both!

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   246   247   248   249   250   251   252   253   ...   555




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish