Python object-oriented programming learning

Source: Internet
Author: User

Python Object-oriented programming
    • Basic Concept Understanding
    1. Object-oriented programming--object oriented programming, short for OOP, is a programming idea. OOP takes objects as the basic unit of a program, and an object contains functions for data and manipulating data.
    2. Process-oriented programming treats a computer program as a set of commands, which is the sequential execution of a set of functions. In order to simplify the program design, the process will continue to cut the function into sub-function, that is, the large function by cutting into small block function to reduce the complexity of the system.
    3. Object-oriented programming treats a computer program as a set of objects, and each object can receive messages from other objects and process them, and the execution of a computer program is a series of messages passing between objects.
    4. The object-oriented design idea is to abstract class, create instance according to class
  • Classes and instances

    #对象class Student(object):    def __init__(self, name, score):        self.name = name;        self.score = score;    def print_score(self):        print(‘%s: %s‘ % (self.name, self.score));    def get_grade(self):        if self.score >= 90:            return ‘A‘;        elif self.score >= 60:            return ‘B‘;        else:            return ‘C‘;bart = Student("yehui", 99);lisa = Student("yehui01", 55);bart.print_score();lisa.print_score();print(lisa.get_grade());# 注意到__init__方法的第一个参数永远是self,# 表示创建的实例本身,因此,在__init__方法内部,# 就可以把各种属性绑定到self,因为self就指向创建的实例本身。
  • Access restrictions
    If we do not want the property value of an instance of an object to be modified by an external code, we can precede the name of the property with two * * Underline __, the variable name of the instance if the underscore begins, then he is a private variable * *.

  • Inheritance and polymorphism
    In the language of object-oriented programming, inheritance is one of its main characteristics (encapsulation, polymorphism, inheritance), in Python, his object-oriented programming inheritance and C + + and other object-oriented languages, such as the subclass can be the properties and methods of the stepfather class, through inheritance, subclasses get all the functions of the parent class . I will not summarize, the following is his way of succession and format:

    #继承class Animal(object):    def run(self):        print("Animal is running...");class Dog(Animal):    def run(self):        print("Dog is running...");class Cat(Animal):    def eat(self):        print("Cat is meating...");class Pig(Animal):    pass;dog = Dog();dog.run();cat = Cat();cat.run();cat.eat();pig = Pig();pig.run();# Dog is running...# Animal is running...# Cat is meating...# Animal is running...

    You can add methods and properties to the subclass of the inherited parent class yourself, or you may not add them. You can also override the method of the parent class, and the method of the class will overwrite the parent class's method. This is one of the benefits of inheritance: polymorphism .

    #多态def run_twice(animal):    animal.run();    animal.run();run_twice(Animal());run_twice(Dog());# Animal is running...# Animal is running...# Dog is running...# Dog is running...

    Python polymorphism allows us to add subclasses of the parent class. Closed for modification: You do not need to modify functions such as run_twice () that depend on the animal type.

    Python is a dynamic language and does not necessarily require an incoming animal type. We just need to make sure that the incoming object has a run () method.

  • Get object Information
    With a series of functions built into Python, we can dissect any object
    1. Use the type () function to determine the type of the object;
    2. Use Isinstance () to determine the type of inheritance for class classes;
    3. Use the Dir () function to get the properties and methods of an object.
Summarize

The idea of object-oriented programming in Python is much the same as that of other object-oriented programs, except that there is a difference in writing and usage, because I have learned C + + so much in this part of the study.

Python object-oriented programming learning

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.