Python------Object-oriented programming

Source: Internet
Author: User

1. What is an object?

The source of the object is to simulate the real world and encapsulate the data and code together. People, cars, houses, are the real world.

Object = Feature (attribute) + method (behavior)

property is actually a variable in the code.

Methods are functions that perform certain tasks by invoking these functions.

Instantiated object: TT (variable) = class name () assignment operation is not required, but if the created object is not assigned to a variable, then the object cannot be used because no reference pointer points to the instance and will eventually be automatically reclaimed by the Python garbage collection mechanism.

How to call the object's method, via TT (variable). method

The Magic method of a class: Constructs a method, as long as an object is instantiated, and this method is called automatically when the object is instantiated. Arguments are passed in when instantiated, and these parameters are automatically passed into the __init__ method. By overriding this method, the process of customizing the initialization of the object

Call each other:

2. What is a class? What is the class for?

Class Description: A class is a kind of thing, a model, which contains objects (methods and properties), by using a class to create a real object, this object is called an instance of this class, also known as an instance object.

Types usually start with uppercase letters.

When the class is instantiated, it should not pass arguments, depending on the constructor.

A better understanding of the class, give two examples

EG1: Factory to produce a batch of toys, is not to create models of toys, according to the model for mass production, is the real toy. Toy model is the class, the real toy is the object of the class instance.

EG2: Building before building, must need the building structure of drawings (including the properties of the building: several floors, how much money, etc., the method of building: Sales, buy people, etc.)

Definition class: Class name:

Object-Oriented Programming

Object-oriented inside is a process-oriented

What is self? Self is the object of this class. Equivalent to a unique designation. How a class is likened to an instance of a drawing class The object is a real house, according to a drawing can be instantiated, thousands of houses, they look similar, but they have their own home. Self is equivalent to the house number. With self, you can easily find your own house.

Python------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.