JAVA创建类问题
设计一个User类,其中包括用户名、口令等属性以及构造方法(至少重载2个)、获取和设置口令的方法、显示和修改用户名的方法等。编写应用程序测试User类。...
设计一个User类,其中包括用户名、口令等属性以及构造方法(至少重载2个)、获取和设置口令的方法、显示和修改用户名的方法等。编写应用程序测试User类。
展开
2个回答
展开全部
class User {
private String userName;
private String password;
public User() {
}
public User(String userName) {
this();
this.userName = userName;
}
public User(String userName, String password) {
this(userName);
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void show(){
System.out.println("用户名为:\t"+this.getUserName());
System.out.println("密 码为:\t"+this.getPassword());
}
public String updateName(String name){
this.setUserName(name);
return this.userName;
}
}
public class UserTest{
public static void main(String args[]){
User user1=new User();
user1.setUserName("张三");
user1.setPassword("110");
user1.show();
System.out.println();
User user2=new User("lisi","ja123");
user2.show();
System.out.println("修改后的用户名为:");
user2.updateName("王五");
user2.show();
}
}
private String userName;
private String password;
public User() {
}
public User(String userName) {
this();
this.userName = userName;
}
public User(String userName, String password) {
this(userName);
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void show(){
System.out.println("用户名为:\t"+this.getUserName());
System.out.println("密 码为:\t"+this.getPassword());
}
public String updateName(String name){
this.setUserName(name);
return this.userName;
}
}
public class UserTest{
public static void main(String args[]){
User user1=new User();
user1.setUserName("张三");
user1.setPassword("110");
user1.show();
System.out.println();
User user2=new User("lisi","ja123");
user2.show();
System.out.println("修改后的用户名为:");
user2.updateName("王五");
user2.show();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询