当前位置:网站首页>Object oriented -- encapsulation, inheritance, polymorphism
Object oriented -- encapsulation, inheritance, polymorphism
2022-07-04 03:33:00 【Naive code writing】
encapsulation :
# encapsulation
# The data ( attribute )、 Realization ( Method 、 function ) Put it inside the class to complete , Display an interface ( Functions that can be called ).
# in other words , Encapsulated classes , It's a black box .
# What do you want to do , Tell directly “ class ” A sound , Just fine , Class is automatically completed
# Compare with function , Class is a higher-level encapsulation ( More advanced functions )
# Of course , Class implements a higher level of encapsulation , It not only encapsulates the operation , It also encapsulates the data .
# such as , Construct a function show(), You can print the values of all variables . And don't care about the variable name and other specific information
# Use ,set、get Before setting , Judge the numerical logic
Construct a class , Construct a method in a class , It can display the corresponding attributes of this class .
Call directly :
class person():
def __init__(self,name,age,grade):
print("===== Start ======")
self.name=name
self.age=age
self.grade=grade
Zhang San =person(' Zhang San ',23,98)
print( Zhang San .name)
print( Zhang San .age)
print( Zhang San .grade)# It's too troublesome every time
encapsulation :
class person():
def __init__(self,name,age,grade):
print("===== Start ======")
self.name=name
self.age=age
self.grade=grade
def show(self):
print(self.name)
print(self.age)
print(self.grade)
print("===== end ======")
Zhang San =person(' Zhang San ',23,98)
Zhang San .show()# It can output directly
Li Si =person( Li Si ,24,88)
Li Si .show()
Wang Wu =person( Wang Wu ,25,66)
Wang Wu .show()
Inherit :
The child class inherits the parent class ( Base class ) Methods
Two understandings :
( From top to bottom )
Extract commonalities , Take developing games as an example :
such as , There are many different kinds of monsters in the game , No
There are many same attributes between the same monsters 、 action ( Method )
We construct a base class “ Monster ”
Put different kinds of monsters , Same property 、 Method , discharge
In the base class “ Monster ” Just inside .
( From top to bottom )
Better extension : Game recharge .
If , Coincident double 11, We can recharge in the class , increase
Add a double 11 Activities .
Meet the game anniversary , Add a discount .
The number of people online has reached 10 ten thousand , Add an event discount .
such , Increase , Recharge class , More and more big
It's troublesome to modify every time ,
Inherit , Every activity , All inherit from the base class .
class guaishou:
def shout(self):
print("AAAAAAAA")
class fei(guaishou):
def fly(self):#() Fill in the parent class , Defined jisuanji This class inherits ( contain )students Function of
print(" I can fly ....")
class shui(guaishou):
def swim(self):
print(" I swim in the water ...")
class lu(guaishou):
def run(self):
print(" Run! , brother ...")
xiaobai=guaishou()
xiaobai.shout()
feishou=fei()
feishou.shout()# You can call the function of the parent class directly ( Method )
feishou.fly()
shuishou=shui()
shuishou.shout()
shuishou.swim()
lushou=lu()
lushou.shout()
lushou.run()
Output results :
AAAAAAAA
AAAAAAAA
I can fly ....
AAAAAAAA
I swim in the water ...
AAAAAAAA
Run! , brother ...
practice :
class student():
def learn(self):
print(" Serious class ")
class jisuanji(student):#() Fill in the parent class , Defined jisuanji This class inherits ( contain )students Function of
def biancheng(self):
print(" Write good code ")
class jinrong(student):
def jinrongjiaoyi(self):
print(" Do business ")
Xiao Ming =student()
Xiao Ming .learn()
Xiaomei =jisuanji()
Xiaomei .learn()# You can call the function of the parent class directly ( Method )
Xiaomei .biancheng()
Little love =jinrong()
Little love .learn()
Little love .jinrongjiaoyi()
Output results :
Serious class
Serious class
Write good code
Serious class
Do business
polymorphic :
Reaction to the same phenomenon
Subclasses implement polymorphic methods
# Subclasses inherit the methods of the base class ( That is output ), No operation is needed to get the method of the base class ( Output )
# Subclasses execute polymorphic methods
# Construct a method that calls the base class A, Add new subclasses , This method can also be used A
practice :
• Construct a base class “ Student ” , contain “ Study ” Method .
• Construct an inheritance class “ Computer science students ”, Re implement the method “ Study ”.
• Construct an inheritance class “ Students majoring in finance ”, Re implement the method “ Study ”.
• Construct a function fun, Can call the base class “ Student ” Methods .
• Construct objects of different classes , Take it as a function fun Parameters of
class student():
def learn(self):
print(" Have a good class ")
class jisuanji(student):
def learn(self):
print(" Write good code ")
class jinrong(student):
def learn(self):
print(" Deal well ")
def fun(student):#() Inside is the class name
print("====!!!=====")
student.learn()
print("====####=====")
Xiao Ming =student()
Xiao Ming .learn()
xiaoming=jisuanji()
xiaoming.learn()
fun(xiaoming)
xiaomei=jinrong()
fun(xiaomei)
xiaomei.learn()
class meishu(student):
def learn(self):
print(" Drawing a picture ")
xiaowang=meishu()
xiaowang.learn()
fun(xiaowang)
边栏推荐
- Jenkins configures IP address access
- Problems and solutions of several concurrent scenarios of redis
- Webhook triggers Jenkins for sonar detection
- Redis transaction
- WP collection plug-in free WordPress collection hang up plug-in
- [PaddleSeg 源码阅读] PaddleSeg计算 mIoU
- Site favorites
- Command Execution Vulnerability - command execution - vulnerability sites - code injection - vulnerability exploitation - joint execution - bypass (spaces, keyword filtering, variable bypass) - two ex
- [Valentine's Day confession code] - Valentine's Day is approaching, and more than 10 romantic love effects are given to the one you love
- Contest3145 - the 37th game of 2021 freshman individual training match_ E: Eat watermelon
猜你喜欢

Setting methods, usage methods and common usage scenarios of environment variables in postman

WordPress collection WordPress hang up collection plug-in

Add token validation in swagger

The "message withdrawal" of a push message push, one click traceless message withdrawal makes the operation no longer difficult

Add IDM to Google browser

1day vulnerability pushback skills practice (3)

Zhihu million hot discussion: why can we only rely on job hopping for salary increase? Bosses would rather hire outsiders with a high salary than get a raise?

MySQL query

Imperial cms7.5 imitation "D9 download station" software application download website source code
![Stm32bug [the project references devices, files or libraries that are not installed appear in keilmdk]](/img/0d/7a8370d153a8479b706377c3487220.jpg)
Stm32bug [the project references devices, files or libraries that are not installed appear in keilmdk]
随机推荐
Why is it recommended that technologists write blogs?
【.NET+MQTT】.NET6 環境下實現MQTT通信,以及服務端、客戶端的雙邊消息訂閱與發布的代碼演示
super_ Subclass object memory structure_ Inheritance tree traceability
Teach you how to optimize SQL
Backpropagation formula derivation [Li Hongyi deep learning version]
[development team follows] API specification
I stepped on a foundation pit today
CSP drawing
Webhook triggers Jenkins for sonar detection
Code Execution Vulnerability - no alphanumeric rce create_ function()
How to pipe several commands in Go?
System integration meets the three business needs of enterprises
1day vulnerability pushback skills practice (3)
2022 attached lifting scaffold worker (special type of construction work) free test questions and attached lifting scaffold worker (special type of construction work) examination papers 2022 attached
选择排序与冒泡排序模板
Www 2022 | taxoenrich: self supervised taxonomy complemented by Structural Semantics
How to use websocket to realize simple chat function in C #
Love and self-discipline and strive to live a core life
Jenkins continuous integration environment construction V (Jenkins common construction triggers)
Contest3145 - the 37th game of 2021 freshman individual training match_ E: Eat watermelon