java基础学习——10、封装与接口

一、封装与接口

封装(encapsulation)是计算机常见的术语,即保留有限的外部接口(interface),隐藏具体实施细节。我们将要封装(encapsulation)对象的成员(成员包括数据成员和方法),从而只允许从外部调用部分的成员。利用封装,我们可以提高对象的易用性和安全性。

 

对象成员的封装:


 

Java通过三个关键字来控制对象的成员的外部可见性(visibility): public, private, protected。

  • public: 该成员外部可见,即该成员为接口的一部分。
  • private: 该成员外部不可见,只能用于内部使用,无法从外部访问。
  • protected:该成员对于子类、友元等同于public,而对于其他的类来说则等于private。
作用域 当前类 同一package 子孙类 其他package
public
protected
X
friendly
X X
private
X
X X

 

 

 

 

 

 

 

下面来封装一个Human类:

 

public class Test
{
    public static void main(String[] args)
    {
        Human aPerson = new Human(160);
        System.out.println(aPerson.getHeight());
        aPerson.growHeight(170);
        System.out.println(aPerson.getHeight());
        aPerson.repeatBreath(100);
    }

}

class Human
{
    /**
     * constructor
     */
    public Human(int h)
    {
        this.height = h;
        System.out.println("I'm born");
    }

    /**
     * accessor
     */
    public int getHeight()
    {
       return this.height;
    }

    /**
     * mutator
     */
    public void growHeight(int h)
    {
        this.height = this.height + h;
    }

     /**
      * encapsulated, for internal use
      */
    private void breath()
    {
        System.out.println("hu...hu...");
    }


   /**
    * call breath()
    */
    public void repeatBreath(int rep)
    {
        int i;
        for(i = 0; i < rep; i++) {
            this.breath();
        }
    }

    private int height; // encapsulated, for internal use
}

 

内部方法并不受封装的影响。Human的内部方法可以调用任意成员,即使是设置为private的height和breath()

外部方法只能调用public成员。当我们在Human外部时,比如Test中,我们只能调用Human中规定为public的成员,而不能调用规定为private的成员。

通过封装,Human类就只保留了下面几个方法作为接口:

 

  • getHeight()
  • growHeight()
  • repBreath()

 

 

我们可以将Human类及其接口表示为如下图的形式:

Human

+getHeight()
+growHeight()
+repBreath()

如果我们从main中强行调用height:

System.out.println(aPerson.height);

将会有如下错误提示:

Test.java:6: height has private access in Human
        System.out.println(aPerson.height);
                                  ^
1 error

一个被说明为private的成员,不能被外部调用。

在Java的通常规范中,表达状态的数据成员(比如height)设置成private。对数据成员的修改要通过接口提供的方法进行(比如getHeight()和growHeight())。这个规范起到了保护数据的作用。用户不能直接修改数据,必须通过相应的方法才能读取和写入数据。类的设计者可以在接口方法中加入数据的使用规范。

posted @ 2017-08-08 10:27  丸wanwan子  阅读(338)  评论(0编辑  收藏  举报