设计User类

设计一个用户类(User),类中变量有用户名、口令和用户个数的变量(赋值、为用户名和口令赋值),获取和设置口令的方法和返回类信息的方法

public class User {
private String user_name;  //用户名称
private String user_password;//用户口令
private static int user_count=0; //统计用户个数
//设置getter、setter方法

public String getUser_name() {
    return user_name;
}
public void setUser_name(String user_name) {
    this.user_name = user_name;
}
public String getUser_password() {
    return user_password;
}
public void setUser_password(String user_password) {
    this.user_password = user_password;
}
public static int getUser_count() {
    return user_count;
}
public static void setUser_count(int user_count) {
    User.user_count = user_count;
}

//定义构造方法(无参、单参、全部参数)
public User() {
    this("无名氏","111111");
}

public User(String user_name) {
    this(user_name,"111111");
}
public User(String user_name,String user_password) {
    this.user_name =user_name;
    this.user_password = user_password;
    user_count++;
}
//获取用户个数
public static String  getCount() {
    return "用户个数为:"+user_count;
            
}
//获取用户相关信息
public String getInfo() {
    return "【用户信息】用户名:"+user_name+"用户口令:"+user_password;
}
//主方法测试
public static void main(String[] args) {
    User userA = new User();
    User userB = new User("小花");
    User userC = new User("大花","111111");
    System.out.println(userA.getInfo());
    System.out.println(userB.getInfo());
    System.out.println(userC.getInfo());
    System.out.println(User.getCount());
}
}


 

猜你喜欢

转载自blog.csdn.net/qq_41663470/article/details/112749534