Object-oriented definitions, and basic grammar

First, what is an object-oriented

Object-oriented programming ideas OOP

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:

Case: an 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

In the object-oriented programmer's angle is changed from the operator into a particular conductor;

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

Case:

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 control object can be responsible for scheduling,

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

 

Case:

Cao Cao poetry

Meat and drink, life Zhenshuang

Meat and drink, life geometry

Wine and song, life geometry

### object-oriented three advantages

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

 

Relationship ### 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

 

# Create classes and objects

The syntax for defining classes

`` `python
name of the class class:
content # class describes the attributes and skills
# attribute with variable description
# description of the behavior of a function

# First class name written specification is intended to see to know the name of the name of a 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:

```python
class Person:
pass

# Create the object
the p-the Person = ()
`` `

Properties wording:

`` `python
property can be written in the class
class attributes are common to all objects

Can also be written in an object
object properties are unique (not the same) for each object


If present in the same class attribute to access the object if there is no access to the object class and

 

class the Person:   # define a class 
    Country = " China "   # define a class variable 

    DEF  the __init__ (Self, * args):   # initialization method, an object is to Self, a parameter must pass 
        the self.name = args [0]   # public variable 
        self.hp = args [. 1 ] 
        self.age = args [2 ] 
        self.sex = args [. 1 ] 

    DEF Walk (Self, n-):   # member methods, generally must pass parameters and must be written in the first bit 
        Print ( " % S walk walk, to go step% d " % (self.name, the n-))   # self.name call member variables 


#Create an object, or a Person is instantiated ALAX 
alex = Person ( " naughty " , " 100 " , " 18 " , " woman " ) 
alex.walk ( 100)   # calls member methods, and traditional values 
Print (Person.country )   # call class variable 
Person.country = " Taiwan "   # modify variable values based 
Print (Person.country) 

# demand calculated perimeter and area of a circle 
PI = 3.14 class circular:   # define a class circle DEF the __init__ (Self, R & lt) : 
        self.r =



      r

    # 面积
    def area(self):
        areas = pi * self.r ** 2
        return areas

    # 周长
    def girth(self):
        girths = self.r * pi * 2
        return girths


a = Circular(10)
res1 = a.area()
res2 = a.girth()
print(res1, res2)

 

```

Deletions attribute change search

`` `python
increase attribute
object variable name. attribute name = attribute value

Delete attribute
del object variable name attribute name

Modify
Object. = New attribute value

View property access all properties of the object
print (Object .__ dict__)

Access to the object class information
print (Object 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

```python
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)


Note: This function can not have any return value / .... None can only be required to do so ..
`` `

 

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

```python
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 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

 

# 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

 

class OldBoyStudent: 
    School = " Oldboy " 

    DEF  __init__ (Self, name, the HP): 
        self.name = name 
        self.hp = the HP 

    DEF the Read (Self):   # object binding method 
        Print ( " % S students in school! " % self.name) 

    @classmethod   # class binding method, also known as static methods, need to access the class attribute data, can not access the object properties 
    DEF show_school (CLS):
         # Print (self.school) 
        Print (cls.school) 

    @staticmethod   #Non-binding approach, do not access an object attribute data, and do not access the class attribute data, is a normal function, in the above plus @staticmethod 
    DEF print_hello ():
         Print ( " the Hello world " ) 


STU = OldBoyStudent ( " chicken brother " , 100 )
 Print (OldBoyStudent.school)   # value oldboy access class attributes, static attributes, or 
Print (STU. the __dict__ )   # { 'name': 'chicken brother', 'hp': 100} all the attributes of the object access 
Print ( stu. the __class__ )   # <class 'in __main __. OldBoyStudent'> access information object class 
stu.read () 
stu.show_school ()   # type of access 
stu.  print_hello()  # 普通函数

# OldBoyStudent.show_school(stu)
# OldBoyStudent.show_school()
# print(OldBoyStudent)

# stu = OldBoyStudent("jack")
# stu.show_school()
#
# OldBoyStudent.print_hello()
# stu.print_hello()

 

Guess you like

Origin www.cnblogs.com/wukai66/p/11240065.html