Object Oriented (i)

Source: Internet
Author: User

Catalogue1. Classes and Objects 2. Object-oriented 3.self what the hell? 4. Constructors 5. Encapsulation 6. Inheritance and multiple inheritance one, classes, and objects
#创建类, class named Mailclass mail:# "method"    def email (self,mail,neirong):        print ("To:", mail, "\ n content:", Neirong)        return True                              #如果不定义返回值, the default is none# "call" obj = Mail ()                                     #创建对象, object name Objobj.email (' Admin ', ' Hello, please find the attachment form. ')          #通过对象执行方法注: When you execute obj = Mail (), the class object pointer points obj to class mail.
Second, object-oriented

Concept: Object-oriented is a programmatic approach that requires classes and objects to be implemented.

A class is a template that contains several functions that implement some functions in a template.

An object is an instance created from a class that can execute functions in a class through an instance object.

Object-oriented is also the classification and encapsulation of functions or things.

Object-oriented three major features:

Encapsulation, inheritance, polymorphism

1. Functional programming and object-oriented programming comparison

Function-Type programming

def add (host,username,passwd):    print (HOST,USERNAME,PASSWD, "add") def remove (host,username,passwd):    print ( HOST,USERNAME,PASSWD, "remove") def modify (host,username,passwd):    print (HOST,USERNAME,PASSWD, "modify") def Select (HOST,USERNAME,PASSWD):    print (HOST,USERNAME,PASSWD, "select") #执行函数add (' host.com ', ' admin ', ' 123 ') remove (' host.com ', ' admin ', ' 123 ') modify (' host.com ', ' admin ', ' 123 ') Select (' host.com ', ' admin ', ' 123 ')

Object-Oriented Programming

Class SQLHelper:    def fetch (self, SQL):        print (obj1.hhost)        print (Obj1.uuserane)        print (OBJ1.PWD)    def create (self, SQL):        pass def-    remove (self, nid):        pass    def modify (self, name):        Passobj1 = SQLHelper () #以下定义的值被封装到对象obj1中, which can be called directly in a class. Obj1.hhost = "c1.salt.com" Obj1.uuserane = "Alex" obj1.pwd = "123" Obj1.fetch ("select * from A") 2. Under what circumstances is it appropriate to use object-oriented?  when some functions have the same parameters, object-oriented methods can be used to encapsulate the value of the parameter once to the object, and then to the object to take a value.
Third, what is self?
Self is a parameter that Python automatically assigns to a value. Which object executes the method, and who is self. Obj1.fetch (...)               Self = obj1


Object Oriented (i)

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.