Object-oriented python learning [entry], python object-oriented

Source: Internet
Author: User

Object-oriented python learning [entry], python object-oriented

Preface

Recently, I have been studying Python object-oriented programming. I have never been familiar with other object-oriented programming languages before. Therefore, learning this part is quite exciting. I would like to summarize it here.

Overview

  • Python supports multiple programming paradigms: process-oriented, object-oriented, and face-oriented (decorative part.
  • Process-oriented: Write base code from top to bottom based on business logic
  • Function: encapsulate a function code into a function. You do not need to write it again later. You only need to call the function.
  • Object-oriented: classifies and encapsulates functions to make development faster, better, and stronger ..."

OOP ideas

  • The basic philosophy of object-oriented: The world is composed of objects with their own motion rules and internal states. The interaction and communication between objects constitute the world.
  • Uniqueness: there are no two identical leaves in the world, and there are no identical objects.
  • Classification is an abstraction of the real world.
  • Three features: encapsulation, inheritance, and Polymorphism

Three main features of object-oriented architecture:

I. Encapsulation

Encapsulation is an abstraction of a specific object. Some parts are hidden and invisible outside the program, meaning they cannot be called.

PRIVATE: Some attributes in a class or function are restricted to a certain region and cannot be called externally.

The method of privatization in Python is also relatively simple, that is, the name of the property (including the method and data) to be privatized is prefixed with double underscores.

For example:

Class ProtectMe (object): def _ init _ (self): self. me = "qiwsir" self. _ name = "kivi" def _ python (self): print ("I love Python. ") def code (self): print9" Which language do you like? ") Self. _ python () if _ name _ = "_ main _": p = ProtectMe () print (p. me) print (p. _ name) # running result qiwsirTraceback (most recent call last): File "21102.py", line 21, in <module> print p. _ nameAttributeError: 'protectme 'object has no attribute' _ name'

Note: The name attribute is hidden and cannot be called.

Call private properties. You can use the property function.

Class ProtectMe (object): def _ init _ (self): self. me = "qiwsir" self. _ name = "kivi" @ property def name (self): return self. _ name if _ name _ = "_ main _": p = ProtectMe () print (p. name) # running result kivi

Therefore, when using the object-oriented encapsulation feature, you need:

  • Encapsulate content somewhere
  • Call encapsulated content from somewhere

Step 1: encapsulate content somewhere

Self is a form parameter.obj1 = Foo('wupeiqi', 18 )Self is equal to obj1.

When executedobj2 = Foo('alex', 78 ) Self is equal to obj2.

Therefore, the content is encapsulated into the objects obj1 and obj2. Each object has the name and age attributes, which are saved in memory.

Step 2: Call encapsulated content from somewhere

When calling encapsulated content, there are two situations:

  • Direct call through objects
  • Indirect call through self
Class Role (object): ac = None # class variable def _ init _ (self, name, role, weapon, life_value): # initialization method self. name = name # instance variable (member variable) self. role = role self. weapon = weapon self. life_val = life_value def buy_weapon (self, weapon): # definition method # self: indicates the instance itself self. weapon = weapon # print ("% s is buying [% s]" % (self. name, weapon) # The process of converting an abstract class into a specific object, that is, instantiating p1 = Role ("sanjiang", 'police ', "B10", 90) # instance t1 = Role ("Chunyun", 'terrorist', "B11", 100)

Ii. Inheritance

Inheritance: the inheritance in object orientation is the same as that in real life. That is, the child can inherit the parent content.

Class SchoolMember (object): # member_nums = 0 def _ init _ (self, name, age, sex): self. name = name self. age = age self. sex = sex # self. enroll () def enroll (self): SchoolMember. member_nums + = 1 print ("SchoolMember [% s] is enrolled! "% Self. name) def tell (self): print ("Hello my name is [% s]" % self. name) class Teacher (SchoolMember): def _ init _ (self, name, age, sex, course, salary ): # override the _ init _ method super (Teacher, self) of the parent class ). _ init _ (name, age, sex) # inherit (New Class) # SchoolMember. _ init _ (self, name, age, sex) # inherit (legacy class) self. course = course self. salary = salary def teaching (self): print ("Teacher [% s] is teaching [% s]" % (self. name, self. course) class Student (SchoolMember): def _ init _ (self, name, age, sex, course, tuition): super (Student, self ). _ init _ (name, age, sex) self. course = course self. tuition = tuition def pay_tuition (self): print ("ca, student [% s] paying tuition [% s] again" % (self. name, self. tuition ))

Summary

The above is all about this article. I hope this article will help you in your study or work. If you have any questions, please leave a message.

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.