当前位置:网站首页>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)
边栏推荐
- Recursive structure
- Leecode 122. Zuijia timing of buying and selling stocks ②
- JVM family -- monitoring tools
- Zlmediakit compilation and webrtc push-pull flow testing
- Aperçu du code source futur - série juc
- The first spring of the new year | a full set of property management application templates are presented, and Bi construction is "out of the box"
- Hospital network planning and design document based on GLBP protocol + application form + task statement + opening report + interim examination + literature review + PPT + weekly progress + network to
- Sword finger offer:55 - I. depth of binary tree
- 2022 examination summary of quality controller - Equipment direction - general basis (quality controller) and examination questions and analysis of quality controller - Equipment direction - general b
- 1day vulnerability pushback skills practice (3)
猜你喜欢

Package and download 10 sets of Apple CMS templates / download the source code of Apple CMS video and film website

New year's first race, submit bug reward more!

SQL injection (1) -- determine whether there are SQL injection vulnerabilities

Want to do something in production? Then try these redis commands

I stepped on a foundation pit today

Code Execution Vulnerability - no alphanumeric rce create_ function()
![Backpropagation formula derivation [Li Hongyi deep learning version]](/img/ef/f76eae39c4f8716a0030a60c85b09c.gif)
Backpropagation formula derivation [Li Hongyi deep learning version]

Li Chuang EDA learning notes 13: electrical network for drawing schematic diagram

How to use websocket to realize simple chat function in C #

The 37 year old programmer was laid off, and he didn't find a job for 120 days. He had no choice but to go to a small company. As a result, he was confused
随机推荐
Basé sur... Netcore Development blog Project Starblog - (14) Implementation of theme switching function
Which product is better for 2022 annual gold insurance?
I stepped on a foundation pit today
[Valentine's Day confession code] - Valentine's Day is approaching, and more than 10 romantic love effects are given to the one you love
Webhook triggers Jenkins for sonar detection
基于PHP的轻量企业销售管理系统
Session learning diary 1
Short math guide for latex by Michael downs
Code Execution Vulnerability - no alphanumeric rce create_ function()
Love and self-discipline and strive to live a core life
Webhook triggers Jenkins for sonar detection
Stm32bug [the project references devices, files or libraries that are not installed appear in keilmdk]
Teach you how to optimize SQL
Résumé des outils communs et des points techniques de l'examen PMP
Day05 錶格
Jenkins configures IP address access
This function has none of DETERMINISTIC, NO SQL..... (you *might* want to use the less safe log_bin_t
Management and thesis of job management system based on SSM
Zigzag scan
Experience summary of the 12th Blue Bridge Cup (written for the first time)