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());

    }
}
相关推荐
xMathematics16 分钟前
计算机图形学实践:结合Qt和OpenGL实现绘制彩色三角形
开发语言·c++·qt·计算机图形学·cmake·opengl
ShiinaMashirol1 小时前
代码随想录打卡|Day27(合并区间、单调递增的数字、监控二叉树)
java·算法
东阳马生架构2 小时前
Nacos简介—3.Nacos的配置简介
java
yuanManGan2 小时前
C++入门小馆: 深入了解STLlist
开发语言·c++
北极的企鹅882 小时前
XML内容解析成实体类
xml·java·开发语言
BillKu2 小时前
Vue3后代组件多祖先通讯设计方案
开发语言·javascript·ecmascript
oioihoii2 小时前
C++23 中 static_assert 和 if constexpr 的窄化布尔转换
java·jvm·c++23
Python自动化办公社区2 小时前
Python 3.14:探索新版本的魅力与革新
开发语言·python
逐光沧海3 小时前
STL常用算法——C++
开发语言·c++
聂 可 以3 小时前
调整IntelliJ IDEA当前文件所在目录(包路径)的显示位置
java·ide·intellij-idea