Python object-oriented-basic

Source: Internet
Author: User

Basic concepts

Object-oriented Universal Object Oriented Programming abbreviation OOP is a kind of programming idea. is to use the object as a basic unit of a program, to encapsulate the data and functions inside, to achieve good reusability, flexibility and extensibility.

Basic concepts:

Object-oriented is an abstraction with two basic concepts: classes and objects.

  • A class is an abstract feature that defines a thing,
  • The object is an instance of the class.

Example: A programmer is a class, and a specific person, such as me, is an object.
Basic elements:
Properties and methods. Take the programmer as an example. The following properties and features are specific to the programmer:

  • Properties: Age, gender and height.
  • Function (method): Write code, repair computer.

Inherited:
One of the main benefits of object-oriented programming is the reuse of code, and one way to implement this reuse is through inheritance mechanisms. Inheritance can be fully understood as a type and subtype relationship between classes.

Polymorphic:
A class inherits several different classes, and when the same method is called, it reacts differently.

Defining classes and creating instances

In Python , the class is class defined by the keyword. PersonFor example, define a person class as follows: According to Python the programming habit, the class name begins with an uppercase letter, followed by ( object ), indicating which class the class inherits from. With the Person definition of a class, you can create specific xiaoming , and xiaohong so on instances. Create an instance using the 类名+() form of a function call like:

Example
in [1 ]:  person (object ): ...: pass  ...: in [2< /span>]: Student =  person () in [3 ]: Teacher =  Person () in [4 ]: print  (student) <  __main __. Person object  at 0x00000271be5ea0f0  >  in [5 ]: print  (teacher) <  __main__. Person object  at 0X00000271BE5EABE0  >  in [6 ]: print  (Student ==  Teacher) false  In [7 ]:  

Python object-oriented-basic

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.