3月11日 JavaWeb 周日

本文档展示了如何使用Java创建一个医生类,并通过构造函数初始化属性,包括姓名、年龄和工作地点。此外,还演示了如何将这些对象存储在ArrayList中,并采用for循环、foreach循环进行遍历输出。

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

/* hao
 * 2018-3-11 10:17:40
 * 月考题
 */
package demo01;

public class Doctor {

    //定义属性
    private String name;
    private int     age;
    private String where;

    //初始化构造方法
    public Doctor(String name,int age,String where){
        this.name = name;
        this.age = age;
        this.where = where;
    }

    //定义方法
    public void show(){
        System.out.println(name+" "+age+" "+where);
    }

    //封装属性
    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 String getWhere() {
        return where;
    }

    public void setWhere(String where) {
        this.where = where;
    }

}
/* hao
 * 2018-3-11 19:03:56
 * 增删改查
 */
package demo01;

import java.util.ArrayList;

public class Test {

    public static void main(String[] args) {
        //创建对象1
        Doctor d1 = new Doctor("张三",10,"山西");
        //创建对象2
        Doctor d2 = new Doctor("李四",11,"北京");
        //创建对象3
        Doctor d3 = new Doctor("王五",12,"天津");

        //添加到ArrayList对象中
        ArrayList<Object> a1 = new ArrayList<Object>();
        a1.add(d1);
        a1.add(d2);
        a1.add(d3);

        //for循环遍历输出医生的详细信息
        System.out.println("for循环遍历结果:");
        for(int i=0;i<a1.size();i++){
            System.out.println(a1.get(i));
        }
        System.out.println();
        //foreach遍历输出
        System.out.println("foreach循环遍历结果:");
        for(Object list : a1){
            System.out.println(list);
        }
        //迭代器遍历
        System.out.println("迭代器遍历结果:");


    }

}

仗义每从狗辈出,读书多是负心人。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值