public class Person {
String name;
int hight;
int age;
String sex;
//构造函数
public Person(String name, int hight, int age, String sex) {
super();
this.name = name;
this.hight = hight;
this.age = age;
this.sex = sex;
}
public void Love(Person...persons)//便于理解,你可以此处Person是相当于数组的类型,persons相当于数组的名称
{
for(int i = 0; i < persons.length;i ++)
{
System.out.println(this.name+"喜欢"+persons[i].name );
}
}
//无参构造方法
Person()
{
}
public static void main(String[] args) {
Person Chorject = new Person();//此处要注意调用的是无参构造函数,类里面必须要有无参构造方法
Person Guan = new Person();
Person Shang = new Person();
//初始化
Chorject.name = "小嘉嘉";
Guan.name = "小官官";
Shang.name = "小单单";
//单个对象调用可变数量的参数
Chorject.Love(Guan);
System.out.println();
//两个个对象调用可变数量的参数(两次调用构造函数)
Chorject.Love(Guan,Shang);
System.out.println();
}
}
Java类可变数量参数
最新推荐文章于 2024-04-18 21:36:52 发布