Python3 --- object-2

Foreword

This article describes the definition of the object, use

2020-01-02-09:58:27

Sky lone

  0X01; What is class?

  Class (Class):  used to describe a collection of objects having the same properties and methods. It defines the set of properties and methods common to each object. Objects are instances of classes.

  Local variables: variables defined in the process, only the role of the current instance of the class.

  Examples of variables: the class declaration, the attribute is represented by a variable, the variable is called an instance variable, instance variable is a self-modified variables.

1 '' ' 
category 2 is defined: the keyword "class" identifies the class definition, "ClassName" identifies the class name. 
. 3 '' ' 
. 4 class ClassName: 
. 5 <-Statement. 1> 
. 6     . 
. 7     . 
. 8 <Statement-N>

  0X02; What is an object?

  Object: a data structure example defined by the class. Objects include two data members (instance variables and class variables) and methods.

  Method: function defined in the class.

  Class variables: Class variables are common throughout the instantiated object. And class variables defined outside the body of the function in the class. Class variables are typically not used as instance variables.

. 1 class Myclass: 
 2 '' 'defined class' '' 
 . 3 I = 1234567 
 . 4 DEF F (Self): # define a class method 
 . 5 return 'Hello World' 
 . 6 
 . 7 # class is instantiated 
 . 8 X = Myclass () 
 . 9 # access properties and methods of a class 
10 print ( "attribute class I Myclass as:" , XI) 
. 11 Print ( "method Myclass class f is:", xf ())
1 '' 'run results:' '' 
2 C: \ the Users \ Aaron \ Desktop \ Pytoon-Cade \ Venv \ the Scripts \ python.exe C: / the Users / Aaron / Desktop / Pytoon-Cade / urllib- Study.py 
. 3 i Myclass class attribute is: 1234567 
. 4 method f Myclass class is: Hello World 
. 5 
. 6 Finished Process Exit with code 0

  0X03; What is inherited?

  Inheritance: that is, a derived class (derived class) inherits the base class (base class) fields and methods. Inheritance also allows a derived class object as a base class object treated.

  Syntax:

