当前位置: 首页 > article >正文

JAVA类和对象练习

 ①定义类Teacher,属性name,subject,行为teach,构造函数初始化两个属性。

 ②定义类Student,属性name,grade,行为study,构造函数初始化两个属性。

 ③定义类ClassRoom,属性Teacher,Student[],构造函数初始化两个属性,行为showMembers()输出教师和学生的名字

class Teacher {
    String name;
    String subject;

    public Teacher(String name, String subject) {
        this.name = name;
        this.subject = subject;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public void teach() {
        System.out.println(this.name + " is teaching " + this.subject);
    }
}

class Student {
    String name;
    int grade;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    public Student(String name, int grade) {
        this.name = name;
        this.grade = grade;
    }

    public void study() {
        System.out.println(this.name + " is studying in grade " + this.grade);
    }
}

class ClassRoom {
    Teacher teacher;
    Student[] students;

    public ClassRoom(Teacher teacher, Student[] students) {
        this.teacher = teacher;
        this.students = students;
    }

    public void showMembers() {
        System.out.print(this.teacher.getName()+",");
        int size = this.students.length;
        for (int i = 0; i < size; i++) {
            System.out.print(this.students[i].getName());
            if (i < size - 1) {
                System.out.print(",");
            }
        }
        System.out.println();
    }
}

public class Test {
    public static void main(String[] args) {
        Teacher teacher = new Teacher("Viktor Axelsen", "Badminton");
        Student[] students = new Student[2];
        students[0] = new Student("Shi Yuqi", 1);
        students[1] = new Student("Lee Zijia", 2);
        ClassRoom classRoom = new ClassRoom(teacher, students);
        classRoom.showMembers();
    }
}

注意下输出格式,可以按需更改。 


http://www.kler.cn/a/469637.html

相关文章:

  • 计算机网络学习
  • 防止密码爆破debian系统
  • 周记-Repeater中的children和item区别
  • Leetcode 3414. Maximum Score of Non-overlapping Intervals
  • 数据挖掘教学指南:从基础到应用
  • Linux中rsync命令使用
  • Vue2: table加载树形数据的踩坑记录
  • 【数据结构05】排序
  • centos,789使用mamba快速安装R及语言包devtools
  • MySQL 的事务与多版本并发控制(MVCC)的那些事
  • Synthesia技术浅析(二):虚拟人物视频生成
  • 为什么HTTP请求后面有时带一个sign参数(HTTP请求签名校验)
  • SAP SD学习笔记26 - 贩卖契约(框架协议)的概要,基本契约 - 数量契约
  • Ubuntu创建python虚拟环境
  • 太速科技-747-4通道 500Msps 12bit 直流耦合 PCIe采集卡
  • 您有一份 Apache Flink 社区年度报告请查收~
  • Node.js 模块系统
  • Linux-----进程处理(execve执行可执行文件)
  • 青少年编程与数学 02-006 前端开发框架VUE 05课题、使用模板
  • 结合 nc 工具利用笑脸漏洞(Smile Bug)攻击 Metasploitable2 Linux
  • javadoc命令的简单使用
  • RabbitMQ通过代码创建交换机和队列
  • 分布式任务调度xxl-job入门案例
  • Chapter 1 Understanding Large Language Models
  • BUG解决:postman可以请求成功,但Python requests请求报403
  • kafka Broker专题