面向对象 练习题4

来源:互联网 发布:淘宝助理怎么使用5.5 编辑:程序博客网 时间:2024/05/17 06:31
package com.lrq;/** *  * @author 李瑞琦 * 4.构造方法与重载:定义一个网络用户类, * 要处理的信息有用户 ID、用户密码、 email 地址。 * 在建立类的实例时把以上三个信息都作为构造函数的参数输入,  * 其中用户 ID 和用户密码时必须缺省时 email地址是用户 ID 加上字符串"@gameschool.com"。 * */public class TestPeople {    public static void main(String[] args) {        // 用户名跟密码缺省时        TestPeople people = new TestPeople("张三", "zhangsan");        people.show();        // 用户名跟密码不缺省时        TestPeople people2 = new TestPeople("李四", "lisi", "@163.com");        people2.show();    }    // 私有属性id,password,email    private String id;    private String password;    private String email;    // 有参构造器给成员变量初始化    public TestPeople(String id, String password, String email) {        this.id = id;        this.password = password;        this.email = email;    }    public TestPeople(String id, String password) {        this.id = id;        this.password = password;        this.email = this.id + "@gameschool.com";    }    public void show() {        System.out.println("账号:" + id);        System.out.println("密码:" + password);        System.out.println("邮箱:" + email);    }}
原创粉丝点击