一 in python, what is object-oriented (oop: object oriented programming)

process-oriented:focus on how to do

1. All steps to complete a certain requirement Step by step from start to finish 2. According to the development requirements, some functionally independent code is packaged into one function after another. 3. The final code is to call different functions in sequence. Features: 1. Pay attention to steps and processes, and pay no attention to division of responsibilities 2. If the requirements are complex, the code will become very complicated 3. Develop complex projects, there is no fixed routine, the development is very difficult

object-oriented:Who will do it Compared to functions, object-oriented is a larger package that encapsulates multiple methods in an object according to its responsibilities. 1. Before you complete a certain requirement, first determine the responsibility - what to do (method) 2. Identify different objects according to their responsibilities, and package different methods (multiple) inside the object. 3. The final completion of the code, is the order to let different objects call different methods Features: 1. Focus on objects and responsibilities, different objects assume different responsibilities 2. More suitable for complex demand changes, is to specifically deal with the development of complex projects, providing fixed routines 3. On the basis of process-oriented, learn some object-oriented syntax

二, familiar with object-oriented programming (python iava c++)

: is a group with the same characteristics or behavior A general term for things, can not be used directly (for example: aircraft manufacturing drawings can not fly to heaven) Feature: Attribute Behavior: Method 对象: A specific existence created by the class can be used directly.

Object creation: Object variable = Class name ()

* In the program development: There should be a class and then the object

类的设计1. Class name of this kind of thing Name, satisfying the big hump nomenclature (the first letter of each word is capitalized, there is no underline between words and words) 2. Attributes What kind of characteristics are created by this class? If you don't know what initial value to set, you can set it to None, indicating that there is nothing, indicating an empty object, no methods and attributes, is a special attribute, you can Assign None to any variable. Assignment format: self. attribute name = attribute value 3. Method What kind of behavior does the object created by this class have? 类的制作class class name: Def method 1 (self): Pass Def method 2 (self): Passselfmeaning(1) Which method is called by the object, which is the reference of which object (2) within the method can access the object property class object life cycle through self (1) can access the object's properties and methods in the object's life cycle (2) An object is created from the calling class name, starting with the life cycle (3) Once the __del__ method of an object is called, the declaration period ends. PRIVATE attribute(1) The outside world cannot directly access, but can be accessed by method call. (2) Definition: Add two underscores in front of the attribute to define the private attribute. (3) The property that the object does not want to be exposed 私方法(1) The outside world cannot directly call (2) Definition: Add two underscores in front of the method to define the private method (3) The method that the object does not want to be publicized

三, three characteristics of object-oriented

1. Package

  • encapsulates attributes and methods into an abstract class (why abstract, because class Can not be used directly)
  • outside use class to create objects, and then let the object call method (the details of the method are encapsulated inside the class)

Example: the need to write a human, Xiao Ming love running, weight 75.0, each The second run will lose weight by 0.5 kg, and each time you eat, it will increase by 1 kg. The weight of Xiaomei is 45.0 kg.

class Person:
    Def __init__(self,name,weight):
        #Add two parameters in the initialization method to be passed by the outside world
        # self.properties = formal parameters
        Self.name = name
        Self.weight = weight
    Def __str__(self):
        Return 'My name is %s and the weight is %.2f' %(self.name,self.weight)
    Def run(self):
        Print '%s love running' %self.name
        # Inside the object method, is the property that can directly access the object
        Self.weight -= 0.5
    Def eat(self):
        Print '%s eat' %self.name
        Self.weight += 1

Xx = Person('小明', 75.0)
Print xx

# Attribute duplicate interference between multiple objects created by the same class
Xm = Person('小美', 45.0)
Print xm

2. Inherit

(1) to achieve code reuse, the same code does not need to be repeated (2) The subclass owns all the properties and methods of the parent class. The subclass inherits from the parent class and can enjoy the methods that the parent class has encapsulated. The subclass should encapsulate the properties and methods unique to the subclass according to its own responsibilities. (3) definition method class class name (parent class): Def subclass method (4) Inheritance has transitivity. The subclass owns the properties and methods of the parent class of the parent class, which is equivalent to the attributes and methods of the grandson who owns the grandfather. (5) Subclasses are also called derived classes. Parent classes are also called base classes. Inheritance is also derived

3. Polymorphism

(1) is based on encapsulation and inheritance. Different subclass objects call the same. Method that produces different results

四, parent class method

Override of parent class method(1) Override the method of the parent class. If the method of the parent class is overridden in the child class, only the method that is overridden in the child class will be called during the run, and the parent will not be called. Class method (2) If you need to display the method that is overridden in the parent class, you need to call the method that was originally overridden in the parent class: parent class name. parent class method (self) (3) Add parent class methods for extended coverage in subclasses (4)! ! ! The initialization method may also be overridden, so you need to call the initialization method of the parent class in the initialization method of the subclass

to inherit the private property of the parent class and the private method (1) in the external subclass object and the parent class Objects cannot directly access the private and private methods of the parent class object (2) In the object method of the subclass, you cannot access the private properties and methods of the calling parent class. (3) If the subclass needs to call the parent class private property and private method: define a public method in the parent class, call the parent class private property and private method in the parent class's public method, then the child class and the parent class object You can indirectly call the parent class's private method and private property by calling the public method of the parent class.

多继承(1)You can make the child class object have multiple parent class properties and methods at the same time. (2) Inheriting

五, new class old class

1.object : is the base class provided by Python for all objects, provides some built-in properties and methods, you can use dir function to view 2. New class: class based on object, recommended 3. Classic class: not based on object, not recommended 4. The object class is used by default in python3.x. If python2.X is not specified, object will not be used as the base class. 5. Format: class A (object):