Content of this section

1、self keyword

2、 encapsulation

3、 Inherit

4、 Static methods

One 、self keyword

effect : The object that calls the current method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class  Foo:
     
     # Static fields or public properties
     country  =  " China "
 
     def  __init__( self ,name,count):
         self .name  =  name
         self .count  =  count
 
     def  bar( self ):
         pass
 
obj1  =  Foo( " jiangsu " , 100000 )
obj1.bar

Be careful :country = " China " Belongs to public field , Its function is : When you save the same thing in each object , You can use static fields

Two 、 encapsulation

  • Class encapsulates : Field 、 Method
  • Object is encapsulated in : Value of normal field
  • Not only can you encapsulate fields , You can also encapsulate objects
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class  F1:
     def  __init__( self ,n):
         self .N  =  n
         print ( "F1" )
     
class  F2:
     def  __init__( self ,arg1):
         self .a  =  arg1
         print ( "F2" )
 
class  F3:
     
    def  __init__( self ,arg2):
         self .b  =  arg2
         print ( "F3" )
 
o1  =  F1( "alex" )
o2  =  F2(o1)
o3  = F3(o2)
 
###### Output alex####
# o3 =F3(o2)
o3  = = = = > o2
 
# o2 = F2(o1)
o3.b.a
 
# o1 = F1("alex")
o1.b.a.N

3、 ... and 、 Inherit

explain : Subclass inherits parent , When an object calls a method , Start with your own class , Can't find , You go to the method in the parent class .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class  F1:
     def  __init__( self ):
         print ( 'F1' )
 
     def  a1( self ):
         print ( "F1a1" )
 
     def  a2( self ):
         print ( "F1a2" )
 
class  F2(F1):
     def  __init__( self ):
         print ( "F1" )
 
     def  a1( self ):
         print ( "F2a1" )
 
     def  a2( self ):
         self .a2()
         print ( "F2a2" )
 
class  F3(F2):
     def  __init__( self ):
         print ( "F3" )
 
     def  a2( self ):
         print ( "F3a2" )
 
obj  =  F3()
obj.a1()
 
# Output
F3a2
F2a1

The order of access to the above code is as follows :

  1. Go first to F3 In looking for a1 Method , Can't find
  2. To F2 Find the parent , eureka a1 Method , To carry out a1 Method
  3. During execution, you need to call F3 Medium a2 Method

Four 、 Static methods

4.1 Class composition

  • Field :1. Common field ( Save in object ),2 Static field ( Save in class )
  • Method : Common method ( Save in class , Using objects to call )
1
2
3
4
5
6
7
class  F1:
     age  =  18   # Static field
     def  __init__( self ,name):
         self .name  =  name   # Common field
 
     def  a1( self ):    # Common method
         print ( 'F1a1' )

4.2 Static methods

characteristic :

  1. Save in class
  2. The caller does not need to create an object
  3. Can have any parameter
1
2
3
4
5
6
7
8
class  F1:
     
     @staticmethod    # Declarations are static methods
     def  a1(n1,n2):    # No need to pass in objects , But you can declare multiple values
         print ( 'xiaogao' )
 
 
F1.a1( 1 , 2 )    # Class name . Method name Way to call

effect : When the object doesn't need to pass in parameters , It's wasteful to create objects , Taking up resources , So it's better to call it directly with the class name , This is like a function .

