Java方法的不固定参数


public class People4 {

	//形参,入参,不固定参数
	int speak(String name,int age,String ...hobbies){
		System.out.println("我叫"+name+",我今年"+age+"岁了");
		System.out.print("我的爱好:");
		for(String hobby:hobbies){
			System.out.print(hobby+" ");
		}
		// 获取爱好的长度
		int totalHobbies=hobbies.length;
		return totalHobbies;
	}
	
	public static void main(String[] args){
		People4 zhangsan=new People4();
		int n=zhangsan.speak("张三",23,"游泳","唱歌","跳舞");
		System.out.println("\n有"+n+"个爱好");
	}
}
Eclipse运行效果图


*这个老师讲过,又忘记了

你可能感兴趣的:(JavaSE)