构造赋值,数组输出。

class Student3{

   private String name;//成员变量

   private int age;//成员变量

   public Student3(String name,int age){//创建两个参数的构造方法

      this.name=name;//赋初值

      this.age=age;//赋初值

   }

   public void getInfo(){//输出方法

      System.out.println("名字是="+name+" " +"年龄是="+age);

   }

}

public class Student2 {

   public static void main(String[] args) {

      Student3 stu1=new Student3("小米",25);//创建学生对象1

      Student3 stu2=new Student3("阿里",24);//创建学生对象2

      Student3 stu[]=new Student3[2];//创建学生数组

      stu[0]=stu1;

      stu[1]=stu2;

      for(int i=0;i<stu.length;i++){//循环输出

        stu[i].getInfo();

      }

   }

}

猜你喜欢

转载自blog.csdn.net/qq_39822872/article/details/82388816
今日推荐