面向对象编程-封装

封装介绍

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

封装的理解和好处

  1. 隐藏实现细节
  2. 可以对数据进行验证,保证安全合理

封装实现的步骤

  • 将属性进行私有化(不能直接修改属性)

  • 提供一个公共的(public)set方法,用于对属性判断并赋值

    • public void setXXX(类型 参数名) {
      // XXX表示某个属性
          //加入数据验证的业务逻辑
          属性 = 参数名;
      }
      
  • 提供一个公共的(public)get方法,用于获取属性的值

    • public 数据类型 getXXX(){
          //权限判断,xxx某个属性
          return xxx;
      }
      

快速入门案例

image-20221118101742062

package com.hsp.encap;
import java.util.Scanner;
public class EncapSulation {
    public static void main(String[] args) {
        Person person = new Person();
        person.setName("jack");
        person.setAge(30);
        person.setSalary(10000.3);
        person.getInfo();
    }
}
class Person {
    public String name;// 名字公开
    private int age; //age私有化
    private double salary; //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("名字的长度不对,需要2-6个字符");
            this.name = "普通用户";
        }
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if(age>=1 && age<=120){
            this.age = age;
        }
        else {
            System.out.println("年龄需要在1-120");
            this.age = 18;
        }
    }

    public void getSalary() {
        //增加对当前对象的权限判断
        System.out.println("想查看" + this.name + "的薪水,请输入密码: ");
        Scanner sc = new Scanner(System.in);
        int count = 3;
        boolean flag = true;
        while(flag) {
            int password = sc.nextInt();
            count--;
            flag = count != 0;
            if(password == 782157532){
                System.out.println(this.name + "的薪水是:" + this.salary);
                break;
            }else {
                System.out.println("输入密码错误");
            }

        }
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
    public void getInfo() {
        System.out.println(this.name + " " + this.age + " " + this.salary);
    }
}

构造器与封装

package com.hsp.encap;
import java.util.Scanner;
public class EncapSulation {
    public static void main(String[] args) {
        Person person = new Person("jack",18,10000);
        person.getInfo();
    }
}
class Person {
    public String name;// 名字公开
    private int age; //age私有化
    private double salary; //salary私有化

    //构造器
    public Person(){

    }

    public Person(String name, int age, double salary) {
//        this.name = name;
//        this.age = age;
//        this.salary = salary;
        //我们可以将set方法写在构造器中,这样依然可以验证数据
        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("名字的长度不对,需要2-6个字符");
            this.name = "普通用户";
        }
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if(age>=1 && age<=120){
            this.age = age;
        }
        else {
            System.out.println("年龄需要在1-120");
            this.age = 18;
        }
    }

    public void getSalary() {
        //增加对当前对象的权限判断
        System.out.println("想查看" + this.name + "的薪水,请输入密码: ");
        Scanner sc = new Scanner(System.in);
        int count = 3;
        boolean flag = true;
        while(flag) {
            int password = sc.nextInt();
            count--;
            flag = count != 0;
            if(password == 782157532){
                System.out.println(this.name + "的薪水是:" + this.salary);
                break;
            }else {
                System.out.println("输入密码错误");
            }

        }
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
    public void getInfo() {
        System.out.println(this.name + " " + this.age + " " + this.salary);
    }
}

封装课堂练习

image-20221118113154656

//Account.java
package com.hsp.encap;

import java.util.Scanner;

public class Account {
    private String name;
    private double balance;
    private String pwd;

    public Account() {

    }

    public Account(String name, double balance, String pwd) {
        this.setName(name);
        this.setBalance(balance);
        this.setPwd(pwd);
    }

    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位的用户名");
            this.name = "普通用户";
        }
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        if (balance > 20) {
            this.balance = balance;
        } else {
            System.out.println("余额必须大于20");
            this.balance = 0;
        }
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        if (pwd.length() == 6) {
            this.pwd = pwd;
        } else {
            System.out.println("密码必须是6位");
        }
    }

    public void showInfo() {
        //可以增加权限的校验
        System.out.println("进行信息校验....");
        System.out.println("请输入验证密码:");
        Scanner sc = new Scanner(System.in);
        String password = sc.next();
        if (this.pwd.equals(password)) {
            System.out.println("账号信息:" + this.name + this.balance);
        } else {
            System.out.println("输入密码错误!");
        }
    }
}
//AccountTest.java
package com.hsp.encap;
public class AccountTest {
    public static void main(String[] args) {
        Account account = new Account("meow",2000,"782157");
        account.showInfo();
    }
}