Let's also talk about object-oriented and class, and also about object-oriented
There is no long story here, and only pseudo code is here
Today I want to cook, stir-fry two dishes, one is fried tomato with eggs and the other is fried cucumber with eggs. Now, what I want to do is buy food in the market first, and then cook
If the process is oriented, I will write it like this.
------------------- Process-oriented -----------------
Dish 1 = eggs
Dish 2 = tomato
Dish 3 = cucumber
Buy food 1
Buy food 2
Buy food 3
Cooking 1 (EGG)
Cut Vegetables 2 (cut tomatoes)
Finished Product 1 = sauteed dish 1 + dish 2
Cooking 1
Cut Vegetables 3 (cut cucumbers)
Finished 2 = sauteed 1 + 3
Print finished 1
Print finished product 2
------------------- Process-oriented -----------------
Obviously, this is complicated and not easy to grasp and allocate, so you can change all of them to the object-oriented method of functions.
---------------- Object-oriented --------------------
Def buy (dish ):
Pay
Return
Def (dish ):
Dish = pass (dish)
Return
Def cut (dish ):
If len (dish) = long:
Dish = slice (dish)
Else:
Dish = cut (dish)
Return
Def fried (dish 1, dish 2 ):
Finished Product = dish 1 + dish 2
Return finished product
If _ name _ = '_ main __':
Dish 1 = buy (eggs)
Food 2 = buy (tomato)
Food 3 = buy (cucumber)
Finished Product 1 = stir-fried (dish 1), cut (dish 2 ))
Finished Product 2 = stir-fried (dish 1), cut (DISH 3 ))
---------------- Object-oriented --------------------
Now I still think it is awkward. Since both finished product 1 and finished product 2 are hot-fried, the product can be directly classified as one type.
---------------- Object-oriented, add class --------------------
Def buy (dish ):
Pay
Return
Class cooking (object ):
Def _ init _ (self, dish 1, dish 2 ):
Self. Dish 1 = dish 1
Self. Dish 2 = dish 2
Def (self ):
Food = pass (self. Dish 1)
Return
Def cut (self ):
If len (dish) = long:
Dish = slice (dish)
Else:
Dish = cut (dish)
Return
Def (self ):
Finished Product = self. () + self. Cut ()
Return finished product
If _ name _ = '_ main __':
Dish 1 = buy (eggs)
Food 2 = buy (tomato)
Food 3 = buy (cucumber)
Finished Product 1 = sauteed food (dish 1, dish 2)
Print finished product 1. Stir Fry ()
Finished Product 2 = fried (dish 1, DISH 3)
Print finished product 2. Stir Fry ()
---------------- Object-oriented, add class --------------------
Next to the class, I talked to my friend two days ago and I gave this example. He said it was hard to understand. Is that true?