zoukankan      html  css  js  c++  java
  • 对象数组和for循环遍历输出学生的信息

    public class Student {
    private int no;
    private String name;
    private int age;
    public Student(int no, String name, int age) {
    // 带参数的构造方法
    this.name=name;
    this.no=no;
    this.age=age;
    }
    public int getNo() {
    return no;
    }
    public void setNo(int no) {
    this.no = no;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    }

    public class TestStudent {
    public static void main(String[] args) {
    // TODO 定义对象数组,使用for循环遍历输出每个学生的年龄
    Student[] stu = new Student[3];//创建对象数组
    //创建学生对象
    Student stu1 = new Student(1,"zhangsan",12);
    Student stu2 = new Student(1,"zhangsan",12);
    Student stu3 = new Student(1,"zhangsan",12);
    //把学生对象依次放入数组
    stu[0]=stu1;
    stu[1]=stu2;
    stu[2]=stu3;
    //遍历输出学生信息
    for (int i = 0; i < stu.length; i++) {
    Student s = stu[i];
    System.out.println(s.getNo()+" "+s.getName()+" "+s.getAge());;
    }
    }
    }

  • 相关阅读:
    php删除最后一个字符
    git删除远程分支
    lsof命令
    高效率的全组合算法(Java版实现)
    Java类变量和成员变量初始化过程
    pig命令行快捷键
    java的HashCode方法
    待学习
    长连接和短连接
    Hadoop学习之SecondaryNameNode
  • 原文地址:https://www.cnblogs.com/rainsnow/p/12187354.html
Copyright © 2011-2022 走看看