Class. 1 DerivedClassName (BaseClassName): 
2 <-Statement. 1> 
. 3     . 
. 4     . 
. 5 <Statement-N> 
. 6 '' ' 
. 7 Note: BaseClassName (the name of the base class in the example) must be defined in a scope with the derived class. 
8 '''

  For example single inheritance:

1 #! To python3 
 2 
 . 3 # define a class 
 . 4 class mycalss: 
 . 5 name = '' 
 . 6 Age = 0 
 . 7 __weight = 0 
 . 8 the __init__ DEF (Self, n-, A, W):  . 9 the self.name = n-  10 self.age = A. 11 .__ weight = Self W DEF 12 is Speak (Self): 13 is Print ( "% S: I% d years old."% (the self.name, self.age)) # 14 15 class inherits class 16 Student (mycalss):. 17 = Grade '' DEF 18 is the __init__ (Self, n-, a, W, G): #. 19 call the parent class function mycalss .__ init__ 20 is (Self, n-, a, W) 21 is self.grade = G # 22 is overwritten parent DEF methods of the class 23 Speak (Self): 24-Print ( "% S says: I am% d years old, and I'm reading now Grade% d"%(self.name,self.age,self.grade)) 25 26 s = student('Peter',10,6,3) 27 s.speak()
1 '' 'run results:' '' 
2 C: \ the Users \ Aaron \ Desktop \ Pytoon-Cade \ Venv \ the Scripts \ python.exe C: / the Users / Aaron / Desktop / Pytoon-Cade / urllib- Study.py 
. 3 Peter said: I am 10 years old, and I am now reading Grade 3 
4 
with Exit code 0 5 Process Finished

  For example multiple inheritance:

  Note: Note that the order of the parent class parentheses, if the same method name of the parent class, but not specified in the subclasses, python from left to right search method that is not found in the sub-class, from left to Find the right parent class contains methods.

1 #! To python3 
 2 
 . 3 # defined in the first class 
 . 4 class mycalss: 
 . 5 name = '' 
 . 6 Age = 0 
 . 7 __weight = 0 
 . 8 the __init__ DEF (Self, n-, A, W):  . 9 the self.name = n-  10 = self.age .__. 11 Self weight = A W DEF 12 is Speak (Self): 13 is Print ( "% S says: I% d years"% (the self.name, self.age)) 15 # 14 define a second class class 16 Speaker : Topic =. 17 '' 18 is name = '' the __init__ DEF. 19 (Self, n-, T): 20 is the self.name = n-21 is self.topic = T DEF 22 is Speak (Self): 23 is Print ( "my name% s, I am an orator, and now my speech theme is S% "% (self.name, self.topic)) 24-# 25 26 class multiple inheritanceSample (Speaker, mycalss): 27 A = '' 28 DEF the __init__ (Self, n-, A, W, G, T): 29 mycalss .__ init__ (Self, n-, A, W) 30 Speaker .__ init__ (Self, n- , T) = 31 is 32 Test Sample ( 'Tim', 25,3,4, 'the Python' ) 33 is test.speak () # same method name, a method is called by default before the parent row in parentheses
# 1 operating results: 
2 C: \ the Users \ aaron \ Desktop \ Pytoon-Cade \ Venv \ Scripts \ python.exe C: / the Users / aaron / Desktop / Pytoon-Cade / urllib- Study.py 
3 My name is Tim, I is an orator, and now my speech theme is Python 
4 
5 Process Finished with Exit code 0

  0X04; What is overridden method?

  Method overrides: If you can not meet the needs of the subclass inherits from a parent class, can be rewritten, this process is called covering methods (override), also known as the overriding method.

. 1 # Python3! 
 2 class the Parent: # parent class 
 . 3 DEF the myMethod (Self): 
 . 4 Print ( 'call the parent class method' ) 
 . 5 
 . 6 class Child (the Parent): # subclasses 
 . 7 DEF the myMethod (Self): 
 . 8 Print ( " call subclass method " )  . 9  10 C = child ()  . 11 C.myMethod () method call # subclass after overwriting  12 super (child, C) .myMethod () # call the parent's method
Results Run # 1: 
2 C: \ the Users \ Aaron \ Desktop \ Pytoon-Cade \ Venv \ the Scripts \ python.exe C: / the Users / Aaron / Desktop / Pytoon-Cade / urllib- Study.py 
. 3 call subclass Method 
4 call the parent class method 
5 
6 Process Finished with Exit code 0

  0X05; what is private class method?

  __private_method : two begins with an underscore, the method is declared as a private method can only be called within the class, external calls can not be in class. self .__ private_methods.

1 #! To python3 
 2 
 . 3 class A: 
 . 4 __s private variable # 0 = 
 5 b = 0 # discloses variable 
 . 6 
 . 7 DEF COUNT (Self): 
 . 8 Self .__ = S +. 1 
 . 9. 1 self.b, + = 
10 Print (Self .__ S ) 
. 11 
COUNT = 12 is A ()  13 is count.count ()  14 Print (count.b) 15 Print (COUNT .__ S) # error, can not access the private instance variables
Results Run # 1 
 2 C: \ the Users \ Aaron \ Desktop \ Pytoon-Cade \ Venv \ the Scripts \ python.exe C: / the Users / Aaron / Desktop / Pytoon-Cade / urllib- Study.py 
 . 3 1 
 . 4 1 
 . 5 Traceback ( Recent Last Call MOST): 
 . 6 File "C: /Users/aaron/Desktop/Pytoon-cade/urllib-Study.py", Line 15, in <Module1> 
 . 7 Print (COUNT .__ S) # error, you can not access the private instance variable 
 . 8 AttributeError: 'A' has NO Object attribute '__s' 
 . 9 
10 Process Finished with Exit code. 1

  Proprietary methods class:

  • __init__:  constructor is called when an object is generated
  • __del__:  destructor, used when the object is released
  • __repr__:  printing, conversion
  • __setitem__:  according to the index assignment
  • __getitem__:  get the value by index
  • __len__:  get length
  • __cmp__:  comparison operation
  • __call__:  function call
  • __add__:  add operation
  • __sub__:  subtraction
  • __mul__:  multiplication
  • __truediv__:  In addition to operations
  • __mod__:  remainder operator
  • __pow__:  power

  0X0N; using common way of example:

  Method class; A

  Within the class, the use of  def keyword to define a method, different from the general definition of the function, class method must contain parameters self, and is the first parameter, representative of the Self are instances of classes.

!. 1 Python3.8 # 
 2 
 . 3 # define a class 
 . 4 class people: 
 . 5 # defines the basic properties 
 . 6 name = '' 
 . 7 Age = 0 
 . 8 # define private property, private property can not be directly accessed from outside the class 
 . 9 __weight = 0 
10 # The method defined construction 
. 11 the __init__ DEF (Self, n-, A, W):  12 is the self.name = n-  13 is self.age = A 14 Self weight .__ = W DEF 15 Speak (Self): 16 Print ( "% S: I % d years old. "% (the self.name, self.age)). 17 # 18 is instantiated class people = P. 19 ( 'Aaron', 10,30 ) 20 is p.speak ()
# 1 operating results: 
2 
3 C: \ the Users \ aaron \ Desktop \ Pytoon-Cade \ Venv \ Scripts \ python.exe C: / the Users / aaron / Desktop / Pytoon-Cade / urllib- Study.py 
4 aaron says: I 10 years old. 
5 
6 Exit Process Finished with code 0

Guess you like

Origin www.cnblogs.com/aaron456-rgv/p/12132184.html