Python Object-oriented programming

Source: Internet
Author: User

In the previous chapter, reading self-study python-socket programming inside found a lot of problems, a focus is not know what Python class is, always in Baidu every code is what meaning, found as if the problem is very big, in case there is also a class how to do, is every word Baidu??? , resolute Baidu "Python object-oriented programming", the following is the learning of Python object-oriented programming experience, the original source from Wu Jianzi , many places to change their own

Outline

1. Creating Classes and objects

2, object-oriented three major features

Creating Classes and objects

Object-oriented programming is a way of programming, which requires "classes" and "objects" to be implemented, so object-oriented programming is actually the use of "classes" and "objects".

A class is a template that can contain multiple functions and functions in a template.

Objects are instances created from templates that can execute functions in a class through an instance object

From the above to see this sentence, do not know is right or wrong, anyway, I can not understand, feel too advanced

" An instance is an object that opens up space in memory.
The girlfriend (object) in the mouth of his parents, her arms around her (example). "

Create a class:

# Create Class Foo:
  #类中定义的函数叫方法 def Bar (self): #类中的函数第一个参数必须是self print ("Bar") def Hello (self, name): print ("I am%s"% name) # Create an object based on class Foo objobj = foo () # is equivalent to getting all the methods (functions) inside the class, of course, this is my own understanding # Foo (). Bar () obj. Bar () # Execute the Bar Method Obj.hello ("Smelond") # Execute the Hello method

# Object-oriented: "Create Object" "Execute method through Object"
# function Programming: "Execute function"

Object-oriented three major features

First, the package

Encapsulation, as the name implies, encapsulates the content somewhere and then calls the content that is encapsulated somewhere.

Therefore, when using object-oriented encapsulation features, you need:

    • Encapsulate content somewhere
    • To invoke the encapsulated content from somewhere

First step: encapsulate the content somewhere

# class Foo:    def __init__ (self, Name, age):  # called constructor method, automatically executes when object is created according to class        self.name = name        Self.age = age# based on class Foo Create object #  automatically executes the __init__ method of the Foo class obj1 = foo ("Wupeiqi")  # Wupeiqi and 16 are encapsulated in the name and age property of (Obj1, self) # Create an object from class Foo #  automatically executes the __init__ method of the Foo class obj2 = foo ("Smelond",  +) # encapsulates Smelond and 16 respectively into the name and age properties of (obj2, self)

Self is a formal parameter, when executing obj1 = Foo (' Wupeiqi ', 18), self equals obj1

When executing obj2 = Foo (' Alex ', 78), self equals obj2

So, the content is actually encapsulated in objects Obj1 and OBJ2, each object has a name and an age attribute, which is similar to saving in memory.

Step Two: Call the encapsulated content from somewhere

When the encapsulated content is called, there are two scenarios:

    • Called directly through an object
    • Indirectly called through self

1. Direct invocation of encapsulated content by object

Invocation method: Object. Property name

Call the encapsulated content directly from the above code print (obj1.name) print (obj1.age) print (obj2.name) print (obj2.age) output wupeiqi18smelond16

2. Indirectly invoking the encapsulated content via self

# Indirect call to Class Foo via self:    def __init__ (self, Name, age):        self.name = name        Self.age = Age    def detail (self): C7/>print (self.name)        print (self.age) obj1 = Foo (' Wupeiqi ', +) Obj1.detail ()  # Python will pass obj1 to the self parameter by default, namely: Obj1.detail (OBJ1), so, at this time self = obj1 inside the method, that is: Self.name is Wupeiqi; Self.age is 18obj2 = Foo (' smelond ', +) Obj2.detail () 
   # Python will pass the obj2 to the self parameter by default, namely: Obj2.detail (OBJ2), so the self = Obj2 inside the method, that is, Self.name is Smelond; Self.age is 17 Output: Wupeiqi18smelond16

In Summary, for object-oriented encapsulation, it is actually using the construction method to encapsulate the content into the object, and then indirectly through the object directly or self to obtain the encapsulated content.

Python Object-oriented programming

Related Article

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.