面向对象----信息的封装和隐藏

信息的封装和隐藏

信息封装和隐藏的必要性


使用者对类内部定义的属性(对象的成员变量)的直接操作会导致数据的错误、混乱或安全性问题。(version1)

 1 public class Animal {
 2     public int legs;    
 3     public void eat(){
 4         System.out.println(“Eating.”);
 5     }
 6     public void move(){
 7         System.out.println(“Moving.”);
 8     }
 9 }
10 
11 public class Zoo{
12     public static void main(String args[]){
13         Animal xb=new Animal(); 
14         xb.legs=4;
15         System.out.println(xb.legs);
16         xb.eat();xb.move();
17     }
18 }

应该将legs属性保护起来,防止乱用。
保护的方式:信息隐藏


Java中通过将数据声明为私有的(private),再提供公开的(public)方法:getXXX和setXXX实现对该属性的操作,以实现下述目的:
  隐藏一个类的实现细节;
  使用者只能通过事先定制好的方法来访问数据,可以方便地加入控制逻辑,限制对属性的不合理操作;
  便于修改,增强代码的可维护性;(version2)

 1 public class Animal {
 2     private int legs;    //将属性legs定义为private,只能被Animal类内部访问
 3     public void setLegs(int i) { //在这里定义方法 eat() 和 move() 
 4         if (i != 0 && i != 2 && i != 4) {
 5             System.out.println("Wrong number of legs!");
 6                 return;
 7         }
 8         legs=i;
 9     }
10     public int getLegs() {
11         return legs;
12     }
13 }
14 public class Zoo {
15     public static void main(String args[]) {
16         Animal xb=new Animal();
17         xb.setLegs(4);    //xb.setLegs(-1000); 
18         xb.legs=-1000;    //非法
19         System.out.println(xb.getLegs());
20     }
21 }



练习
1、创建程序,在其中定义两个类,Person和TestPerson类的定义如下。用setAge()设置人的合法年龄(0~130),用getAge()返回人的年龄。在Test类中实例化Person类的对象b,调用setAge()和getAge()方法,体会Java的封装性。

 1 public class  Person{
 2     public static void main(String[] args) {
 3         private int age;
 4 
 5         public int getAge(){
 6             return age;
 7         }
 8         //为属性赋值的 set 方法
 9         public void setAge(int age) {
10             if(age >130 & age<0){
11                 System.out.println("age 赋值不合法: " + age);
12                 return;
13             }
14             this.age = age;
15         }
16     }
17 }
public class TextPerson{
    public static void main(String[] args) {
        Person p = new Person;
        p.setAge(22);
        p.getAge();
        System.out.println("年龄为:" + getAge);
    }
}

2、求圆的面积

 1 /**
 2  * 什么是信息的封装和隐藏?
 3  * 
 4  * 1. 把属性隐藏起来: 使用 private 关键字来修饰
 5  * 2. 提供 public 的 set, get 方法来访问属性.  7  */
 8 public class Circle {
 9     
10     //构造器于类同名, 且没有返回值(连 void 都没有). 
11     //JVM 会为每一个类提供一个默认的无参数的构造器: public Circle(){}
12     //构造器的作用: 对类的属性进行初始化. 
13     public Circle(){
14         radius = 10;
15     }
16     
17     //半径
18     //private 关键字: 可以修饰类的成员, 一经使用 private 修饰, 该属性将不能在外部被直接访问. 
19     //可以提供 public 方法来操作属性: getXxx() 返回属性值, setXxx() 为属性赋值。 
20     private int radius;
21     
22     //读取属性值的方法
23     public int getRedius(){
24         return radius;
25     }
26     
27     //为属性赋值的 set 方法
28     public void setRedius(int r) {
29         if(r <= 0){
30             System.out.println("redius 赋值不合法: " + r + ", " +
31                     "redius 取默认的初始值 0");
32             return;
33         }
34         
35         radius = r;
36     }
37     
38     //返回圆的面积
39     public double findArea(){
40         return 3.14 * radius * radius;
41     }
42 }
 1 public class TestCircle {
 2     public static void main(String[] args) {
 3         
 4         //创建对象一定是调用了类的某一个构造器. 
 5         //如果在类中没有显式的定义构造器, 则将调用默认的那个构造器. 
 6         Circle c = new Circle();
 7         
 8         //c.redius = -2;
 9         //通过方法为 Circle 对象的 redius 属性赋值
10 //        c.setRedius(3);
11         
12         System.out.println(c.findArea()); 
13         System.out.println("redius: " + c.getRedius()); 
14     }
15 }
原文地址:https://www.cnblogs.com/justdoitba/p/7221607.html