2017年6月19日 星期一

第八章 馬牛叫聲

題目:延續上題,將上提的main()移動到MyAnimal的類別中,將程式另存為MyAnimal.java。在main()增加兩隻動物「牛 哞喔」、「馬 沒嘿」 ,並讓兩隻動物各叫一聲

public class Animal { //HomeWork0801

String type;
String voice;

void sound(){
System.out.println(type+"、"+voice);
}

}

public class MyAnimal {

public static void main(String[] args) {

Animal cat=new Animal();
Animal dog=new Animal();
Animal cattle=new Animal();
Animal horse=new Animal();

cat.type="cat";
cat.voice="mew";
dog.type="dog";
dog.voice="bark";
cattle.type="cattle";
cattle.voice="moo";
horse.type="horse";
horse.voice="neigh";


cat.sound();
dog.sound();
horse.sound();
cattle.sound();

}
}

沒有留言:

張貼留言