面向对象:把要完成的一件事,通过对象间的协作实现。
面向过程:把要完成的一件事,通过循序依次调用各个模块实现。
我把大象装进冰箱这件事为例,用面向对象和面向过程实现,都是用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("小明", "大象", "冰箱");
}
}
运行结果:
小明打开冰箱
装大象进冰箱
关闭冰箱