【学校实验】编码实现一个地址簿类(AddressBook),通过getAllInfo()输出,实现另一个类(TestAddressBook)该类存有主方法

题目:

【学校实验】编码实现一个地址簿类(AddressBook),通过getAllInfo()输出,实现另一个类(TestAddressBook)该类存有主方法_第1张图片

代码:

1.AddressBook代码:

public class AddressBook {
    private String name;
    private String address;
    private String tel;

    //无参构造函数
    public AddressBook() {
    }

    //满参构造函数
    public AddressBook(String name, String address, String tel) {
        this.name = name;
        this.address = address;
        this.tel = tel;
    }

    //getter方法和setter方法
    public String getName() {
        return name;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }


    public void getAllInfo(){
        System.out.println("name is : " + name);
        System.out.println("address is : " + address);
        System.out.println("tel is : " + tel);
    }
}

2.TestAddressBook类代码:

public class TestAddressBook {
    public static void main(String[] args) {
        AddressBook zhangsan_addr = new AddressBook();
        zhangsan_addr.setName("张三");
        zhangsan_addr.setAddress("珠海");
        zhangsan_addr.setTel("159196xxxxx");
        System.out.println(zhangsan_addr.getAddress());
        System.out.println(zhangsan_addr.getName());
        System.out.println(zhangsan_addr.getTel());
        zhangsan_addr.getAllInfo();
    }
}

运行结果:

【学校实验】编码实现一个地址簿类(AddressBook),通过getAllInfo()输出,实现另一个类(TestAddressBook)该类存有主方法_第2张图片

你可能感兴趣的:(JavaSE入门实验,java,面向对象编程)