`
3213213333332132
  • 浏览: 78657 次
社区版块
存档分类
最新评论

面向对象面向过程

    博客分类:
  • java
阅读更多
面向对象:把要完成的一件事,通过对象间的协作实现。
面向过程:把要完成的一件事,通过循序依次调用各个模块实现。
我把大象装进冰箱这件事为例,用面向对象和面向过程实现,都是用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("小明", "大象", "冰箱");
	}
}



运行结果:
小明打开冰箱
装大象进冰箱
关闭冰箱
5
3
分享到:
评论
3 楼 3213213333332132 2015-07-03  
谢谢
2 楼 3213213333332132 2015-07-03  
Function 写道
IceBox  和  Elephant   本身已经表示 原意思了

个人感觉 setType("非洲");  setType("西门子");

输出: 小明把非洲大象推进西门子冰箱

后面还可以给  People IceBox Elephant 扩展更多的属性,让这句话更加的丰富



是的,你说的很对,我通过这样的代码,能引发别人去思考我觉得高兴。
其实这里只是简单的诠释面向对象的思维,要真正利用java的多态去完成,那会更丰富的。
1 楼 Function 2015-07-03  
IceBox  和  Elephant   本身已经表示 原意思了

个人感觉 setType("非洲");  setType("西门子");

输出: 小明把非洲大象推进西门子冰箱

后面还可以给  People IceBox Elephant 扩展更多的属性,让这句话更加的丰富

相关推荐

Global site tag (gtag.js) - Google Analytics