由面向过程到面向对象

/ 0评 / 0

面向过程

        “面向过程”(Procedure Oriented)是一种以过程为中心的编程思想。好比一辆汽车,在面向过程中,我们关注的是一个汽车事件,而不是汽车本身,这个就比如:

 
public class 汽车运营{
	void 汽车启动{
	}
	void 汽车到站{
	}
}

面向对象

        “面向对象”(Object Oriented,简称OO)是一种以事物为中心的编程思想。而在面相对象中,我们的核心思想是“万物皆为对象”,先创建对象,再由对象去激发事物。

 
public class 汽车{
	void 到站(){
	}
	void 启动(){
	}
}

        面向过程其实是最为实际并且简单的一种思考方式,就算是面向对象的方法也是含有面向过程的思想。具体到代码的举例:

面相过程

 
void test1(){
	printf("调用了test1\n");
}

void test2();//如果这里不定义,在main中调用的时候报错,会提示找不到该函数

 int main(int argc, char const *argv[])
 {
 	test1();
 	test2();
 	return 0;
 }

 void test2(){
 	printf("调用了test2\n");
 }

面向对象

 
//虽然用JAVA举例,然而楼主并不喜欢JAVA
public class HelloWorld {
    public static void main(String[] args) {

         Test test = new Test();

         test.setRoleName("dadfasdfasfdsa");

         System.out.println("Helloworld! " + test.getRoleName);
    }
}

public class Test{
	private String roleName;

	public String getRoleName() {
		return roleName;
	}
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}
}

评论已关闭。