• Java学习 --- 面向对象三大特征之封装


    目录

    一、访问修饰符

     二、封装

    三、参考案例

    一、访问修饰符

    Java提供了四种访问控制修饰符号,用于控制方法和属性(成员变量)的访问权限(范围)。

    1、公开的(public):对外公开。

    2、受保护(protected):对子类和同一包中的类公开

    3、默认:没有修饰符号,向同一个包的类公开

    4、私有(private):只有类本身可以访问,不对外公开

     二、封装

    把抽象出的数据[属性]和对数据的操作[方法]封装在一起,数据被保护在内部,程序的其它部分只有通过被授权的操作[方法],才能对数据进行操作。

    封装的好处:1、隐藏实现细节  2、可以对数据进行验证,保证安全合理。

    封装的实现步骤:1、将属性进行私有化(private) 2、提供一个公共的(public)set方法,用于对属性判断并赋值  3、提供一个公共的(public)get方法,用于获取属性的值。

    示例代码:

    1. public class Encapsulation01 {
    2. public static void main(String[] args) {
    3. Person person = new Person();
    4. person.setName("小明");
    5. person.setAge(1200);
    6. person.setSalary(5600);
    7. person.info();
    8. }
    9. }
    10. class Person{
    11. public String name;
    12. private int age;
    13. private double salary;
    14. public String getName() {
    15. return name;
    16. }
    17. public void setName(String name) {
    18. if (name.length()>= 2 && name.length() <= 6){
    19. this.name = name;
    20. }else {
    21. System.out.println("你的名字过长或过短");
    22. }
    23. }
    24. public int getAge() {
    25. return age;
    26. }
    27. public void setAge(int age) {
    28. //加入判断逻辑
    29. if (age >= 1 && age <= 120){
    30. this.age = age;
    31. }else {
    32. System.out.println("请输入正确的年龄");
    33. }
    34. }
    35. public double getSalary() {
    36. return salary;
    37. }
    38. public void setSalary(double salary) {
    39. this.salary = salary;
    40. }
    41. //提供一个方法对外展示综合信息
    42. public void info(){
    43. System.out.println("姓名:"+name+"\t年龄:"+age+"\t工资"+salary);
    44. }
    45. }

    构造器:在给属性赋值时能通过构造器绕过

    解决办法:

    1. public Person(String name, int age, double salary) {
    2. //在构造器中使用set方法
    3. setName(name);
    4. setAge(age);
    5. setSalary(salary);
    6. }

    三、参考案例

    创建程序,在其中定义两个类:Account和AccountTest类体会Java的封装性。

    1. Account类要求具有属性:姓名(长度为2位3位或4位)、余额(必须>20)、密码(必须是六位),如果不满足,则给出提示信息,并给默认值。

    2.通过setXxx的方法给Account的属性赋值。

    3.在AccountTest中测试。

    Account类

    1. public class Account {
    2. private String name;
    3. private double balance;
    4. private String password;
    5. public Account() {
    6. }
    7. //在构造器中调用set方法,防止跳过属性赋值检查
    8. public Account(String name, double balance, String password) {
    9. setName(name);
    10. setBalance(balance);
    11. setPassword(password);
    12. }
    13. public String getName() {
    14. return name;
    15. }
    16. public void setName(String name) {
    17. if (name.length()>= 2 && name.length() <= 4){
    18. this.name = name;
    19. } else {
    20. System.out.println("请输入2~4长度的名字");
    21. this.name = "张三";
    22. }
    23. }
    24. public double getBalance() {
    25. return balance;
    26. }
    27. public void setBalance(double balance) {
    28. if (balance > 20){
    29. this.balance = balance;
    30. } else {
    31. System.out.println("余额不足");
    32. }
    33. }
    34. public String getPassword() {
    35. return password;
    36. }
    37. public void setPassword(String password) {
    38. if (password.length() == 6) {
    39. this.password = password;
    40. }else {
    41. System.out.println("密码长度为六位");
    42. this.password = "000000";
    43. }
    44. }
    45. }

    AccountTest类

    1. public class AccountTest {
    2. public static void main(String[] args) {
    3. Account account = new Account("老王", 100, "666666");
    4. System.out.println(account.getName());
    5. System.out.println(account.getBalance());
    6. System.out.println(account.getPassword());
    7. }
    8. }

  • 相关阅读:
    ik分词器
    微信小程序开发学习6(基础加强之使用npm包和全局数据共享及分包【Tab底栏案例改进】)
    tp6+vue-elementui-admin实现前后端权限分离框架
    【单元测试】Junit 4(二)--eclipse配置Junit+Junit基础注解
    【推理引擎】从源码看ONNXRuntime的执行流程
    Android ViewBinding和DataBinding功能作用区别
    基于Echarts实现可视化数据大屏北斗车联网大数据服务平台首页面
    TSINGSEE青犀视频AI算法助力构建城市市容·街面秩序管理解决方案
    ARM——综合作业
    win10系统启用win32长路径
  • 原文地址:https://blog.csdn.net/qq_46093575/article/details/126770591