Java学习记录:对象数组

package com.sr.mll;
class StudentId {
    private String name;
    private int id;
    private String classroom;
    private int totalScore;
    private int ranking;
    public StudentId () {

    }
    public StudentId (String name, int id, String classroom) {
        this.name = name;
        this.id = id;
        this.classroom = classroom;
    }
    public StudentId (String name, int id, String classroom, int totalScore, int ranking) {
        this(name, id, classroom);
        this.totalScore = totalScore;
        this.ranking = ranking;
    }

    public String getName() {
        return name;
    }

    public int getId() {
        return id;
    }

    public String getClassroom() {
        return classroom;
    }

    public int getTotalScore() {
        return totalScore;
    }

    public int getRanking() {
        return ranking;
    }
    public void printId () {
        String str = this.name + "\t\t" + this.id + "\t" + this.classroom;
        System.out.println(str + "\t" + this.totalScore + "\t\t" + this.ranking);
    }
}
class StudentList {
    public StudentList () {

    }
    int number = 0;
    StudentId[] list = new StudentId[60];       //定义并初始化对象数组
    public void addNumber (StudentId stu) {
        list[number] = stu;
        number++;
    }
    public void print () {
        System.out.println("姓名" + "\t\t学号" + "\t\t班级" + "\t\t总分" + "\t\t排名");
        for (int i = 0; i < number; i++) {
         list[i].printId();              
        }
    }
}

public class Zh05 {
    public static void main(String[] args) {
        StudentList std = new StudentList();
        StudentId lb = new StudentId("刘备",2024022,"C666",579,134);
        StudentId zs = new StudentId("张三",2024025,"C667",555,200);
        StudentId ls = new StudentId("李四",2024038,"C659",498,312);
        std.addNumber(lb);       //将对象存入数组
        std.addNumber(zs);
        std.addNumber(ls);
        std.print();
    }
}

以上是自己写的一段代码,记录学习情况,如有不对,请多见谅。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值