Python ()-Object-oriented

Source: Internet
Author: User

1. Wedge 2. Process vs Object oriented

Process-oriented: process (pipelined thinking)
Advantage: Greatly reduce the complexity of the writing program, just follow the steps to be executed, stacking the code can be.
Cons: The process is used to solve a problem, code reaching.

Application scenario: Once you have completed a very rarely changed scenario, the notable examples are Linux kernel, git, and Apache HTTP server.
--------------
Function: The function code is encapsulated into the function, no need to repeat the code, the call is convenient
--------------

Object oriented: Object (God-Mind)

The advantage is that it solves the extensibility of the program.
A single modification of an object is immediately reflected in the entire system, such as the character of a character parameter in the game and the ability to modify it easily.

Cons: Poor controllability, inability to process and result as accurately as possible to predict problems,
Once the object-oriented program is started, the interaction between the objects solves the problem and the final result cannot be predicted.

Application scenario: frequently changing requirements of software, general requirements of the changes are concentrated in the user layer, Internet applications, enterprise internal software, games, etc.
Object-oriented programming is not all. For a software quality, object-oriented programming is just to solve extensibility.
---------------------------

Class: A kind of thing (man, dog, Tiger) having the same characteristics (human being a class,)
Object/instance: A specific thing (next door Jen, Downstairs Wong Choy) (human, specific to a person, this specific person is a concrete example, is also an object)
Instantiation: Class-to-object procedure
===================

3. Primary knowledge classes and objects

Dict #字典类型 is a big class.
{' Age ': #具体到某一个字典 is an object of the dictionary
An object is a specific individual that has a real existence.

In Python, a variable represents a feature , a function represents a skill , and thus a class of things with the same characteristics and skills is a ' class ', and the object is the specific one in this category.
==========

Class

Declaration of the class:

#类名大写variable = value # static property (Data property) def __init__ (self): #初始化方法 (Add the necessary properties to the object) (the object itself) passdef func: #  Dynamic Properties (self must Plus) (function properties, methods) (functions inside the class are not called functions, called methods)

  


============

Two kinds of functions of the class:

1. Attribute references
2. Instantiation

Property Reference:

#定义一个人类role = ' person ' # character attributes are human #人都可以走路, that is, there is a way to walk #查看人的role属性(Property Acquisition: Class. Property Instance. Properties) print (Person.walk) #引用人的走路方法, Note that this is not a call to Person.country = ' China ' # Add Property #删除属性 #修改属性 ( Role is already there, re-assigned value)

  

================

Instantiation:
The class name + () is instantiated and automatically triggers the __INIT__ function, which you can use to customize your own features for each instance.

Instantiation process:
Construction Method: Object creation
Initialize: Add some attributes to the new object

Grammar:

Object name = Class Name (parameter)

p = person (' Zhou ') #类名 () is equal to the execution of person.__init__ ()
#执行完__init__ () returns an object. This object is similar to a dictionary and has some properties and methods that belong to the person itself.
#你可以偷偷的理解: p = {' name ': ' Zhou ', ' Walk ': Walk}


View Properties & Call Methods:

    • The name of the object. Property name of (object)
    • The name of the object. (Class) method name ()

About self:

Self: Automatically passes the object/instance itself to the first parameter of __init__ when instantiated, you can also give him an individual name, but it is not recommended, (the code is not understood by others)


The addition of class attributes:

A: Where do the properties of the class we define are stored? There are two ways of viewing

    1. dir (class name): A list of names is isolated
    2. The class name. __dict__: A dictionary is found, key is the property name (function method name), value is the property value (function method memory address) (__dict__ is a dictionary form, but cannot be modified, can only be viewed)

Second: Special class properties

The class name. __name__ # the name of the class (string) class name. __doc__ # the document string for the class #功能说明字符串class name. __base__ # the class name of the first parent class (speaking of inheritance) . __bases__ # A tuple of all the parent classes of a class (speaking of inheritance) class name. __dict__ # the Dictionary attribute class name of the class . __module__ # the class name of the module where the class definition resides . __class__ # class for instance (in modern class only)


============================

Knowledge about the object:

classPerson:#Define a humanRole =' Person'         #People's role attributes are peopledef __init__(self, name, aggressivity, life_value): Self.name= Name#each character has its own nickname;Self.aggressivity = aggressivity#each character has its own attack power;Self.life_value = Life_value#each character has its own health value;defAttack (Self,dog):#A man can attack a dog, and the dog here is also an object.                     #people attack the dog, then the dog's health will fall according to the attack .Dog.life_value-= Self.aggressivit


Object is an example of the actual existence of a class, that is, an instance

Object/instance has only one function: Property reference

p = person ('Zhou'# value passed, default __init__ function (default call __init__)print  (p.name) Print (p.aggressivity) Print (P.life_value) # Zhou # Ten #  +

You can also refer to a method, because the method is also a property, but a function-like property, we also call it dynamic properties.
Referencing a dynamic property is not the way to do this, but to call the method and call the function is the same, you need to add parentheses after the


Fixed format: (Definition & Call)

Defined:

class class Name: def __init__  = args1         #Attribute (properties of the object)Self.attribute2 = args2def func1 (self): Pass         # methods (method name) def Func2 (self):pass

Call:
Object name = Class name (kitty,18) #对象就是实例, which represents a specific thing obj.class_name (argument 1, argument 2 ...)
            #类名 (): Class name + parenthesis is the instantiation of a class, equivalent to calling the __init__ method
#括号里传参数, the parameter does not need to pass self, and the other corresponds to the formal parameter one by one in Init
            #结果返回一个对象 (The object's properties are inside the __init__ function)
Object name. Object's Property 1 #查看对象的属性, directly with the object name. Property name Obj.[attribute1]
The name of the object. Method Name () #调用类中的方法, directly with the object name. Method Name () to obj.method1. ()

Python ()-Object-oriented

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.