Python Learning (11) Python class

Source: Internet
Author: User

Python class

Object-oriented programming is one of the effective methods of software writing.

How to write a python program

    • 1. Function programming, using function mode
    • 2. Object-oriented programming, using class mode
Create Class
创建方法    构造方法,__init__(self,arg)        obj = 类(‘a1‘)    普通方法        obj = 类(‘xxx’)        obj.普通方法名()

The format is as follows:

ClassDatabasehelper:Def__init__(Self, IP, port, username, pwd):Self.ip = IPSelf.port = PortSelf.username = UsernameSelf.pwd = pwdDefAdd(Self,content):# Use the user name, password, and other linked data in the self to print (' Content ')# Close Data LinkDefDelete(Self,content):# Use the user name, password, and other linked data in the self to print (' Content ')# close data link def update (self,content): # uses linked data such as user name, password, etc. in self to print (# Close Data link def get  (self,content): # take advantage of the user name encapsulated in self, Link data such as password print ( ' content ') # close data link  #封装数据库登陆IP, port, username, password S1 = databasehelper (3306,  ' Jorbabe ',  ' 6666 ')       
Object-oriented three characteristics of object-oriented three major features: encapsulation

Use the construction method to encapsulate the same properties:

def __init__(self, n,a): self.name = n self.age = a self.xue = ‘o‘ b1 = Bar(‘jorbabe‘, 18)b2 = Bar(‘yunlei‘, 19)
Two main characteristics of object-oriented: inheritance

1. Inheritance

class 父类:    pass    class 子类(父类):    pass

2. Overriding methods to prevent the execution of methods in the parent class

3. Self is always the caller of the method that executes the change

4.

Super (subclass, Self). Methods in the parent class (...)

Parent class name. Method in parent class (self,...)

5. Support multiple inheritance in Python

    • A. Left priority
    • B. A road goes to the black
    • C. At the same root, the root
Three main characteristics of object-oriented: polymorphism
python====> 原生多态,忽略多态,python传参不用指定参数的类型。
‘jorbabe‘def func(arg):    print(arg)        func(1) func(‘jorbabe‘)
Class Member
class Foo:  #静态字段,属于类  country = ‘中国‘ def __init__(self, name,): # 普通字段 self.name = name # 普通方法 def show(self): print(self.name)obj = Foo(‘jorbabe‘)obj.nameobj.show()

Class Member: Field

    • Normal field, saved in object, execute only through object access
    • Static fields, which are saved in the class, can be accessed through the object or accessed through the class

Method

    • Common method, saved in a class, called by an object, self= "Object
普通方法    class Foo:        def bar(self): print (‘bar‘) #推荐 obj = Foo() obj.bar() Foo.bar(obj)
    • Static methods, which are saved in the class, are called directly by the class
#类方法ClassFoo:def bar print ( ' bar ')  #静态方法  @staticmethod  def sta (): print ( ' STA ')  #静态方法 + parameter  @staticmethod def  Stac (A1,A2): print (A1,A2) Foo.sta () Foo.stac (1,2" STA 1 2   
    • Class method, saved in the class, called directly by the class, cls= "current class
#类方法ClassFoo:DefBar(Self): print ( ' bar ')  #静态方法 @staticmethod def sta (): print ( ' STA ')  #静态方法 + reference @staticmethod def Span class= "Hljs-title" >stac (A1,A2): Print (A1,A2)  #类方法 @ Classmethod def classmd #cls: class name print (CLS) print ( "Classmd ') foo.classmd () <class ' __main__ . foo ' > Classmd            

Application Scenarios:

    • If you need to save some values in an object and perform a function, you need to use the values in the object-> the normal method
    • No value in any object is required, static method

Property

class Foo: def __init__  (self): self.name =  ' a ' def bar (self): Print ( ' bar ')  #属性或特性 @property def per (self): Print ( ' per ') obj = Foo () # obj.per () obj.per       
    • Call mode one by one corresponds
ClassFoo:Def__init__(Self):Self.name =ADefBar(Self): print (' Bar ')#属性或特性. Call Mode: Obj.per @propertyDefPer(self): # print (' per ') return Span class= "Hljs-number" >1  #属性传参. Call mode: Obj.per = 123 @per. Setter def  Per (self,val): Print (val) # The property parameter is deleted. Call mode: Del obj.per @per. deleter def  Per (self): Print ( ' 666666 ') obj = Foo () # obj.per () obj.per r = obj.per print (r) obj.per = 123 del obj . per 1 123 666666     
中国的所有省份,用面向对象知识表示?class Province:    # 静态字段,属于类    country =  ‘中国‘ def __init__(self, name): # 普通字段,属于对象 self.name = name henan = Province(‘河南‘)henan.namehenan.name = "河南南"#hebei = Province(‘河北‘)# Province.countryprint (Province.country)print (henan.name)中国河南南
Example: Using the attribute function to realize paging
ClassPargination:#定义页码Def__init__(Self,current_page):#判断是否是整数try:p = Int (current_page) except Exception asE:p =1#页码赋值Self.page = P#计算开始 @propertyDefStart(Self): val = (Self.page-1) *10Return Val#计算结尾 @propertydef end(self): val = self.page *  return val#定义元组列表li = [] #定义元组列表赋值 for i in Range (+): Li.append (i)while true:p = input (' Please enter page number to view: ') #给类的方法传值 obj = pargination (p) 
                                                
                                                  #类方法未添加属性前的调用方式
                                                 # print (Li[obj.start (): Obj.end ()]) #方法添加属性后的调用方式, go to bracket print (li[obj.start:obj.  end])    
                                                              
Please enter the page number you want to view:5[40,41,42,43,44,45, 46, 47, 48, 49] Please enter the page number to view: 6[50, 51, 52, 53, 54, 55, 56, 57, 58, 59] Please enter the page number to view: Aa[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Please enter the page number you want to view:            

Python Learning (11) Python class

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.