怎样解析java中的封装

1.解析java中的封装

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

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

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

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

2 为什么需要封装?

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

没有封装时示例代码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都是是错的

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

示例代码运行截图

示例代码

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 可以设置属性的特性(只读、可读写、可写)没有封装时示例代码

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

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

示例代码运行截图

示例代码

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

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

示例代码运行截图

示例代码

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完整代码

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 完整代码运行截图

梦想,并不奢侈,只要勇敢地迈出第一步。

怎样解析java中的封装

相关文章:

你感兴趣的文章:

标签云: