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