Python (xi) object-oriented programming

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/lexi3555/article/details/80701383

First, the concept

1, oriented Object Programming: Data operation has become packaged with objects.

2, categories: classification of objects of the same type, abstract, have common features, write common feature of these together to form a class.

Members of the class data members and member comprises methods, data members represented by a variable form, expressed in a functional form member method.

3, variables outside the class method called attributes, variables within the class method called field.

4, the data members of the class data members and member data is divided into objects, the data members of the class is the class of all shared objects simultaneously and data members of the object.

Outside the main program or class data members of the object belongs to the object, the object can only be accessed by name

Data belong to the class members of the class can be accessed by class name or object name.

Second, the definition and use of the class

Private members: a double underscore (__) at the beginning of a private member, did not provide strict access protection to private members in python.

Access mechanism: Reference http://www.cnblogs.com/wanpython/archive/2013/05/23/3095534.html


Third, the method (described object has behavior)

Public methods and private methods: refers to an instance method belongs to the object, that is, each object has its own public methods (not begin with an underscore) and a private method (start with double underscore __). Public method called directly by object name, private methods can only be invoked by the self instance method, or by calling a special way on the outside.

Static methods and class methods: by class name and the object name to call, but can not access the members belonging to the object directly, you can only access the class members.


Fourth, property

1, set the read-only @property


2, read and modify the set, can not be deleted


3, setting read, modify, delete


Fifth, inheritance

1, the concept of

(1) in the hierarchy, the existing, good design class is called the parent class or base class, newly designed class is called the subclass or derived class.

(2) a derived class can inherit the parent class's public member, but can not inherit the private members.

(3) If you need to call a method in the base class of the derived class can be used built-in function super () or implemented through the "base class name. Method name ()" method.

2, the Person class design, and according Person Teacher derived class, respectively, to create objects of class Person class and Teacher


Note: Object-oriented knowledge is very large, do it first here Beginners



Guess you like

Origin blog.csdn.net/lexi3555/article/details/80701383
Recommended