封装
关键字:private
隐藏类的内部细节,提供外部访问的接口,叫做封装。
封装使用get ,set方法。
this: 表示当前对象。
类中拥有构造方法。构造方法是用来构建对象。
构造方法是: 方法名称与类名相同,无返回值的方法。
默认一个类中,有一个无参构造方法。不写也有。
当手动提供了一个有参构造方法。系统将不再提供无参构造方法。~
// 无参构造方法。
public User() { }
// 有参构造方法。
public User(String id, String username, String password) { this.id = id;
this.username = username;
this.password = password;
}
// 重载构造方法。
public User(String username,String password){ this.username = username;
this.password = password;
}; ~