Python Note four: Object-oriented

Source: Internet
Author: User

1. Class

class Student (object):     def __init__ (self, Name, score):         = name        = Score

1) __init__ constructor method, __init__ the first parameter of the method is always self , representing the created instance itself, when called, without passing the parameter.

2) __del__ destructor, use when releasing object

3) __call__ function call

2. A simple example:

classStudet (object):def __init__(Self,name,score): Self.name=name Self.score=scoredefPrint_score (STD):Print '%s:%s'%(Std.name, Std.score)defGet_grade (self):ifSelf.score >= 90:            return 'A'        elifSelf.score >= 80:            return 'B'        elifSelf.score >=70:            return 'C'        Else :            return 'D'Ares=studet ('Ares', 90) Ares.print_score ()Print 'The score grade is:', Ares.get_grade ()

3. Variables:

1) __ares indicates that Ares is a private variable (private) and is accessible only internally and cannot be accessed externally

2) starting with a double underscore, and ending with a double underscore, is a special variable that can be accessed directly by a special variable.

3) An instance variable name that starts with an underscore, for example _name , an instance variable outside of which is accessible, but, as you see in the conventional rules, when you look at such a variable, it means, "although I can be accessed, but please treat me as a private variable, do not feel free to access".

4. Processing function:

1) type() function: Determine object type

2) isinstance() function: Determine the type of class

3) dir() function: Get all the properties and methods of an object

5. Inheritance and polymorphism:

1) Inherit template

  class Animal(object):

def fun ():

Pass

  class Dog(Animal):

    pass

  class Cat(Animal):

    pass

Animal is the parent class for dog and Cat.

2) Advantages:

A. Inheritance can take all the functions of the parent class directly, so that you do not have to re-zero, subclasses only need to add their own unique methods, but also the parent class does not fit the method overrides overrides;

B. polymorphic

6. Multiple Inheritance:

Class in the inheritance relationship, usually, the main line is a single inheritance, if you need to "mix" additional functionality, through multiple inheritance can be achieved.

  class Animal(object):

def fun ():

Pass

  class Runnable(object):

    def run(self):

      print(‘Running...‘)

Multiple inheritance:

  class Dog(Animal, Runnable):

    pass

Python Note four: 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.