python--Object-Oriented summary

Source: Internet
Author: User

The summary focuses on the advantages of object-oriented, high scalability

The sample code is as follows:

1 classChinese:2     def __init__(self,name,age,sex):3Self.name=name4Self.age= Age5self.sex=Sex6 7 8P1 = Chinese ('Egon', 18,'male')9P2 = Chinese ('Alex', 38,'female')TenP3 = Chinese ('WPQ', 48,'female')

We insert a data property into the class, just add it to the class, and the object can be accessed without any changes, and the code is validated as follows:

1 classChinese:2Country =' China'3     def __init__(self,name,age,sex):4Self.name=name5Self.age= Age6self.sex=Sex7 8 9P1 = Chinese ('Egon', 18,'male')TenP2 = Chinese ('Alex', 38,'female') OneP3 = Chinese ('WPQ', 48,'female') A  - Print(p1.country) - Print(p2.country) the Print(P3.country)

The results are as follows:

China
China
China certificate can be accessed

Insert a Function property, just add it in the class, the object can be accessed without any changes, and the code is validated as follows:

1 classChinese:2Country =' China'3     def __init__(self,name,age,sex):4Self.name=name5Self.age= Age6self.sex=Sex7 8     defEat (self):9         Print('%s is eating'%self.name)Ten  One  AP1 = Chinese ('Egon', 18,'male') -P2 = Chinese ('Alex', 38,'female') -P3 = Chinese ('WPQ', 48,'female') the  - p1.eat () - p2.eat () -P3.eat ()

The results are as follows:

Egon is eating
Alex is eating
WPQ is eating

python--Object-Oriented summary

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.