封装和关键字private
封装
(将一些细节信息隐藏,对外界不可见)
1.方法是一种封装;
2.关键字private也是一种封装
关键字private的使用
创建一个学生类,其成员变量有姓名等信息,在定义成员变量时声明其为private类型的,则该成员变量在该类这之外只可以间接使用;
用setter——getter(布尔型变量为is)方法,其格式如上图所示。在进行setXxx(参数){}时,可以在{}内用if语句对传入的数据进行判断,从而剔除一些不合理的数据,使数据更为安全合理的使用。