面向对象编程三大特性之一--封装

来源:互联网 发布:数控车床编程实例初级 编辑:程序博客网 时间:2024/05/19 02:05

我们知道,我们通常所说的面向对象编程有三大特性,继承,封装与多态。

封装,既然作为三大特性之一,什么是封装,怎么封装,以及封装有什么好处?

先看一下比较官方的一种定义:封装是把过程和数据包围起来,对数据的访问只能通过已定义的接口。面向对象计算始于这个基本概念,即现实世界可以被描绘成一系列完全自治、封装的对象,这些对象通过一个受保护的接口访问其他对象。封装是一种信息隐藏技术,在java中通过关键字private实现封装。什么是封装?封装把对象的所有组成部分组合在一起,封装定义程序如何引用对象的数据,封装实际上使用方法将类的数据隐藏起来,控制用户对类的修改和访问数据的程度。

比如我们把一个学生的姓名 ,性别,年龄,以及其所具有的一些方法封装为一个学生实体,并对外提供一些接口,即使一种封装。通常我们所写的类,就是对一些具有相同事物的属性及方法的抽象,最后封装在一起。下面通过一个对图片类的封装,来进一步理解封装。封装之后的图片类,可以任意的在窗体上移动,改变大小,移动的速度这些都可以实现。

package com.sg.PictureWatch;import java.awt.Graphics;import java.awt.Image;import javax.swing.ImageIcon;public class PictureWatch {       Image img;            /**              *图片的起始位置x,y              *图片的宽高              *图片在x,y方向上移动的速度              **/       int x,y,width,height,Xspeed,Yspeed;            //最少只需要传入一张图片路径即可       public PictureWatch(String path)         {           this(path,0,0);         }       public PictureWatch(String path,int x,int y)         {          this(path,x,y,new ImageIcon(path).getIconWidth(),new ImageIcon(path).getIconHeight());         }       public PictureWatch(String path,int x,int y,int width,int height)         {           this(path,x,y,width,height,2,2);         }       public PictureWatch(String path,int x,int y,int width,int heighth,int Xspeed,int Yspeed)         {          img=new ImageIcon(path).getImage();          this.x=x;          this.y=y;          this.width=width;          this.height=height;          this.Xspeed=Xspeed;          this.Yspeed=Yspeed;         }                //将图片画出来         public void draw(Graphics g){             g.drawImage(img, x, y, width,height,null);         }          public void move(int xAdd, int yAdd, int times, boolean needWait) {             moveAndChange(xAdd, yAdd, 0, 0, times, needWait);         }         public void change(int wAdd, int hAdd, int times, boolean needWait) {             moveAndChange(0, 0, wAdd, hAdd, times, needWait);         }         public void moveAndChangeTo(int xAdd, int yAdd, int wAdd, int hAdd, int times, boolean needWait) {             moveAndChange(xAdd - x, yAdd - y, wAdd - width, hAdd - height, times, needWait);         }         public void moveAndChange(int xAdd, int yAdd, int wAdd, int hAdd, int times, boolean needWait) {             Object o = needWait ? new Object() : null;             new Thread() {                 public void run() {                     int xs = xAdd / times;                     int ys = yAdd / times;                     int ws = wAdd / times;                     int hs = hAdd / times;                     for (int i = 0; i < times; i++) {                         x += xs;                         y += ys;                         width += ws;                         height += hs;                         isleep(30);                     }                     if (needWait) {                         synchronized (o) {                             o.notify();                         }                     }                 };             }.start();             if (needWait) {                 synchronized (o) {                     try {                         o.wait();                     } catch (InterruptedException e) {                         // TODO Auto-generated catch block                         e.printStackTrace();                     }                 }             }         }         private void isleep(int i) {             // TODO Auto-generated method stub             try {                 Thread.currentThread().sleep(30);             } catch (InterruptedException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();             }         }       }
这里在创建图片的时候,最少就可以直接给一个图片的路径即可,或者给其他的参数,包括x,y的位置,宽高等。

在图片移动的过程中,通过对象先用相应的move,change方法,可以发现这样用起来更加的方便。

通过封装,不仅在使用的使用更加的方便,而且使我们的代码的逻辑更加强大。所以,能够轻松正确使用封装,是我们能否写出漂亮代码的第一步。






0 0
原创粉丝点击