Java学习记录:类之间的聚合关系

package comm.lbb.jcc;
class Person2 {           
    private String name;
    private int age;
    public Person2 () {

    }
    public Person2 (String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}
class Student1 extends Person2 {
    private int id;
    public Student1 () {

    }
    public Student1 (String name, int age, int id) {
        super(name, age);
        this.id = id;
    }

    public int getId() {
        return id;
    }
    public void Msg () {
        System.out.println("姓名:" + this.getName() + "\t年龄:" + this.getAge() + "\t\t学号:" + this.id);
    }
}
class School {
    private String name;
    private Student1[] stu;
    public School () {

    }
    public School (String name, Student1[] stu) {
        this.name = name;
        this.stu = stu;
    }

    public String getName() {
        return name;
    }

    public Student1[] getStu() {
        return stu;
    }
    public void print () {
        System.out.println("学校:" + this.name + "\n学生:");
        for (Student1 x: this.stu) {
            x.Msg();
        }
    }
}
public class Ch04 {
    public static void main(String[] args) {
        Student1[] stu = new Student1[6];
        stu[0] = new Student1("刘备",18,2024001);
        stu[1] = new Student1("张三",17,2024002);
        stu[2] = new Student1("张飞",19,2024003);
        stu[3] = new Student1("关羽",16,2024004);
        stu[4] = new Student1("李四",18,2024005);
        stu[5] = new Student1("悟空",17,2024006);
        School sh = new School("哈达学院",stu);
        sh.print();

    }
}

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

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值