Python Object-oriented classes

Source: Internet
Author: User

Object oriented into introduction

Process-oriented programming

The core is the process (pipeline thinking), the process is the steps to solve the problem, the process-oriented design is like a well-designed pipeline, consider when to deal with what things. The main applications are in places where little modification is done, such as the Linux kernel, git, Apache server, etc.

Advantages: Greatly reduce the design complexity of the program

Cons: Poor scalability, change a place is likely to change multiple places, reaching

Object-Oriented programming: not all programming, just to solve the software extensibility

The core is the object (God-Mind), the object as the basic unit of the program, an object containing the data and the function of manipulating the data. Object-oriented is the computer program as a collection of objects, every object can receive messages from other objects, the execution of a computer program is a series of messages between the various objects passed. The main application in the requirements of the frequent changes in software, general requirements of the changes are concentrated in the user layer, Internet applications, enterprise internal software, games, etc.

Advantages: Solve the problem of poor scalability of the program, the modification of an object will be immediately reflected in the entire program system

Cons: Poor controllability, inability to predict program execution results

Classes and objects

The concept of objects

Object is also called instance, is generated by the class, the object is a combination of data properties and method attributes, such as the hero in Lol, a hero is an object, with health value, blue bar attack and other data attributes, but also has qwer four skills belong to the method attribute.

The concept of a class

The class contains the same property parts of a set of objects, such as the game characters, all characters have names, tags (tank warrior mage), health value, blue bar attack, etc., although the specific values are different, but can be included in the class to generate.

In Python, a variable is represented as a data property, and a function is used to represent a method property.

In real life, there are objects after the first class, such as the first people, after the concept of human, and in programming, is the first class, after the object, class generation object.

declare a class: similar to the definition of a function

Defining functions: Using the DEF keyword

Defining classes: Using the class keyword

" " Class Name:    Data Attribute    method Property ''class  Style:    Pass
classChinese:country=' China' #Data Properties    def __init__(self,name,color,age):#the INIT function must not have a return value, only initialize        if  notisinstance (NAME,STR):RaiseTypeError Self.name=name#p1.name= ' Xiaobai 'Self.color=color#p1.color= ' Yellow 'Self.age=age#p1.age=22    defTalk (self):#Function Properties        Print('%s is talking'%self.name)defEat (self):Print('is eating')    defLaugh (self):Print('is laughing')#instantiation: __init__ (p1,name,color,age)P1=chinese ('Xiaobai','Yellow', 22) P2=chinese ('Xiaohei','Yellow', 25)Print(Chinese.__dict__)#use __dict__ to view properties under the Chinese classPrint(P1.__dict__)#use __dict__ to view properties under Chinese object instantiation P1P1.talk ()
Print (Chinese.country,id (chinese.country))
Print (P1.country,id (p1.country))

Execution results

{' __module__ ': ' __main__ ', ' country ': ' China ', ' __init__ ': <function chinese.__init__ at 0x0000024c5d9cb9d8> Talk ': <function chinese.talk at 0x0000024c5d9cba60>, ' eat ': <function chinese.eat at 0x0000024c5d9cbae8> Laugh ': <function chinese.laugh at 0x0000024c5d9cbb70>, ' __dict__ ': <attribute ' __dict__ ' of ' Chinese ' objects& gt;, ' __weakref__ ': <attribute ' __weakref__ ' of ' Chinese ' objects>, ' __doc__ ': None} #Chinese. __dict__


{' name ': ' Xiaobai ', ' Color ': ' Yellow ', ' age ': #p1. __dict__


Xiaobai is talking #p1. talk ()

China 2906932484504 #print (Chinese.country,id (chinese.country))


China 2906932484504 #print (P1.country,id (p1.country))

From the execution results you can see that the class has data properties and function properties, and the object has only data properties. Objects have data properties that the class does not have, and objects have common data properties and function properties in the class.

Python Object-oriented classes

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.