2.设计一个 User 类,其中包括 ID 号、用户名、性别、年龄、密码、忘记 提醒等属性,且初始密码均为 111111。另外,还有注册、密码更改、用 户基本信息显示 3 个方法,构造方法中实现 ID 号的递增。编写应用程 序完成上述 User 类的测试。
1条回答 默认 最新
- 我是一只小小小小小鸟 2017-12-04 01:01关注
User类
public class User { private static long id=0; private String name; private String sex; private int age; private String password; private String forget; public User() { super(); } public User(String name, String sex, int age, String password, String forget) { super(); this.id = getId()+1; this.name = name; this.sex = sex; this.age = age; this.password = password; this.forget = forget; } public static long getId() { return id; } public static void setId(long id) { User.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getForget() { return forget; } public void setForget(String forget) { this.forget = forget; } public User addUser(String name, String sex, int age, String password, String forget) { User user = new User(name, sex, age, password, forget); System.out.println(name+"创建成功"); return user; } public void changePasswordById(long id,String newPassword){ if (this.id==id) { this.password=newPassword; System.out.println("密码修改成功"); } } public void showUserMsg(){ System.out.println("用户Id"+this.id+"\t"+"用户姓名:"+this.name+"\t" +"用户性别"+this.sex+"\t"+"用户年龄"+this.age+"\t"+"用户密码:"+this.password); } }
Test类
public class Test { public static void main(String[] args) { User user = new User("张三", "男", 18, "123", "提醒"); user.showUserMsg(); User user2 = user.addUser("李四", "女", 20, "456", "提醒"); user2.showUserMsg(); user.changePasswordById(1, "789"); user.showUserMsg(); } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
- ¥15 求daily translation(DT)偏差订正方法的代码
- ¥15 js调用html页面需要隐藏某个按钮
- ¥15 ads仿真结果在圆图上是怎么读数的
- ¥20 Cotex M3的调试和程序执行方式是什么样的?
- ¥20 java项目连接sqlserver时报ssl相关错误
- ¥15 一道python难题3
- ¥15 牛顿斯科特系数表表示
- ¥15 arduino 步进电机
- ¥20 程序进入HardFault_Handler