目录
一、封装基本介绍
面向对象编程有三大特征:封装、继承和多态。
封装(encapsulation)就是把抽象出的数据【属性】和对数据的操作【方法】封装在一起,数据被保护在内部,程序的其他部分只有通过被授权的操作【方法】,才能对数据进行操作。
二、封装的理解和好处
1)隐藏实现细节:方法(连接数据库)<--调用(传入参数。。。)
2)可以对数据进行验证,保证安全合理‘
三、封装的实现步骤
1)将属性进行私有化(private)【不能直接修改属性】
2)提供一个公共的(public)set的方法,用于对属性的判断并赋值
public void setxxx(类型 参数名){
//加入数据验证的业务逻辑
属性=参数名
}
3)提供一个公共的(public)get 方法,用于获取属性的值
public 数据类型 getxxx (){
//权限判断
return xx;
}
四、快速入门案例
1)
public class encapsulation01 {
public static void main(String[] args) {
Person person = new Person();
Person rose = new Person("rose", 20, 30000);
person.setAge(45);
person.setName("jack");
person.setSalary(30000);
System.out.println(person.info());
System.out.println(rose.info());
}
}
class Person{
public String name;
private int age;
private double salary;
public Person() {
}
public Person(String name, int age, double salary) {
// this.name = name;
// this.age = age;
// this.salary = salary;
setName(name);
setAge(age);
setSalary(salary);
}
public String getName() {
return name;
}
public void setName(String name) {
//判断字符串长度
if(name.length()>=2&&name.length()<=6){
this.name = name;
}else{
System.out.println("名字长度有异常,请重新输入");
this.name = "无名的人";
}
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age>=1&&age<=150){
this.age = age;
}else {
System.out.println("你输入的年龄不对,需要在1-150以内");
this.age = 18;//给一个默认的年龄
}
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String info(){
return "姓名="+name+"年龄="+age+"薪水="+salary;
}
}
2)将构造器和setxxx结合
//
有三个属性的构造器
public Person(String name, int age, double salary) {
// this.name = name;
// this.age = age;
// this.salary = salary;
//
我们可以将
set
方法写在构造器中,这样仍然可以验证
setName(name);
setAge(age);
setSalary(salary);
}
五、课下练习
题目要求:创建程序,在其中定义两个类,Account.java和AccountTest.java类,体验java的封装性
1、Account 类要求具有属性,姓名(长度为两位三位或者四位)、余额(必须>20)密码(必须是六位),如果不满足,则给出提示信息,并给默认值(程序员自己设置)
2、通过setxxx的方法给Account 的属性赋值
3、在AccountTest中测试
package com.encap;
//需求:1、Account类要求具有属性,姓名(长度为2.3.4位)、
//余额(必须满20)密码必须是六位,如果有不满足则给出提示信息
//并给出默认值
//2、通过setxxx的方法给Account的属性赋值
public class Account {
String name;
double balance;//余额
String passw;
public String getName() {
return name;
}
public void setName(String name) {
if(name.length()>=2&&name.length()<=4){
this.name = name;
}else{
System.out.println("你输入的名字有异常请重新输入,系统默认name=无名人");
this.name = "无名人";
}
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
if(balance>=20){
this.balance = balance;
}else{
System.out.println("你的余额仅剩"+balance+"不满20元,请及时充值");
this.balance = balance;
}
}
public String getPassw() {
return passw;
}
public void setPassw(String passw) {
if(passw.length()==6){
this.passw = passw;
}else{
System.out.println("密码格式有误,长度为6");
}
}
}
package com.encap;
public class AccountTest {
public static void main(String[] args) {
Account account = new Account();
account.setName("柴东升");
account.setBalance(100000.0);
account.setPassw("123456");
System.out.println("姓名="+account.name+"薪水="+account.balance+"密码="+account.passw);
}
}