current position:Home>Python notes - day15 - object oriented programming

Python notes - day15 - object oriented programming

2022-05-15 06:03:55Wandering mage 12

Preface
python Grammar learning , Leave it to those who need it , Understand everything !!

# coding=utf8
# @time:2022/4/24 20:27
# Author  Haoyu 


# 1. Programming idea 
# 1) Process oriented programming ( The poor ) -  When you encounter a problem, you immediately think of the code to realize the logic to solve the problem 
#  Tools : Logic 、 Algorithm 

# 2) Functional programming ( Small endowment ) -  When I encounter a problem, I immediately think of whether an existing function can help me solve this problem ; without , Define a function that can solve this problem 
#  Tools : function 

# 3) object-oriented programming ( Local tyrants ) -  When you encounter a problem, you immediately think about whether an object has the ability to solve this problem , If not, create a class that can create such an object 
#  Tools : class 、 object 

#  One 、 Recognize classes and objects 
# 1. What is a class? , What is an object ?
'''''''''
 class : Class is a collection of objects with the same functions and properties .( Classes are abstract )
 object : An instance of a class is an object ( The object is concrete )

 From a life perspective :
 If people are human , You are the object of people , You are another object of people ;
 If the car is a class , Bicycle 、 The bus 、 Cars are a subclass of cars ( classification ), Only a specific car is the object 
'''''''''

# 2. Defining classes  -  Describe clearly through code that you are a collection of objects with the same properties and functions 
'''''''''
 grammar :
class  Class name :
     Class 
     The content of the class ( Class properties and class methods )
    
 explain :
class   -    keyword : Fixed writing 
 Class name   -    Programmers name themselves :
          requirement : Is an identifier. ; It can't be a keyword 
          standard : Hump naming ( Words are capitalized ), title case ; Know what you know ; Do not use the module name provided by the system 、 Class name 、 Function name ...
 Class   -    Comments with three double quotes , It is mainly used to describe the functions of classes .( not essential )
 The content of the class     -    Include the properties of the class ( Class properties and object properties ) And methods of classes ( Object methods 、 Class method 、 Static methods )
'''''''''

# 3. Create objects 
'''''''''
 grammar :
 Class name ()
'''''''''

'''''''''
class Student:
    """
     This is a student class 
     attribute : Gender 、 Age 、 Student number 、 major 
     function : Study 、 play 
    """
    def __init__(self):
        self.name=' Xiao Ming '
        self.age=18
        self.study.id='001'
        self.major=' Computer '


    def study(self):
        print(' study hard , Day day up ')

    def play(self):
        print(' Summoner Canyon about a wave !')


#  Create two student objects 
s1=Student()
s2=Student()
print(s1 is s2)
'''''''''

#  Two 、 Construction method and initialization method ( function )
# 1. What construction method 
#  Name and class have the same name , The function is to create the object of the corresponding class 
# python When defining a class , The system will automatically create the corresponding constructor for this class 

#  Magic methods : In class __ At the beginning and __ The functions at the end are collectively called magic methods ; This kind of method does not need to be called by the programmer , The system will automatically call... When appropriate ;
# 2. Initialization method :__init__
'''''''''
1) When creating objects through classes , The system will automatically call... In this class __init__ Method ;
2) In class __init__ Method time , Just guarantee the method name and default parameters self Beyond immobility , Programmers can add parameters and function bodies at will according to the situation ;
3) Do you need parameters when calling the constructor to create an object , You need several parameters to see... In the corresponding class __init__ Method besides self Are there any additional formal parameters other than , There are several ;


 Be careful : Add parameter :
class Dog:
    def __init__(self,name,age):
        print(name,age)
d1=Dog(' rhubarb ',14) #  rhubarb  14
d2=Dog(' Xiao Huang ',17) #  Xiao Huang  17
'''''''''
'''''''''
class Person:
    def __init__(self):
        print('init')
p1=Person()
p2=Person()
'''''''''

#  3、 ... and 、 Attributes of a class 
#  The properties in the class are divided into : Class properties ( Class field ) And object properties 
'''''''''
 Class properties :
 Object properties :
'''''''''

More secure sharing , Please pay attention to 【 Security info】 WeChat official account !

copyright notice
author[Wandering mage 12],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/131/202205110607424111.html

Random recommended