Object-Oriented programming knowledge point

Source: Internet
Author: User

Features
Class
Object

Encapsulation
Inheritance
Polymorphism

Syntax

Call a function -- execute -- return results

R1 = role. _ init _ () return x342423

R1 = role (R1, "A", "police", "15000 ")
R1.name = ""
R1.role = "poice"
R1.monetary = 15000
R1.buy _ gun () role. buy_gun (R1)



Attribute
Method
What is the usage of class variables? Attribute shared by everyone to save costs
Class person:
CN = "China"
Def _ init _ (self, name, age, ADDR, Cn = "China ")
Self. Name = Name
Self.cn = Cn
P1 = person (name, age, ADDR)
 
Constructor

Destructor: it is automatically executed when an instance is released or destroyed. It is usually used for some final work, such as closing some database connections and closing open temporary files.

Private method, private property

Class variable
Instance variables


Encapsulation

Inheritance
Py2 classic classes are inherited by depth first, while new classes are inherited by breadth first.
Py3 classic classes and new classes are inherited by breadth first.

Polymorphism
One interface and multiple implementations

Class
Attribute
Instance variables
Class variable
Private Attribute _ VaR
Method
Constructor,
Destructor
Private Method

Object: the object obtained after instantiating a class


Encapsulation
Expose the implementation details of some functions

Inheritance
Inheritance, combination

Code reuse
Single inheritance
Multi-inheritance,
2.7 classic category, depth first, new category, breadth first
3. X is breadth-first
Class Foo (object ):
Def _ init _ (self, name, age, sex, salary, course ):

Self. Salary = salary
Self. Course = Course



Polymorphism
Interface reuse, one interface, multiple implementations

Static Method
Only nominal Classification Management. In fact, the static method cannot access any attributes of the class or instance.
Class Method
Only class variables are allowed. instance variables cannot be accessed.

Attribute Method
Convert a method into a static property


Reflection
Hasattr (OBJ, name_str) is used to determine whether the corresponding name_str string exists in the object obj.
Getattr (OBJ, name_str) obtains the memory address of the corresponding method in the OBJ object based on the string.
Setattr (OBJ, 'y', Z), is equivalent to ''x. Y = v''
Delattr

Exception
Try:
Code
Except T (error1, erro2) as E:
Print E

Failed t exception: Catch all errors, not recommended

Object-Oriented programming knowledge point

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.