解析java中的封装

本文深入解析了Java中的封装概念,通过生活中的电视机例子进行类比,阐述了封装的意义在于信息隐藏和提供公共接口。封装能够确保输入数据的合理性,防止非法值的设置,并能设定属性的读写特性。文中通过示例代码对比了封装前后对猫类属性的操作,强调了封装在数据校验和属性权限控制上的重要性。
摘要由CSDN通过智能技术生成

解析java中的封装

1 含义

1.1 以生活中的例子为例,打开电视机的时候你只需要按下开关键,电视机就会打开,我们通过这个操作我们可以去间接的对电视机里面的元器件进行亮屏和显示界面操作,具体怎么实现我们并不知道,我们只需要通过这个公共的电源键去对电视机进行操作就行。

1.2 封装也是同理,它会把一些属性(成员变量)拿个黑盒子装起来,然后使用者是直接接触不到这些属性的,使用时,通过一些公共方法(中间商)去进行访问。

1.3 是为了信息隐藏,禁止直接访问一个对象中的数据的实际表现形式,通过接口去进行访问

1.4 封装=私有化属性+公共调用属性的方法

2 为什么需要封装?

2.1 保证输入的数据是合理的

​ 题目:定义一个Cat类,用户需手动输入猫的寿命;假设猫的最长寿命为20岁,那么输入大于20的数或者小于0的数字显然是非法的

没有封装时
示例代码
Cat
public class Cat {
    String  name;
    String type;
    int age;
    public Cat(){

    }
    //定义有参构造,方便实例化对象时,初始化值
    public Cat(String name, String type, int age) {
        this.name = name;
        this.type = type;
        this.age = age;
    }
    public void showInfo(){
        System.out.println("猫的名字为: "+name+" 猫的品种为: "+type+" 猫的年龄为: "+age);
    }

}
TestCat
public class TestCat {
    public static void main(String[] args) {
        Cat cat=new Cat("叮当","狸花猫",100);
        System.out.println("修改前: ");
        cat.showInfo();
        cat.age=-2;
        System.out.println("修改后: ");
        cat.showInfo();
    }
}

分析

可以明显的发现,用户可以通过对象名.属性值的方式去修改属性,而且修改属性的值没有限制,输入啥就输出啥,显然输入猫的年龄为200或者-2都是是错的

而且构造方法初始化时也没有去判断数据的合法性

示例代码运行截图

在这里插入图片描述

封装后
示例代码
Cat
public class Cat {
    private String  name;
    private String type;
    private int age;
    public Cat(){

    }
    //定义有参构造,方便实例化对象时,初始化值
    public Cat(String name, String type, int age) {
        this.name = name;
        this.type = type;
        setAge(age);
    }

    public String getName() {
        return name;
    }

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

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if(age<0||age>20){
            System.out.println("初始化的年龄有误,请检查后重新输入!!!");
            return;
        }
        this.age = age;
    }

    public void showInfo(){
        System.out.println("猫的名字为: "+name+" 猫的品种为: "+type+" 猫的年龄为: "+age);
    }

}

TestCat
public class TestCat {
    public static void main(String[] args) {
        Cat cat=new Cat("叮当","狸花猫",100);
        System.out.println("修改前: ");
        cat.showInfo();
        System.out.println("修改后: ");
        cat.setAge(10);
        cat.showInfo();
    }
}

分析

你会发现实例化cat对象时,对年龄赋值的100并没有成功,因为没有符合年龄的条件,因而被赋予了默认值

而通过set方法去设置(写入)合理的年龄,会发现年龄是可以被设置成功的

也就是说封装可以将一些不合理的数据过滤掉,不被赋值进去,这样可以使程序的数据变得更加合理。

示例代码运行截图

在这里插入图片描述

2.2 可以设置属性的特性(只读、可读写、可写)

没有封装时
示例代码
Cat
public class Cat {
    String  name;
    String type;
    int age;
    public Cat(){

    }
    //定义有参构造,方便实例化对象时,初始化值
    public Cat(String name, String type, int age) {
        this.name = name;
        this.type = type;
        this.age=age;
    }

