面向过程、面向函数、面向对象、函数式编程都该如何理解

面向过程编程

#include <stdio.h>
#include <stdlib.h>

/**
* 思路:
* 1、打开洗衣机
* 2、放入衣服
* 3、放入洗衣液
* 4、关上洗衣机
**/

int main() {
	// 1、打开洗衣机
    int size = 2;
    int* machine = (int*)malloc(size * sizeof(int));
    if (machine == null) {
        printf("内存分配失败\n");
        return 0;
    }
        
    // 2、放入衣服
    m[0] = 1;

    // 3、放入洗衣液
    m[1] = 2;
    
    // 4、关上洗衣机
    int result = 0;
    if (m[0] == 1 && m[1] == 2) {
        result = 1;
    }
    free(m);
    
    if (result == 1) {
    	printf("洗衣完成!");  
    } else {
    	printf("洗衣失败!");  
    }
    
    return = 0;
}

面对函数编程

#include <stdio.h>
#include <stdlib.h>

/**
* 思路:
* 1、Step1:打开洗衣机
* 2、Step2:放入衣服
* 3、Step3:放入洗衣液
* 4、Step4:关上洗衣机
**/

int main() {
	// 1、打开洗衣机
    int* machine = openWashingMachine();
    if (machine == null) {
        printf("内存分配失败\n");
        return 0;
    }
        
    // 2、放入衣服
    int cloth = 1;
    putClothToMachine(machine, cloth);
    
    // 3、放入洗衣液
    int liquid = 2;
    putLiquidToMachine(machine, liquid);
    
    // 4、关上洗衣机
    int result = closeWashingMachine(machine);
    if (result == 1) {
    	printf("洗衣完成!");  
    } else {
    	printf("洗衣失败!");  
    }
    
    return 0;
}

int* openWashingMachine() {
    int size = 2;
    int* m = (int*)malloc(size * sizeof(int));
    return m;
}

int closeWashingMachine(int* m) {
    int result = 0;
    if (m != null) {
        if (m[0] == 1 && m[1] == 2) {
            result = 1;
        }
    }
    
    free(m);
    return result;
}

void putClothToMachine(int* m, int cloth) {
    if (m == null) {
        return;
    }
    m[0] = cloth;
}

void putLiquidToMachine(int* m, int liquid) {
    if (m == null) {
        return;
    }
    m[1] = liquid;
}

面向对象编程

/**
* 思路:
* 1、洗衣机.打开()
* 2、人.放入衣服(洗衣机, 衣服)
* 3、人.放入洗衣液(洗衣机, 洗衣液)
* 4、洗衣机.关上()
**/

class WashingMachine {
	private int cloth;
	private int liquid;
    
    private final static WashingMachine MACHINE = new WashingMachine(); 
    
    public static WashingMachine open() {
        return MACHINE;
    }
    
    public boolean close() {
        if (cloth == 1 && liquid == 2) {
            return true;
        }
        return false;
    }
	
	public void setCloth(int cloth) {
		cloth = cloth;
	}
	
	public void setLiquid(int liquid) {
		liquid = liquid;
	}
}

class Person {
    public void putClothToMachine(WashingMachine m, int cloth) {
        if (m == null) {
            return;
        }
        m.setCloth(cloth);
        return;
    }
    
    public void putLiquidToMachine(WashingMachine m, int liquid) {
        if (m == null) {
            return;
        }
        m.setLiquid(liquid);
        return;
    }
}

public class WashingCloth {
    public static void main(String[] args) {
        // 1、打开洗衣机
        WashingMachine machine = WashingMachine.open();

        // 2、放入衣服
        Person person = new Person();
        int cloth = 1;
        person.putClothToMachine(machine, cloth);

        // 3、放入洗衣液
        int liquid = 2;
        person.putLiquidToMachine(machine, liquid);

        // 4、关上洗衣机
        boolean result = machine.close();
        if (result) {
            System.out.println("洗衣完成!");
        } else {
            System.out.println("洗衣失败!");
        }

        return 0;
    }
}

函数式编程

import java.util.Map;
import java.util.function.Consumer;

/**
* 思路:
* 1、洗衣机.打开()
* 2、放入衣服、洗衣液等原料
* 4、洗衣机.关上()
**/

class WashingMachine {
	private int cloth;
	private int liquid;
    
    private final static WashingMachine MACHINE = new WashingMachine(); 
    
    public static WashingMachine open() {
        return MACHINE;
    }
    
    public boolean close() {
        if (cloth == 1 && liquid == 2) {
            return true;
        }
        return false;
    }
	
	public void setCloth(int cloth) {
		cloth = cloth;
	}
	
	public void setLiquid(int liquid) {
		liquid = liquid;
	}
}

public class WashingCloth {
    public static void main(String[] args) {
        // 1、打开洗衣机
        WashingMachine machine = WashingMachine.open();
        
        Consumer<Integer> putClothToMachine = (cloth) -> {
            if (machine == null) {
                return;
            }
            machine.setCloth(cloth);
        }
        
        Consumer<Integer> putLiquidToMachine = (liquid) -> {
            if (machine == null) {
                return;
            }
            machine.setLiquid(liquid);
        }

        // 2、放入一系列原料
        Map<Integer, Consumer> table = Map.ofEntries(
            Map.entry(1, putClothToMachine),
            Map.entry(2, putLiquidToMachine)
        );
        putSomeToMachine(table);

        // 4、关上洗衣机
        boolean result = machine.close();
        if (result) {
            System.out.println("洗衣完成!");
        } else {
            System.out.println("洗衣失败!");
        }

        return 0;
    }
    
    putSomeToMachine(Map<Integer, Consumer> table) {
        for (Map.Entry<Integer, Consumer> entry : table.entrySet()) {
            entry.getValue().accept(entry.getKey());
        }
    }
}

参考:
https://blog.csdn.net/jiadajing267/article/details/121216442

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值