Object-oriented, procedure-oriented, object-relational grade classes and bound method

An object-oriented:

         Object-oriented programming is an idea, is a way for programmers to better programming ideas.

The core is the object, the program is a set of objects, is responsible for scheduling control these objects to interact with the task.

 

 

In the object-oriented programmer angle changed into a particular conductor from the operator.

He stressed: object is not created out of thin air, we need to design their own

 

Object-oriented three major advantages:

  1. Scalability
  2. flexibility
  3. Reuse (code can be used multiple times)

 

Disadvantages:

  1. Increased complexity of the program
  2. We can not accurately predict the results

Be used: high scalability requirements of the program, typically directly to the user. For example QQ, micro-letter

 

Second, process-oriented

The core concern is the process, step by step process is the execution step in doing both before doing

 Process for the advantages:

Poor scalability, maintainability

 

scenes to be used:

Requirements for lower extension programs such as: system kernel, git, calculator

 

** not require any program object-oriented, have to analyze the specific needs

 

 

 

 

 

 

Third, the classes and objects

1. classes and objects

This is the core concepts oop (object-oriented programming) in

Categories: either type, class, an abstract concept, is a set of features and the same behavior with the same object

 

Object: a thing is the existence of specific, have their own characteristics and behavior of the object is a combination of characteristics and skills

 

         The relationship between classes and objects

         Class contains a series of objects, the object belongs to a class.

In life is to have only the object classes, and the program is the first class and then have the object, we have to tell the computer objects of this class has what features, what kind of behavior.

Summary: When using object-oriented programming, the first step is the need to think what kind of objects, objects have what kind of features and behavior, which summed up according to the type of information required.

 

2, create classes and objects

The syntax for defining classes

Class Name of the class:

Content class (described attributes and skills)        

 

Description property with variable skill with function

 

Name of the class writing requirements:

The first is intended to see to know the name of the name is big hump nomenclature

Hump ​​word is capitalized, the big hump is the first letter capitalized, small hump is the first letter lowercase

The syntax to create objects

Define a class

Class Person:

           Pass

 

Then create an object:

P = Person()

 

Properties can be written in a class, class attributes are common to all objects

Properties can also be written in an object, object properties, each object is unique (not the same)

 

Note: If the same attribute is present in the class and object, sequential access this property is the first property in an object, if not found in the object, then go inside to find the class attribute

 

Increase property

The object variable name. Attribute name = attribute value

 

Delete property

Variable name del objects. Attribute name

 

modify

Object = new attribute value

 

View property access all attributes of an object

print (Object .__ dict__)

 

Class information access objects

print (Object .__ class__)

        

__init__ method

Initialization method is called, is essentially a function

 

Features 1: When the object is instantiated, the method is performed automatically __init__

Feature 2: automatically object as the first argument, the parameter name is self, self can be another name, but it is not recommended to change

Function: an initial value of the user is attached to the object, the function can not have any return value, so only a predetermined None

 

Exercise: Create a class with several properties to set the property to him by the initialization method

class Dog:

    def __init__(self,kind,color,age):

        self.kind = kind

        self.color = color

        self.age = age

 

d1 = Dog ( "two ha", "black and white", 1)

d1 = Dog ( "Teddy", "brown", 2)

The essence of the object function is to integrate data and process the data together, so to get an object on both his function data and processing data to be processed

 

  1. Bound method object

Class methods are bound method objects by default

It is special because:

  1. When the function is called an object, the object itself automatically passed as the first argument
  2. When the class name to call, it is a normal function, there are several parameters to pass several parameters

 

Exercise: Write a class of students, with a greeting skills to be able to export its own name information

class Student:

   

    def __init__(self,name):

        self.name = name

    

    def say_hi(self):

        print("hello my name is %s" % self.name)

 

st = Student(“lila”)

st.say_hi()

 

4. The method of binding the class

         Class binding method used to decorate @classmethod

Special features: Regardless of class or object calls are automatically incoming class itself, as the first argument

 

** when binding to the object: When the function logic requires access to data objects in time

** When binding to the class: When the data logic functions to be accessed when the class

 

The non-binding method

Or static method is called, is that is does not need to access the data class, you do not need data access objects

Syntax: @staticmethod

      Def function name ():

                     'Function body

Guess you like

Origin www.cnblogs.com/oldboyliuhong/p/11240324.html