Basis for

Object-oriented programming ideas OOP

What is object-oriented

Object-oriented programming is a kind of thinking is summed up the experience of predecessors, how to guide programmers to write better programs,

The core is the object of the program is a collection of objects, program control eye responsible for scheduling these objects to interact with tasks:

Three advantages of object-oriented

1. Scalability

2. Flexibility

3. reusability

 

Disadvantages:

1. The complexity of the program improved

2. not accurately predict the results

 

scenes to be used

High scalability requirements of the procedure, typically directly to the user, for example: qq, micro-channel

 

Process-oriented programming ideas

The core concern is the process, that process is a step by step to step, both before doing the doing

Advantages: clear logic, simplifying complex issues, process

Disadvantages: poor scalability, maintainability

scenes to be used:

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

 

Remember: Not all program objects to be oriented, have specific needs analysis

 

Classes and Objects

This is the core concepts of OOP

class

Both types, categories, is an abstract concept

It is a collection with the same features and the same behavior of the object

 

Objects

Is a thing existed specific, have their own characteristics and behavior

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 there is a first, and then the object class

While in the program is the first class in order to have an object, we have to tell the computer what kind of object features what behavior

A summary conclusion: When using object-oriented programming, the first step is to think about what kind of object needs, what kind of an object with characteristics and behavior, which summed up the type of information required in accordance with

 

 

 

 

Creating classes and objects

The syntax for defining classes

class name of the class: the content class describes the attributes and skills to describe attributes with variables describing the behavior of a function written specification class name: The first is intended to see to know the name, 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. 
   
   
   



Create an object syntax: 

class the Person: 
    Pass 
# create the object 
p = Person ()

  

Properties wording:

Properties can be written in the class 
class attributes are common to all objects can also be written in an object object properties are unique (each object is not the same) and if the object exists in the same class property, to access the object if there is no access class








Exercise: describes a teacher needs to contain a common class and a property unique property 
class Teacher: 
    School = "Oldboy" 
T1 = Teacher () 
t1.name = "Jack" 
t1.age = 28
    

  

Deletions attribute change search

Increase property 
object variable name. Attribute name = attribute value to delete attribute del object variable name. Attribute name modify objects. Attribute = new value to view properties   visit is the object of all the properties print ( object. __Dict__) Access Objects class information print ( the object. the __class__)











init method

Initialization method is called, is essentially a function

Features 1: When the object is instantiated, the init method of automatically

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

Function: Users assign initial values ​​to the object

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

Dog class: 
    DEF the __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) 
Note: this function does not have any return value / .... only None

  

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

 

Bound method object

Class methods are bound method objects by default

It special is that,

When the object will automatically call the function passed in the object itself, as the first argument

When the class name to call him is a normal function, there are several parameters have to pass a few 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)

  

Class binding method

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 the functional logic required to access the data object, binding to the subject

When the data logic function needs to access class, to bind to the class

 

Non-binding approach

Or static method is called, is that is does not need data access class. Does not need to access the object's data

Syntax: @staticmethod

uncommonly used

 

Exercise: add a save method is a method to get students to class, save that the object is stored in a file, get the object is to obtain from a file

import os
import pickle
import time


class Student:
    def __init__(self,name):
        self.name = name

    def say_hi(self):
        print('name',self.name)

    def save(self):
        with open(self.name,'wb') as f:
            pickle.dump(self,f)

    @staticmethod
    def get(name):
        with open(name,'rb') as f:
            obj = pickle.load(f)
            return obj

print(Student.__name__)

# stu = Student('rose')    
# stu.save()
# 
# stu1 = Student('jack')
# stu1.save()
# 
# obj = Student.get('rose')
# print(obj.name)
# 
# obj = Student.get('jack')
# print(obj.name)
View Code

 

 

 

Guess you like

Origin www.cnblogs.com/pangqian/p/11240791.html