封装(encapsulation
)
面向对象的三大基本特征
什么是封装?
封装(encapsulation)就是把抽象出的数据属性
和对数据的操作方法
封装在一起,数据被保护在内部,程序的其它部分只有通过被授权的操作方法
,才能对数据进行操作。
好处
封装的实现(三步走)
-
(1)将属性进行私有化private
【不能直接修改属性】
-
public void setxxx(类型,参数名) {属性 = 参数名;
}
-
(3)提供一个公共的(public
)get 方法,用于获取属性的值
public void setxxx() {return xx;
}
两个案例快速入门
案例一
定义一个 person 类,要求不能随便查看人的年龄,工资这两个隐私,并对设置的年龄进行合理的验证,年龄合理就是设置,否则给默认年龄,必须在 1 - 120 岁之间,工资不能随便查看
package encapsulation;import java.util.Scanner;public class test1 {public static void main(String[] args) {person p = new person();p.setName("jacskon");p.setAge(18);p.setSalary(10000);p.getSalary();p.getinfo();person p1 = new person("1234567",0,1);p1.getinfo();}
}class person {public String name;private int age;private double salary;public person() {}public person(String name, int age, double 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("你输入的名字:" + name +"不合理\n提醒:名字长度需要在2-6个字符之间,给你默认姓名---用户01");this.name = "用户01";}}public int getAge() {return age;}public void setAge(int age) {if (age >= 1 && age <= 120) {this.age = age;} else {System.out.println("你的年龄不合理,给你默认年龄----18");this.age = 18;}}public double getSalary() {int cnt = 1;while(true){System.out.println();System.out.println("这是第" + cnt + "次身份验证");System.out.print("输入密码验证身份:");Scanner input = new Scanner(System.in);String password = input.next();if (password.equals("123")) {System.out.println();System.out.println("身份验证通过~~");System.out.println("您的薪水是:" + salary);break;} else {System.out.println();System.out.print("身份验证失败,重新输入密码");System.out.println();cnt += 1;}if(cnt == 4){System.out.println("三次密码错误,身份验证失败,账户锁定!");break;}}return 0;}public void setSalary(double salary) {this.salary = salary;}public void getinfo() {System.out.println();System.out.println("=====个人信息如下=====");System.out.println("姓名:" + name);System.out.println("年龄:" + age);System.out.println("工资:" + salary);}
}你输入的名字:jacskon不合理
提醒:名字长度需要在2-6个字符之间,给你默认姓名---用户01这是第1次身份验证
输入密码验证身份:123身份验证通过~~
您的薪水是:10000.0=====个人信息如下=====
姓名:用户01
年龄:18
工资:10000.0
你输入的名字:1234567不合理
提醒:名字长度需要在2-6个字符之间,给你默认姓名---用户01
你的年龄不合理,给你默认年龄----18=====个人信息如下=====
姓名:用户01
年龄:18
工资:1.0
案例二
创建程序,在其中定义两个类:Account 和 AccountTest 类会封装 Java 的封装性。
1. Account 类要求具有以下属性:
-
姓名(长度为 2 至 3 位或 4 位)
-
-
密码(必须是六位,如果不足,则给出提示信息,并给默认值【程序员自己定】)
2. 通过 setXxx 的方法给 Account 的属性赋值。
3. 在 AccountTest 中测试。
package encapsulation;import java.util.Scanner;public class test2 {public static void main(String[] args) {accounttest test = new accounttest();test.init();System.out.println("\n");System.out.println("=====这里是acoount传入数据的返回结果");account test1 = new account();test1.setName("jackson");test1.setBalance(10);test1.setPassword("123");test1.getinfo();}
}class account {String name;double balance;String password;public account() {}public account(String name, double balance, String password) {setName(name);setBalance(balance);setPassword(password);}public String getName() {return name;}public void setName(String name) {if (name.length() >= 2 && name.length() <= 4) {this.name = name;} else {System.out.println("输入的名字长度应在2-4个字符,给你默认姓名:01");this.name = "01";}}public double getBalance() {return balance;}public void setBalance(double balance) {if (balance >= 20) {this.balance = balance;} else {System.out.println("余额不足20,默认余额设为0");}}public String getPassword() {return password;}public void setPassword(String password) {while (true) {if (password.length() == 6) {System.out.println("密码设置成功~~");this.password = password;break;} else {System.out.print("密码长度需要六位,请重新输入:");Scanner input = new Scanner(System.in);password = input.next();System.out.println();}}}public void getinfo(){System.out.print("名字:" + name + "\n余额:" + balance + "\n密码:" + password);}
}class accounttest {public void init(){System.out.println("====这里是accounttest传入数据的返回结果");account person = new account();person.setName("jack");person.setBalance(25);person.setPassword("123456");person.getinfo();}
}
====这里是accounttest传入数据的返回结果
密码设置成功~~
名字:jack
余额:25.0
密码:123456=====这里是acoount传入数据的返回结果
输入的名字长度应在2-4个字符,给你默认姓名:01
余额不足20,默认余额设为0
密码长度需要六位,请重新输入:123456密码设置成功~~
名字:01
余额:0.0
密码:123456
案例二充分体会了在不同类中调用方法