0


解析java中的封装

解析java中的封装

1 含义

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

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

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

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

2 为什么需要封装?

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

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

没有封装时

示例代码
Cat
publicclassCat{String  name;String type;int age;publicCat(){}//定义有参构造,方便实例化对象时,初始化值publicCat(String name,String type,int age){this.name = name;this.type = type;this.age = age;}publicvoidshowInfo(){System.out.println("猫的名字为: "+name+" 猫的品种为: "+type+" 猫的年龄为: "+age);}}
TestCat
publicclassTestCat{publicstaticvoidmain(String[] args){Cat cat=newCat("叮当","狸花猫",100);System.out.println("修改前: ");
        cat.showInfo();
        cat.age=-2;System.out.println("修改后: ");
        cat.showInfo();}}
分析

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

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

示例代码运行截图

在这里插入图片描述

封装后

示例代码
Cat
publicclassCat{privateString  name;privateString type;privateint age;publicCat(){}//定义有参构造,方便实例化对象时,初始化值publicCat(String name,String type,int age){this.name = name;this.type = type;setAge(age);}publicStringgetName(){return name;}publicvoidsetName(String name){this.name = name;}publicStringgetType(){return type;}publicvoidsetType(String type){this.type = type;}publicintgetAge(){return age;}publicvoidsetAge(int age){if(age<0||age>20){System.out.println("初始化的年龄有误,请检查后重新输入!!!");return;}this.age = age;}publicvoidshowInfo(){System.out.println("猫的名字为: "+name+" 猫的品种为: "+type+" 猫的年龄为: "+age);}}
TestCat
publicclassTestCat{publicstaticvoidmain(String[] args){Cat cat=newCat("叮当","狸花猫",100);System.out.println("修改前: ");
        cat.showInfo();System.out.println("修改后: ");
        cat.setAge(10);
        cat.showInfo();}}
分析

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

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

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

示例代码运行截图

在这里插入图片描述

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

没有封装时

示例代码
Cat
publicclassCat{String  name;String type;int age;publicCat(){}//定义有参构造,方便实例化对象时,初始化值publicCat(String name,String type,int age){this.name = name;this.type = type;this.age=age;}publicvoidshowInfo(){System.out.println("猫的名字为: "+name+" 猫的品种为: "+type+" 猫的年龄为: "+age);}}
TestCat
publicclassTestCat{publicstaticvoidmain(String[] args){//猫的品种一经出生,就不能修改了,但是我这边还是可以通过对象名.属性名的方式去修改Cat cat=newCat("叮当","狸花猫",8);System.out.println("修改猫的品种前: ");
        cat.showInfo();System.out.println("修改猫的品种后: ");
        cat.type="短尾";
        cat.showInfo();}}
分析

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

示例代码运行截图

在这里插入图片描述

封装后

示例代码
Cat
publicclassCat{String  name;privateString type;int age;publicCat(){}//定义有参构造,方便实例化对象时,初始化值publicCat(String name,String type,int age){this.name = name;this.type = type;this.age=age;}//获取当前猫对象的品种publicStringgetType(){return type;}publicvoidshowInfo(){System.out.println("猫的名字为: "+name+" 猫的品种为: "+type+" 猫的年龄为: "+age);}}
TestCat
publicclassTestCat{publicstaticvoidmain(String[] args){//猫的品种一经出生,就不能修改了,但是我这边还是可以通过对象名.属性名的方式去修改Cat cat=newCat("叮当","狸花猫",8);System.out.println("修改猫的品种前: ");
        cat.showInfo();System.out.println("修改猫的品种后: ");
        cat.type="短尾";
        cat.showInfo();}}
分析

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

示例代码出现错误的截图

在这里插入图片描述

3 如何使用封装?

3.1 先私有化属性

privateString  name;privateString type;privateint age;

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

//显然猫的名字是可读写性质,品种是只读属性,年龄为可读写属性//为可读写属性就是set方法以及get方法都有,只读属性就是只有get方法publicStringgetName(){return name;}publicvoidsetName(String name){this.name = name;}//type(品种)属性只可以读取,不可以写入publicStringgetType(){return type;}publicintgetAge(){return age;}publicvoidsetAge(int age){if(age<0||age>20){System.out.println("猫的年龄输入有误,请检查后重新输入!!!!");return;}this.age = age;}

3.3完整代码

Cat

publicclassCat{privateString  name;privateString type;privateint age;publicCat(){}//定义有参构造,方便实例化对象时,初始化值publicCat(String name,String type,int age){this.name = name;this.type = type;this.age=age;}publicStringgetName(){return name;}publicvoidsetName(String name){this.name = name;}//type(品种)属性只可以读取,不可以写入publicStringgetType(){return type;}publicintgetAge(){return age;}publicvoidsetAge(int age){if(age<0||age>20){System.out.println("猫的年龄输入有误,请检查后重新输入!!!!");return;}this.age = age;}publicvoidshowInfo(){System.out.println("猫的名字为: "+name+" 猫的品种为: "+type+" 猫的年龄为: "+age);}}

TestCat

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

3.4 完整代码运行截图

在这里插入图片描述

标签: java 封装 特性

本文转载自: https://blog.csdn.net/SSS4362/article/details/125688949
版权归原作者 SSS4362 所有, 如有侵权,请联系我们删除。

“解析java中的封装”的评论:

还没有评论