object-oriented 【day07】: Knowledge review ( 11、 ... and ) More articles about

  1. Preparation before interview ---C# Knowledge review ----03

    After a day's running , mixed , It's not a detailed list Let's go back to the title ,C# Knowledge review Let's look at the content , database 3NF Forgive me for the title party Today, I will continue to recall 1.HTTP in Post and Get difference It's so simple , Do you feel excited , Changshu ...

  2. Spring Knowledge review (08)spring aware

    Spring Knowledge review (08)spring aware BeanNameAware Get... In the container bean name BeanFactoryAware Get current bean factory Applicatio ...

  3. Spring Knowledge review (07) Event publishing and monitoring

    Spring Knowledge review (07) Event publishing and monitoring 1.DemoEvent extends ApplicationEvent { public DemoEvent(Object source, String ...

  4. Spring Knowledge review (01)Java Config

    Spring Knowledge review (01) One .Java Config 1. Services and service injection 2.Java annotation : More powerful 3. Test verification Two . Annotation injection 1. Services and service injection 2. Configuration is loaded 3. Test verification 3、 ... and . summary ...

  5. Java Advanced ( hexacosa- ) Project development knowledge review of the company

    Project development knowledge review of the company Preface " si " It's not the best choice at some point , Especially when you have problems , No clue . In the course of an experiment , We need to implement the CRUD operation . Due to the previous project development process , ...

  6. $Django Rest Framework- Authentication component , Permission component Knowledge review choices,on_delete

    One Little knowledge review #orm class UserInfo (models.Model): id = models.AutoField (primary_key=True) name = models. ...

  7. django Review and supplement of knowledge points

    One .django Knowledge review 1.Cookie operation - Key value pairs stored locally in the client 2.Session operation - 3.URL route - /index -> view. function 4.MTV 5.View The returning party in the ...

  8. Spring Learning notes :Spring Integrate Mybatis(mybatis-spring.jar)( One : Knowledge review )

    One . Knowledge review 1.Mybatis Environment building (DAO The realization of the layer )( Use maven Project management tools ) Dependency packages that need to be introduced : <!-- unit testing junit --> <dependency> ...

  9. Zero basis Python Knowledge review ( One )

    If you are little white , It is recommended that you just install python-3.7.0-amd64.exe And then on the computer cmd Command prompt   Type to check if it is installed pip, Generally installed python There will be . >pip         ...

  10. Front end knowledge review ——HTML,CSS piece

    Front end knowledge review -- It's a note that I just changed my career and organized the front-end knowledge I learned for the interview , Personal purpose is very strong , For reference only . doctype What's the usage? doctype Document type declaration is a standard general markup language , The purpose is to tell mark ...

Random recommendation

  1. ARM The constant expression of

    ARM The constant expression of   if Intel The immediate number in the instruction , I believe everyone is familiar with . Allied ,Arm Directive “ Count now ” It's a constant expression . It's called a constant expression , There's a reason not to call it an immediate number . Intel Instructions belong to CISC finger ...

  2. loadrunner Support https How to operate the protocol - Summary of experience

    problem : user portal Support https agreement , use loadrunner When recording the login script, it was found that the user name and password were not recorded The recorded script is as follows : login() { lr_think_time(10); web_url(& ...

  3. soap Message mechanism Explain

    SOAP(Simple Object Access Protocol, Simple object access protocol ) As an information exchange protocol, it is widely used in distributed applications , Such as WebService. In the use of .Net Development WebService When , ...

  4. 【Objective-C】04- first OC Program parsing

    explain : This Objective-C project , It's learning iOS The prelude to development . Also in order to let have the object-oriented language development experience program ape , You can start at high speed Objective-C. Suppose you don't have programming experience , Or right Objective-C.iOS Development ...

  5. Python The first entry program

    #!usr/bin/env python3 # stay UNIX On , When a program is referenced in the console , The first two bytes of the file are read in first . If these two bytes are ASCII character #!, #shell It is assumed that the file will be executed by the interpreter , ...

  6. QObject There is eventFilter, Very powerful ( Do whatever you want , For example, for QLineEdit participle )

    I believe everyone has used dictionaries ! Because English is not very good ...O(∩_∩)O~, So we often translate words ! sketch Realization effect Source code More reference Realization principle : Mouse over a word , Get mouse position , And then take the words in the corresponding position , translate ! be based on ...

  7. BZOJ 1426: Collect stamps [DP expect square ]

    Portal The question : Yes n Two different stamps , Pipi wants to collect all kinds of stamps . The only way to collect it is to buy it from my classmate Fanfan , You can only buy one at a time , And the stamps I bought are n Which of these stamps is equal probability , The probabilities are 1/n. But because Fanfan also likes to post ...

  8. R-- Apply Family functions

    APPLY Family functions : apply(x,a,f) A function acting on a dimension of a matrix or data frame fx For matrix or data frame :a by 1 On behalf of the line ,a by 2 Representative column :f Is the action function . lapply(x,f) Yes x Every tuple function of f, The result is ...

  9. ESLint Specific rule settings

    "no-alert": 0,// No use alert confirm prompt "no-array-constructor": 2,// Array constructors are prohibited & ...

  10. Python Crawler directory

    Python Crawler directory Tool use Pycharm Connect Linux Remote development mongodb stay windows Lower installation start Crawler bag grabbing tool Fiddle Set up Crawler bag grabbing tool Charles Set up Reptile tools fiddle stay ...