Python Object-oriented encapsulation

Source: Internet
Author: User

Classes Class
Inheritance (single inheritance, multiple inheritance)

class method @classmethod Decoration
Instance method def fn (self)
static method @staticmethod Decoration

Code area:

classpeople:#Defining basic PropertiesName="'; Age=0; #defines a private property that cannot be accessed outside the class    __weight=R; def __init__(self, n, A, W): Self.name=N; Self.age=A; Self.weight=W; defSpeak (self):Print('%s is%s years old.'%(Self.name, self.age));p= People ('Dog','8');p. Speak ();classpeople:#Common VariablesName='Jack'; Age=12;p=people ();#direct access to shared variablesPrint(p.name);Print(p.age);classPeople2:#Private Variables    __name='Jack'; __age=12;p 2=people2 ();#private variable is not accessible outsidePrint(P2.name);#Access will be error#accessing private properties through internal methodsclasspeople:__name='Jack'; __age=12; defGetName (self):returnSelf.__name; defgetage (self):returnSelf.__age;p=people ();Print(P.getname (), P.getage ());#instance properties may not be defined in classclassPeople:age=12;p=people ();p. Name='Jack';Print(P.name, p.age);Print(people.age);Print(People.name);#will error#built-in construction method definition propertiesclassPeople:name='Jack'; def __init__(self, Age): Self.age=age;p= People (12);Print(p.age);Print(People.age);#will errorclassPeople:country=' China';Print(People.country);# ChinaPeople.country ='Japan';Print(People.country);#JapanP=people ();Print(P.country);#JapandelP.country;#Delete Instance PropertiesPrint(P.country);#will errorclassPeople:country=' China'; #class method, decorated with Classmethod@classmethoddefGetcountry (CLS):returncls.country;p=people ();Print(P.getcountry ());# ChinaclassPeople:country=' China'; #instance Method    defgetcountry (self):returnSelf.country;#Instantiating Classesp =people ();Print(P.getcountry ());#Static MethodsclassPeople:country=' China'; #Static Methods    #static methods do not pass the self parameter@staticmethoddefgetcountry ():#calling a variable directly with the class name        returnPeople.country;#Direct printing of static methods without instantiationPrint(People.getcountry ());

Python Object-oriented encapsulation

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.