Python Basic Learning 1-Object oriented

Source: Internet
Author: User

#!/usr/bin/env python#-*-coding:utf-8-*-class Foo: #定义类 def Mail (self,email,message): #定义类的方法 print (' Send mail to%s! letter Interest:%s '% (email,message)) return trueobj = Foo (), #创建对象obj. Mail (' [email protected] ', ' Test message Information 123 ') #对象调用方法 the self parameter is not assigned Value, the system automatically assigns a # class object to the wrapper and call class Sqlhelper:def fetch (SELF,SQL): #这里的self refers to an object that is instantiated later, obj, all of which can be assigned the value of the Obj object Obj.hhost,obj.uuserane , obj.pwd print (self.hhost) print (self.uuserane) print (self.pwd) print (sql) obj = SQLHelper () obj . hhost= "c1.salt.com" obj.uuserane= "Alex" obj.pwd= "123" Obj.fetch ("SELECT * from user") #========> when to use object-oriented? When some functions in a function have the same parameters, you can use an object-oriented approach to encapsulate the value of the parameter once to the object, and then # go to the object to value the public private =====>class of the #=====> class person:name= "small Crucian carp" #公有变量 __ Home= "CQ" #两个下划线 represents the private variable of the class Def take (self): print (Self.message) P=person () print (p.name) #公有变量访问p. message= "I am the Person class" p . Take () #====> class inheritance ====>class man: #从Person类继承 Passm=man () m.message= "I Am Man" m.take () #从父类继承了take方法 #=== The combination of = = Class ====> to combine several classes that cannot be inherited into a class # The following example combines turtles and fishes into a pond class ClasS turtle:def __init__ (self,x): Self.num=xclass fish:def __init__ (self,y): Self.num=yclass pool:d EF __init__ (self,x,y): self.x = Turtle (x) self.y = Fish (y) def print_num (self): Print ("The pool has%d fish and%d Turtle "% (self.x.num,self.y.num)) pool = Pool (1,10) pool. Print_num ()

Python Basic Learning 1-Object oriented

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.