    public void showInfo(){
        System.out.println("猫的名字为: "+name+" 猫的品种为: "+type+" 猫的年龄为: "+age);
    }

}
TestCat
public class TestCat {
    public static void main(String[] args) {
        //猫的品种一经出生,就不能修改了,但是我这边还是可以通过对象名.属性名的方式去修改
        Cat cat=new Cat("叮当","狸花猫",8);
        System.out.println("修改猫的品种前: ");
        cat.showInfo();
        System.out.println("修改猫的品种后: ");
        cat.type="短尾";
        cat.showInfo();
    }
}

分析

按照常理来说,猫的品种一经出生就已经限定死了,也就是说它是可读属性,但是上面的例子可却以对不可更改的属性进行修改,因此这就也是没有封装的一个弊端,属性的特性在它面前都是属于"我们都一样"的这种效果。”(啥都是可以读写)

示例代码运行截图

在这里插入图片描述

封装后
示例代码
Cat
public class Cat {
    String  name;
    private String type;
    int age;
    public Cat(){

    }
    //定义有参构造,方便实例化对象时,初始化值
    public Cat(String name, String type, int age) {
        this.name = name;
        this.type = type;
        this.age=age;
    }
    //获取当前猫对象的品种
    public String getType() {
        return type;
    }

    public void showInfo(){
        System.out.println("猫的名字为: "+name+" 猫的品种为: "+type+" 猫的年龄为: "+age);
    }

}

TestCat
public class TestCat {
    public static void main(String[] args) {
        //猫的品种一经出生,就不能修改了,但是我这边还是可以通过对象名.属性名的方式去修改
        Cat cat=new Cat("叮当","狸花猫",8);
        System.out.println("修改猫的品种前: ");
        cat.showInfo();
        System.out.println("修改猫的品种后: ");
        cat.type="短尾";
        cat.showInfo();
    }
}

分析

这个时候程序不会正常运行,因为把type属性私有化后,不能通过对象名.属性名的方式进行调用,然后也只能通过getType方法获取它的值,没有set方法,因此不能修改它的值

示例代码出现错误的截图

在这里插入图片描述

3 如何使用封装?

3.1 先私有化属性

 private String  name;
 private String type;
 private int age;

3.2 然后根据需求去进行公开方法(get/set)

 //显然猫的名字是可读写性质,品种是只读属性,年龄为可读写属性
//为可读写属性就是set方法以及get方法都有,只读属性就是只有get方法
 public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    //type(品种)属性只可以读取,不可以写入
    public String getType() {
        return type;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if(age<0||age>20){
            System.out.println("猫的年龄输入有误,请检查后重新输入!!!!");
            return;
        }
        this.age = age;
    }

3.3完整代码

Cat
public class Cat {
    private String  name;
    private String type;
    private int age;
    public Cat(){

    }
    //定义有参构造,方便实例化对象时,初始化值
    public Cat(String name, String type, int age) {
        this.name = name;
        this.type = type;
        this.age=age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    //type(品种)属性只可以读取,不可以写入
    public String getType() {
        return type;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if(age<0||age>20){
            System.out.println("猫的年龄输入有误,请检查后重新输入!!!!");
            return;
        }
        this.age = age;
    }

    public void showInfo(){
        System.out.println("猫的名字为: "+name+" 猫的品种为: "+type+" 猫的年龄为: "+age);
    }

}

TestCat
public class TestCat {
    public static void main(String[] args) {
        //猫的品种一经出生,就不能修改了,但是我这边还是可以通过对象名.属性名的方式去修改
        Cat cat=new Cat("叮当","狸花猫",8);
        System.out.println("一年前: ");
        cat.showInfo();
        cat.setName("小梦");
        System.out.println("获取猫的品种: "+cat.getType());
        cat.setAge(9);
        System.out.println("一年后: ");
        cat.showInfo();
    }
}

3.4 完整代码运行截图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SSS4362

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值