Interface Test Learning-python Seventh lesson (Object oriented programming)

Source: Internet
Author: User

used to be process-oriented programming ., do one thing in order, must go through a lot of operation. Now it's object-oriented programming . in class, the teacher gave a very appropriate example, such as buying a car, the process is to learn the car first, then go to the 4s shop to buy a car, then go to the certificate, then insurance, then a temporary license, then a formal license. The object-oriented approach is to integrate the scenarios that all of these steps will experience, directly open a vehicle 
to purchase the certificate center, and then all of the fines are dealt with from the school to the license and even later.
in my understanding, object-oriented is to create a model for something that has something in common, a model that has the same properties as these things, and a common way of dealing with these things. And then, once you need to operate on something separately, just call
this model, and the thing can be built quickly based on the properties and methods that are already well established.
Object-oriented programming has several basic points, namely, classes, constructors, properties, methods, instantiation. There are also class variables, instance variables, class methods, instance methods, destructors, privatization, inheritance, and so on.
1, class

1 class num1 ():   2     Pass 3 4 class num2 (object): 5     Pass

2. Properties

A property is actually a variable, including a class variable (a variable defined directly in the class) and an instance variable (a variable defined under "Def __init__ (self):").

For example, a class has a lot of students, all living in Chengdu, the students of the grade, class and age assumptions are the same, then these are the common attributes of students, you can use variables to define first.

 1  class   Students ():  2  county =  "   China   " #   class variables, public variables, each instance can be used to save memory  3  def  Span style= "COLOR: #800080" >__init__   (self):  4  self. class  =  "   High class 32   " #   Instance variable  5  self.age = 

3. Methods

If we are going to print the name, class and age of the class student. Then you have to create a method, also a function. For example, the following Print_info function allows you to print out the name, class, and age of each object.

1  classStudents ():2County =' China'  #class variables, public variables, each instance can be used, can save memory3      def __init__(self, name):4Self.class='High 32 Classes'  #instance Variable5Self.age = 186Self.name =name7  8      defPrint_info (self):9          Print('student's name is%s'%self.name)Ten          Print('student's class is%s'% self.class) One          Print('The student's age is%s'% self. Age)

4. Instantiation

Each class must be instantiated in order to be used, not the car model to drive on the road, or to have a real car. Instantiation is equivalent to comparing your favorite model to get a real object, this item is purchased according to the model comparison, so it has the same color as the model, shape and so on. So the instantiated object is the property and method of owning the class.

1  classStudents ():2County =' China'  #class variables, public variables, each instance can be used, can save memory3      def __init__(self, name):4Self.class='High 32 Classes'  #instance Variable5Self.age = 186Self.name =name7  8      defPrint_info (self):9          Print('student's name is%s'%self.name)Ten          Print('student's class is%s'% self.class) One          Print('The student's age is%s'%self.age) A  -ZXM = Student (name ='Zhang Xiaoming')#instantiation of - Print(ZXM.class) # Output results are  theZxm.print_info ()# will print ZXM name, class, age of this object

5. Constructors and destructors

A constructor is a function that runs when a class is instantiated, and a destructor is a function that destroys an instantiated object. The database needs to be shut down after the database is connected, where the code that shuts down the database is placed in the destructor, which automatically destroys the instance and automatically closes the link when it finishes executing.

1 classMyDb (object):2     # Destructors3     def __del__(self):4 self.cur.close ()5 self.coon.close ()6         Print('Over ....')7     #constructor Function8     def __init__(Self,9 Host,user,passwd,db,Tenport=3306,charset='UTF8'): One         Try: ASelf.coon =Pymysql.connect ( -Host=host,user=user,passwd=passwd, -port=port,db=db,charset=CharSet, theAutocommit=true#Auto Commit, no more commits are required in non-SELECT statements -             ) -         exceptException as E: -             Print('Database link failed! %s'%e) +         Else: -Self.cur = Self.coon.cursor (cursor=pymysql.cursors.dictcursor)

6. Private

Variables are private and public variables, as the name implies, private variables are non-modifiable variables that can only be called within a class, and public variables are variables that can be called modified outside of the class. A private variable is defined by adding two underscores before the variable name.

1 ImportRedis2 classMy (object):3     def __init__(self):4Self.__host='***.*.**.**'  #Private Variables5Self.__port= 63796Self.__password='*******'7 8SELF.R = Redis. Redis (host=self.host,port=self.port,password=Self.password)9 Ten     defGet (self, k): Oneres =Self.r.get (k) A         ifRes: -             returnRes.decode () -         returnNone
1 my = my ()  #实例化 2 my.port = 9999  #  Port is a private variable cannot be modified 3  Print(My.  __host)  #  host is a private variable and cannot be transferred out of class with 4 My.  __close()  #  Close is a private method and cannot be used out of class

7. Inheritance

When defining a new class, "Object" is usually written in parentheses, which is actually the class that the definition inherits from. So the class written in parentheses is the class to which the defined class inherits. The inherited class can be either a parent or a base class, and the new class becomes a subclass. Subclasses can inherit all the properties and methods of the parent class, and can define their own properties and methods without affecting the parent class.

For example, the following defines a base class for connecting to a database that has properties such as host, port, password, and so on. These properties are used regardless of whether the MySQL database is connected or the Redis database. You can then directly inherit the base class when you define MySQL classes and Redis classes so that the properties of host, port, and password can be used directly.

1 classBase (object):2     def __init__(self, host, port, password):3Self.host =Host4Self.port =Port5Self.password =Password6 7 8 classMysql (Base):9     PassTen  One  A classRedis (Base): -     Pass



Interface Test Learning-python Seventh lesson (Object oriented programming)

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.