JAVA编程思想第四版英文原版习题答案. pdf原版的

源代码在线查看: ex1.java

软件大小: 4845 K
上传用户: liu4052032
关键词: JAVA 编程 英文
下载地址: 免注册下载 普通下载 VIP

相关代码

				// holding/Ex1.java
				// TIJ4 Chapter Holding, Exercise 1, page 394
				/* Create a new class called Gerbil with an int gerbilNumber that's 
				* initialized in the constructor. Give it a method called hop() that displays
				* which gerbil number that is, and that it's hopping. Create an ArrayList and 
				* add Gerbil objects to the List. Now use the get() method to move through
				* the List and call hop() for each Gerbil.
				*/
				import java.util.*;
				
				class Gerbil {
					private int gerbilNumber;
					public Gerbil(int i) {
						gerbilNumber = i;
					}
					public void hop() {
						System.out.println("Gerbil " + gerbilNumber + " hops");
					}
				}
				
				public class Ex1 {
					public static void main(String[] args) {
						ArrayList gerbils = new ArrayList();
						for(int i = 0; i < 10; i++)
							gerbils.add(new Gerbil(i));
						for(int i = 0; i < 10; i++) 
							gerbils.get(i).hop();
						// or, alternatively, using foreach syntax:
						for(Gerbil g : gerbils)
							g.hop();
					}
				}			

相关资源