java_封装

基本介绍

面向对象编程有三大特征:封装、继承和多态。

封装介绍

封装的理解和好处

封装的实现步骤(三步)

案例

java 复制代码
package com.hspedu.encap;


public class Encapsulation01 {
    public static void main(String[] args) {
        // 如果要使用快捷键alt+r,需要先配置主类
        // 第一次,我们使用鼠标点击形式运算程序,后面就可以用了
        Person person = new Person();
        person.setName("Jackkkkkkkkkkkkkkku");
        person.setAge(30);
        person.setSalary(250000);

        System.out.println(person.info());
        System.out.println(person.getSalary());
    }
}
/*    那么在 java 中如何实现这种类似的控制呢?
        请大家看一个小程序(com.hspedu.encap: Encapsulation01.java), 不能随便查看人的年龄,工资等隐私,并对设置的年龄进行合理的验证。年龄合理就设置,否则给默认
        年龄, 必须在 1-120, 年龄, 工资不能直接查看 , name 的长度在 2-6 字符 之间*/
class Person {
    public String name; //名字公开
    private int age;// age私有化
    private double salary; //..

//    public void setName(String name) {
//        this.name = name;
//    }
//
//    public String getName() {
//
//    }

    //自己写setXxx 和 getXxx太慢了,我们使用快捷键
    // 然后根据要求完善我们的代码;
    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),给默认年龄18");
            this.age = 18; //默认年龄
        }

    }

    public double getSalary() {
        //可以在这里增加对当前对象的权限判断
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    // 写一个方法,返回属性信息
    public String info() {
        return "信息为 name=" + name + " age =" +age + " 薪水=" + salary;
    }
}

封装与构造器

java 复制代码
package com.hspedu.encap;


public class Encapsulation01 {
    public static void main(String[] args) {
        // 如果要使用快捷键alt+r,需要先配置主类
        // 第一次,我们使用鼠标点击形式运算程序,后面就可以用了
        Person person = new Person();
        person.setName("Jackkkkkkkkkkkkkkku");
        person.setAge(30);
        person.setSalary(250000);

        System.out.println(person.info());
        System.out.println(person.getSalary());

        //如果我们自己使用构造器指定属性
        Person smith = new Person("smith", 2000, 40000);
        System.out.println("======smith=======");
        System.out.println(smith.info());
    }
}
/*    那么在 java 中如何实现这种类似的控制呢?
        请大家看一个小程序(com.hspedu.encap: Encapsulation01.java), 不能随便查看人的年龄,工资等隐私,并对设置的年龄进行合理的验证。年龄合理就设置,否则给默认
        年龄, 必须在 1-120, 年龄, 工资不能直接查看 , name 的长度在 2-6 字符 之间*/
class Person {
    public String name; //名字公开
    private int age;// age私有化
    private double salary; //..

    // 构造器 alt + insert
    public Person() {

    }

    // 有三个属性的构造器 alt + insert
    public Person(String name, int age, double salary) {
//        this.name = name;
//        this.age = age;
//        this.salary = salary;
        //我们可以将set方法写在构造器中,这样仍然可以验证
        setName(name);
        setAge(age);
        setSalary(salary);
    }


    //自己写setXxx 和 getXxx太慢了,我们使用快捷键
    // 然后根据要求完善我们的代码;
    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),给默认年龄18");
            this.age = 18; //默认年龄
        }

    }

    public double getSalary() {
        //可以在这里增加对当前对象的权限判断
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    // 写一个方法,返回属性信息
    public String info() {
        return "信息为 name=" + name + " age =" +age + " 薪水=" + salary;
    }

}

练习

java 复制代码
package com.hspedu.encap;

public class Account {
    //为了封装,将3个属性设置为private;
    private String name;
    private double yue;
    private String password;

    //提供两个构造器
    public Account() {
    }

    public Account(String name, double yue, String password) {
        this.setName(name);
        this.setYue(yue);
        this.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,3,4位长度,给默认名字");
            this.name = "无名";
        }

    }

    public double getYue() {
        return yue;
    }

    public void setYue(double yue) {
        if (yue > 20) {
            this.yue = yue;
        } else {
            System.out.println("你的余额不足20!请及时充值,给默认值0");
            this.yue = 0;
        }
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        if (password.length() == 6) {
            this.password = password;
        } else {
            System.out.println("你的密码不是6位,请再次修改密码!默认密码为123456");
            this.password = "123456";
        }
    }

    // 显示账号信息
    public void showInfo() {
        System.out.println("账号信息 name= " + name + " 余额=" + yue + " 密码=" + password);
    }

    public String Info() {
        return "账号信息 name= " + name + " 余额=" + yue + " 密码=" + password;
    }
}
java 复制代码
package com.hspedu.encap;


public class AccountTest {
    public static void main(String[] args) {
        Account account = new Account();
        account.setName("jack");
        account.setYue(60);
        account.setPassword("666666");

        account.showInfo();

        // 使用构造器 修改对象的属性
        System.out.println("======smith======");
        Account smith = new Account("smith", 888, "345678");
        System.out.println(smith.Info());

    }
}
相关推荐
程序喵大人2 小时前
【C++进阶】STL容器与迭代器 - 01 STL 容器先解决元素放在哪里
开发语言·c++·stl
cui_ruicheng3 小时前
Python从入门到实战(十六):多进程编程
开发语言·python
Jelena157795857923 小时前
电商运营分析数据比价接口实战:多平台价格监控与智能决策系统
java·大数据·数据库
wdfk_prog4 小时前
嵌入式面试真题第 15 题:不可恢复异常后的通用崩溃快照、调用栈保存与离线分析架构
linux·开发语言·面试·架构
晴空了无痕4 小时前
从 Go 基础到 K8s:一条可落地的 Go 服务端成长路线
开发语言·后端·golang·kubernetes
神明不懂浪漫4 小时前
【第五章】Java中的继承与多态
java·开发语言
神州世通5 小时前
解密企业通信安全防线:Avaya Aura 三层安全架构深度解析
开发语言·php
AC赳赳老秦6 小时前
企业工商公开信息采集分析:OpenClaw 批量查询企业工商信息,生成企业画像报告
大数据·开发语言·python·自动化·php·deepseek·openclaw
AI多Agent协作实战派6 小时前
AI多Agent协作系统实战(十七):凌晨4点,我的AI系统在“假装工作“——3个bug同时爆炸的5小时
java·前端·bug
gaolei_eit6 小时前
Java+Ai+vue
java·spring·maven