Object-oriented programming, an object class, and

Object-oriented programming, an object class, and

First, object-oriented

1. 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, the programmer is responsible for scheduling these objects to interact with the task.

2, several cases

Case 1: The elephant put into the refrigerator?

Process-oriented:

1. Open the refrigerator

2. Load elephant

3. Close the refrigerator

Object-oriented:

Find the object loaded with elephants skills

Summary: changes in the object-oriented programmer's perspective, from the conductor into a specific operator

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

Case 2 :: Buddhist sutras

Tathagata there is a pile of broken books to spread out, he did it yourself, but to find five objects to help him up

Tathagata as long as the object can be responsible for controlling scheduling

If an object is changed, it will not affect other objects, scalability

Case 3 :: Cao Cao poetry, poetry ordered his men will be made of engraved on wood

First day: meat and drink, life Zhenshuang

The next day: meat and drink, life geometry

On the third day: wine and song, life geometry

3, the three major advantages of object-oriented

1. Scalability

2. Flexibility

3. reusability

Disadvantages:

1. The complexity of the program improved

2. not accurately predict the results

4, usage scenarios

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

Second, the process-oriented programming ideas

1, the core concern is the process

The process is step by step to step, both before doing the doing

2, advantages and disadvantages

Advantages: clear logic, simplifying complex issues, process

Disadvantages: poor scalability, maintainability

3, usage scenarios

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

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

Third, the classes and objects (which is the core of OOP concepts)

1, class

Both types, categories, is an abstract concept

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

2, Object

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

The object is a combination of characteristics and skills

3, 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

4, create classes and objects

The syntax for defining classes

class 类的名称:
    # 类中的内容 描述属性和技能 
    #描述属性用变量
    #描述行为用函数 

#类名称 书写规范  首先是见名知意 名称是大驼峰命名法  
#驼峰就是单词首字母大写 , 大驼峰是第一个字母大写,小驼峰是第一个字母小写

Create an object syntax:

class Person:
    pass

#创建对象 
p = Person()

Properties wording:

# 属性可以写在类中 
# 类中的属性,是所有对象公共的 

# 也可以写在对象中 
# 对象中的属性,是每个对象独特的(不一样的)


# 如果类中和对象中存在同样的属性,先访问对象 如果没有在访问类 


# 练习: 描述一个老师类 需要包含 一个公共属性和 一个独特的属性 

class Teacher:
    school = "oldboy"
    
t1 = Teacher()
t1.name = "jack"
t1.age = 28

Deletions attribute change search

# 增加属性 
# 对象变量名称.属性名称 = 属性值

# 删除属性
del 对象的变量名称.属性名称 

# 修改 
对象.属性 = 新的值 

# 查看属性  访问的是对象的所有属性 
print(对象.__dict__)

# 访问对象的类信息
print(对象.__class__)

5, 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

class Dog:
    def __init__(self,kind,color,age):
        self.kind = kind
        self.color = color
        self.age = age

d1 = Dog("二哈","黑白",1)
d1 = Dog("泰迪","棕色",2)


注意:该函数不能有任何返回值/.... 只能是None  规定如此..

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

6, 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)

7, 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 binding to the object: When the function logic requires access to data objects in time

When binding to the class: When the data logic function needs to access the class when

8, 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

Guess you like

Origin www.cnblogs.com/DcentMan/p/11247336.html