— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet89/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   85   86   87   88   89   90   91   92   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

Song t2 = new Song();
t2.setArtist(“Travis”);
t2.setTitle(“Sing”);
Song s3 = new Song();
s3.setArtist(“Sex Pistols”);
s3.setTitle(“My Way”);
five instances 
of class Song


you are here
4
methods 
use 
instance variables
73
The size affects the bark
A small Dog’s bark is different from a big Dog’s bark.
The Dog class has an instance variable size, that the 
bark() method uses to decide what kind of bark sound 
to make.
Dog
size
name
bark()
class Dog {
int size;
String name;
void bark() {
if (size > 60) {
System.out.println(“Wooof! Wooof!”);
} else if (size > 14) {
System.out.println(“Ruff! Ruff!”);
} else {
System.out.println(“Yip! Yip!”);
}
}
}
class DogTestDrive {
public static void main (String[] args) {
Dog one = new Dog();
one.size = 70;
Dog two = new Dog();
two.size = 8;
Dog three = new Dog();
three.size = 35;
one.bark();
two.bark();
three.bark();
}
}
File Edit Window Help Playdead
%java DogTestDrive
Wooof! Wooof!
Yip! Yip!
Ruff! Ruff!


74
 
chapter 4
00000011
int
method 
parameters
You can send things to a method
Just as you expect from any programming language, you can pass values into 
your methods. You might, for example, want to tell a Dog object how many 
times to bark by calling: 
d.bark(3);
Depending on your programming background and personal preferences
you might use the term arguments or perhaps parameters for the values passed 
into a method. Although there are formal computer science distinctions that 
people who wear lab coats and who will almost certainly not read this book, 
make, we have bigger fi sh to fry in this book. So you can call them whatever 
you like (arguments, donuts, hairballs, etc.) but we’re doing it like this:

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   85   86   87   88   89   90   91   92   ...   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