先看一个未封装的Demo案例一:
package android.java.oop03; class Person { int age; } public class PottingDemo { public static void main(String[] args) { Person person = new Person(); person.age = -200; System.out.println("年龄是:" + person.age); } }
执行结果:
年龄是:-200
封装后的Demo案例一:
package android.java.oop03; class Person { /** * 加入私有private,不对外暴露 */ private int age; public void setAge(int age) { if (age < 0 || age > 200) { throw new IllegalArgumentException("setAge age值不规范,age value:" + age); } this.age = age; } public int getAge() { return age; } } public class PottingDemo { public static void main(String[] args) { Person person = new Person(); person.setAge(9); System.out.println("年龄是:" + person.getAge()); } }
执行结果:
年龄是:9
未封装的案例代码 案例二:
package android.java.oop03; class Counter { int add(int number1, int number2) { return number1 + number2; } int jian(int number1, int number2) { return number1 - number2; } int cheng(int number1, int number2) { return number1 * number2; } int chu(int number1, int number2) { return number1 / number2; } void doCounter(int number1, String f, int number2) { int result; if ("+".equals(f)) { result = add(number1, number2); } else if ("-".equals(f)) { result = jian(number1, number2); } else if ("*".equals(f)) { result = cheng(number1, number2); } else if ("/".equals(f)) { result = chu(number1, number2); } else { throw new IllegalArgumentException("传入的运算符 有异常,不能是 f" + f); } println(result); } void println(int result) { System.out.println("运算的结果是:" + result); } } public class PottingDemo2 { public static void main(String[] args) { Counter counter = new Counter(); counter.doCounter(8, "+" ,8); counter.doCounter(20,"-", 10); counter.doCounter(9, "*", 9); counter.doCounter(100,"/",2); } }
执行结果:
运算的结果是:16
运算的结果是:10
运算的结果是:81
运算的结果是:50
封装的案例代码 案例二:
package android.java.oop03; class Counter { private int add(int number1, int number2) { return number1 + number2; } private int jian(int number1, int number2) { return number1 - number2; } private int cheng(int number1, int number2) { return number1 * number2; } private int chu(int number1, int number2) { return number1 / number2; } public void doCounter(int number1, String f, int number2) { int result; if ("+".equals(f)) { result = add(number1, number2); } else if ("-".equals(f)) { result = jian(number1, number2); } else if ("*".equals(f)) { result = cheng(number1, number2); } else if ("/".equals(f)) { result = chu(number1, number2); } else { throw new IllegalArgumentException("传入的运算符 有异常,不能是 f" + f); } println(result); } private void println(int result) { System.out.println("运算的结果是:" + result); } } public class PottingDemo2 { public static void main(String[] args) { Counter counter = new Counter(); counter.doCounter(8, "+" ,8); counter.doCounter(20,"-", 10); counter.doCounter(9, "*", 9); counter.doCounter(100,"/",2); } }
执行结果:
运算的结果是:16
运算的结果是:10
运算的结果是:81
运算的结果是:50
1.封装可以自定义隐藏细节(private),自定义对外提供接口(public);
2.封装可以提高安全性,private;
3.封装可以提高复用性
举例:
未封装的表现:把 主版,电源,这些零件全部摆放在桌子上,每次开机时,用金属物触碰主板上的开机卡槽来开机;
1.不安全,万一不小心把水倒在主板上
2.暴露了细节
封装的表现:把 主板 电源,这些零件全部用机箱封装起来(隐藏细节),对外提供暴露的接口(USB,显示器,网卡等,开机开关);
1.安全,被封装了,受保护了
2.隐藏了细节