Java之类与对象(学生类封装)

本文介绍了一个简单的学生类管理系统的设计与实现,包括学生类的定义、入学检查、学生信息查询及更新等功能。通过实例化学生对象并进行操作,展示了如何使用面向对象的方法来管理学生信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*定义一个学生类,属性:姓名,年龄,性别,住址,班级,
1.方法:入学方法(参数为年龄):判断学生的年龄是否大于18岁,如果大于18岁则可以入学,并打印该学生的所有信息。
2.方法:查找方法(参数为姓名,学生数组),如果有该同学的姓名则显示查找成功,否则查找失败。
3.方法:修改方法(原姓名,学生数组)如果有该同学的姓名,则键盘输入要修改的项(比如要修改年龄,则输入年龄),最后显示修改后的该学生所有信息。
4.定义测试类,实例化学生类,定义一个含有10学生姓名的数组遍历添加,根据数组信息对三个方法进行调用。
*/


/**
 * 功能:封装学生类
 * 类名:Students
 * 作者:******
 * 时间:2018年7月31日下午9:31:21
 */
public class Students {
    private String name;
    private int age;
    private String sex;
    private String _class;
    private String address;
    public Students() {}
    public Students(String name, int age, String sex, String _class, String address) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        this._class = _class;
        this.address = address;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @param age the age to set
     */
    public void setAge(int age) {
        this.age = age;
    }
    /**
     * @param sex the sex to set
     */
    public void setSex(String sex) {
        this.sex = sex;
    }
    /**
     * @param _class the _class to set
     */
    public void set_class(String _class) {
        this._class = _class;
    }
    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @return the age
     */
    public int getAge() {
        return age;
    }
    /**
     * @return the sex
     */
    public String getSex() {
        return sex;
    }
    /**
     * @return the _class
     */
    public String get_class() {
        return _class;
    }
    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }
    
    public void show() {
        System.out.println("姓名:"+name+",年龄:"
                +age+",性别"+sex+",班级"+_class+",住址"+address);
    }
}
 

/**
 * 功能:管理功能类
 * 类名:StudentBiz
 * 作者:***
 * 时间:2018年7月31日下午9:33:07
 */
public class StudentBiz {
    Students[] stus=new Students[10];
    public void init(){
        stus[0] = new Students("张一", 21, "男", "一班", "烟台大学");
        stus[1] =new Students("张二", 22, "男", "二班", "烟台大学");
    }
    /**
     * 功能:增加
     * author:******
     * time:2018年7月31日下午9:33:07
     * @param stus
     * @param stu
     */
    public void addStu(Students stu) {
        for(int i=0;i<stus.length;i++) {
            if(stus[i]==null) {
                stus[i]=stu;
                break;
            }
        }
    }
    
    
    public void schhool() {
        for(int i=0;i<stus.length;i++) {
            if(stus[i]==null) 
                break;
            if(stus[i].getAge()>18) {
                System.out.println("可以入学,详细信息:");
                stus[i].show();
            }
        }
    }
    
    
    /**
     * 功能:查询
     * author:******
     * time:2018年7月31日下午9:33:07
     * @param stus
     */
    Scanner in=new Scanner(System.in);
    public void find() {
        System.out.print("请输入要查找的姓名:");
        String inName=in.next();
        
        boolean flag=false;
        for(Students stu1:stus) {
            if(stu1==null) 
                break;
            if(inName.equals(stu1.getName())) {
                flag=true;
                System.out.println("查找成功,此学生信息是:");
                stu1.show();
                break;
            }
        }
        if(!flag)
            System.out.println("查找失败");
    }
    /**
     * 功能:修改
     * author:******
     * time:2018年7月31日下午9:33:07
     * @param stu
     */
    public void update() {
        System.out.print("请输入要修改的学生姓名:");
        String inName=in.next();
        
        boolean flag=false;
        int i;
        for(i=0;i<stus.length;i++) {
            if(stus[i]==null) 
                break;
            if(inName.equals(stus[i].getName())) {
                flag=true;
                System.out.println("该生存在,此学生信息是:");
                stus[i].show();
                break;
            }
        }
        if(flag){
            System.out.print("输入修改项(姓名/年龄/性别/班级/住址):");
            String type=in.next();
            switch (type) {
            case "姓名":
                System.out.print("输入新的姓名:");
                stus[i].setName(in.next());
                break;
            case "年龄":
                System.out.print("输入新的年龄:");
                stus[i].setAge(in.nextInt());
                break;
            case "性别":
                System.out.print("输入新的性别:");
                stus[i].setSex(in.next());
                break;
            case "班级":
                System.out.print("输入新的班级:");
                stus[i].set_class(in.next());
                break;
            case "住址":
                System.out.print("输入新的住址:");
                stus[i].setAddress(in.next());
                break;
            default:
                break;
            }
            stus[i].show();
        }else {
            System.out.println("该生不存在");
        }
    }
    
    public void showAllIn() {
        for(int i=0;i<stus.length;i++) {
            if(stus[i]==null) 
                break;
            stus[i].show();
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值