Python object-oriented,

Source: Internet
Author: User

Python object-oriented,
1. Object 1.1 Polymorphism

This means that the same operation can be performed on objects of different classes.

1.2 Encapsulation

Working details of hidden objects in the external world

1.3 inheritance

Create specialized class objects based on common classes

 

2. Class and type 2.1 create class
# Create class mycalss: z = 10086 # There is a special difference between class methods and common functions, they must have an additional first parameter name self # self, such as this, but because it represents the object itself, it is often called self def add (self, x, y): print (x + y) def getting (self): print ("Hello") def info (self, name, age): self. name = name self. age = ageobj = mycalss () obj. getting () obj. add (2, 3) obj.info ("Bob", "18") print (obj. name, obj. age) Output: Hello5Bob 18
2.2 privatization
Class mycalss: name = "Bob" # privatization method def _ inaccessible (self): print ("_ inaccessible cannot be accessed from outside") obj = mycalss () # access nameprint (obj. name) # You can modify nameobj. name = "Jack" print (obj. name) obj. _ inaccessible () Output: BobJackTraceback (most recent call last): File "c: \ Users \ jm \ Desktop \ PythonSpace \ Untitled-6.py", line 17, in <module> obj. _ inaccessible () AttributeError: 'mycalls' object has no attribute' _ inaccessible'
Class mycalss: name = "Bob" # privatization method currently _ inaccessible def _ inaccessible (self): print ("_ inaccessible") cannot be accessed from outside ") def accessible (self): self. _ inaccessible () # The method can internally access obj = mycalss () obj. accessible () Output :__ inaccessible
2.3 class attributes

You can use vertex (.) to access object attributes.

You can also use the following functions to access attributes:

Getattr (obj, name [, default]): hasattr (obj, name) of the object to be accessed: Check whether there is an attribute setattr (obj, name, value): Set an attribute. If the property does not exist, a new property delattr (obj, name) will be created: Delete the property setattr (obj, name, value): Set an attribute. If the property does not exist, a new property is created.
2.4 inheritance
Class Filter: def init (self): self. blocked = [] def filter (self, sequence): return [x for x in sequence if x not in self. blocked] class SPAMFilter (Filter): # SPAMFilter is a subclass of Filter (single inheritance) def init (self): # override the init method self. blocked = ["SPAM"] f = Filter () f. init () print (f. filter ([1, 2, 3]) s = SPAMFilter () s. init () print (s. filter (["SPAM", "SPAM", "eggs", "bacon", "SPAM"]) # filter "SPAM" output: [1, 2, 3] ['eggs', 'bacon']
# Multi-inheritance class class1 (object): def getting (self): print ("class1") class class2 (object): def getting2 (self): print ("class2 ") class class3 (class1, class2): passcl = class3 () cl. getting () cl. getting2 () Output: class1class2
2.5 Constructor
Class student (object): def _ init _ (self, name, age): self. name = name self. age = ages = student ("Bob", "18") print (s. name, s. age) Output: Bob 18

 

 

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.