Java方法的不固定参数

来源:互联网 发布:ios绅士软件 编辑:程序博客网 时间:2024/06/16 05:11

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运行效果图


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

0 0
原创粉丝点击