面向对象面向过程

编程技术  /  houtizong 发布于 3年前   227
面向对象:把要完成的一件事,通过对象间的协作实现。
面向过程:把要完成的一件事,通过循序依次调用各个模块实现。
我把大象装进冰箱这件事为例,用面向对象和面向过程实现,都是用java代码完成。

1、面向对象
package bigDemo.ObjectOriented;/** * 大象类 *  * @Description * @author FuJianyong * 2015-7-1下午05:47:11 */public class Elephant { private String name;   public Elephant(String name){this.name = name;}public String getName() {return name;}}

package bigDemo.ObjectOriented;/** * 冰箱类 *  * @Description * @author FuJianyong * 2015-7-1下午05:39:05 */public class IceBox {private String name;public void open(People people){System.out.println(people.getName() + "把" + this.name + "打开");}public void close(People people){System.out.println(people.getName() + "把" + this.name + "关闭");}public IceBox(String name) {this.name = name;}public String getName() {return name;}}

package bigDemo.ObjectOriented;/** * 人类 *  * @Description * @author FuJianyong * 2015-7-1下午05:39:15 */public class People {     private String name;/** * 描述人的动作 */private String action;public void openIceBox(IceBox iceBox){iceBox.open(this);//this表示调用此方法的对象(当前对象)}public void pushElephant(Elephant elephant, IceBox iceBox){System.out.println(this.getName() + "把" + elephant.getName() + this.action + iceBox.getName());}public void closeIceBox(IceBox iceBox){iceBox.close(this);}public People(String name, String action){this.name = name;this.action = action;}public String getName() {return name;}}


package bigDemo.ObjectOriented;/** * 面向对象实现装大象进冰箱 *  * @Description * @author FuJianyong * 2015-6-30上午10:14:26 */public class ObjectOriented {public static void main(String[] args) {final IceBox iceBox = new IceBox("冰箱");final Elephant elephant = new Elephant("大象");final People people = new People("小明", "推进");        people.openIceBox(iceBox);        people.pushElephant(elephant, iceBox);        people.closeIceBox(iceBox);}}

运行结果:
小明把冰箱打开
小明把大象推进冰箱
小明把冰箱关闭

如果测试类中的引入新的对象,people类新增一个方法,即可变成完成另外一件事。
package bigDemo.ObjectOriented;/** * 牛奶类 * * @author FuJianyong * 2015-7-2下午10:05:10 */public class Milk {private String name;   public Milk(String name){this.name = name;}public String getName() {return name;}}

people类中加入一个方法
public void pushMilk(Milk milk, IceBox iceBox){System.out.println(this.getName() + "把" + milk.getName() + this.action + iceBox.getName());}


package bigDemo.ObjectOriented;/** * 面向对象实现装大象进冰箱 *  * @Description * @author FuJianyong * 2015-6-30上午10:14:26 */public class ObjectOriented {public static void main(String[] args) {final IceBox iceBox = new IceBox("冰箱");final Milk milk = new Milk("牛奶");final People people = new People("小白", "放进");        people.openIceBox(iceBox);        people.pushMilk(milk, iceBox);        people.closeIceBox(iceBox);}}


运行结果:
小白把冰箱打开
小白把牛奶放进冰箱
小白把冰箱关闭

2、面向过程
package bigDemo.ProcessOriented;/** * 面向过程实现装大象进冰箱 * @Description * @author FuJianyong * 2015-7-1下午05:08:47 */public class ProccessOriented {public void openIceBox(String people, String elephant, String iceBox){System.out.println(people + "打开冰箱");pushElephant(elephant, iceBox);}public void pushElephant(String elephant, String iceBox){System.out.println("装" + elephant + "进" + iceBox);closeIceBox(iceBox);}public void closeIceBox(String iceBox){System.out.println("关闭" + iceBox);}public static void main(String[] args) { new ProccessOriented().openIceBox("小明", "大象", "冰箱");}}


运行结果:
小明打开冰箱
装大象进冰箱
关闭冰箱
上一篇:ztree异步加载

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客