1.案例
2.代码实现
面向过程
java
import java.util.Scanner;
/**
* Procedure Oriented Programming 面向过程编程
*/
public class POP {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("1.学生成绩管理");
System.out.println("2.学生选修管理");
System.out.println("3.退出系统");
System.out.println("请选择:");
int input = sc.nextInt();
if(input == 1){
outer:
while(true){
System.out.println("1.增加成绩");
System.out.println("2.修改成绩");
System.out.println("3.删除成绩");
System.out.println("4.增加查询成绩");
System.out.println("5.返回主菜单");
System.out.println("请选择菜单编号");
int order = sc.nextInt();
switch(order){
case 1 :
System.out.println("你选择了增加成绩");
break;
case 2 :
System.out.println("你选择了修改成绩");
break;
case 3 :
System.out.println("你选择了删除成绩");
break;
case 4 :
System.out.println("你选择了查询成绩");
break;
case 5 :
break outer;
}
}
}else if(input ==2){
System.out.println("你选择了选课系统");
}else{
System.out.println("感谢使用XX系统");
break;
}
}
}
}
面向对象
分析:
a.该过程涉及到的对象(事物)有两个:用户和菜单
b.用户拥有执行增删改查的动作
java
public class Menu {
public int order;//编号
public String name;
public Menu(int order,String name){
this.order=order;
this.name=name;
}
public void show(){
System.out.println(order + "." + name);
}
}
java
public class User {
public void addScore(){
System.out.println("你选择了增加成绩");
}
public void deleteScore(){
System.out.println("你选择了删除成绩");
}
public void updateScore(){
System.out.println("你选择了修改成绩");
}
public void searchScore(){
System.out.println("你选择了查询成绩");
}
}
java
import java.util.Scanner;
/**
* Object Oriented Programming 面对对象编程
*/
public class OOP {
public static Menu[] mainMenus = {
new Menu(1,"学生成绩管理"),
new Menu(2,"学生选课管理"),
new Menu(3,"退出系统")
};
public static Menu[] secondMenus = {
new Menu(1,"增加成绩"),
new Menu(2,"修改成绩"),
new Menu(3,"删除成绩"),
new Menu(4,"查询成绩"),
new Menu(5,"返回主菜单")
};
public static Scanner sc = new Scanner(System.in);
public static User user = new User();
public static void main(String[] args) {
gotoMain();
}
//去主菜单
public static void gotoMain(){
showMenus(mainMenus);//显示主菜单
int number = sc.nextInt();
if(number == 1){
gotoSecond();
}else if(number == 2){
System.out.println("你选择了学生选课管理");
gotoMain();//去主菜单
}else{
System.out.println("感谢使用XX系统");
}
}
//去二级菜单
public static void gotoSecond(){
showMenus(secondMenus);//显示二级菜单
int order = sc.nextInt();
switch(order){
case 1:
user.addScore();//用户增加成绩
gotoSecond();
break;
case 2:
user.updateScore();//用户修改成绩
gotoSecond();
break;
case 3:
user.deleteScore();//用户删除成绩
gotoSecond();
break;
case 4:
user.searchScore();//用户查询成绩
gotoSecond();
break;
case 5:
gotoMain();
break;
}
}
public static void showMenus(Menu[] menus){
for(int i=0;i<menus.length;i++){
menus[i].show();;
}
System.out.println("请选择菜单编号:");
}
}
对比
面向过程侧重点在过程的实现上。面向对象的侧重点在对象上,需要利用对象的行为来完成过程的组装。