classPerson :"""Personnel Information""" #name (common attribute)Name ="' #Age (Common attribute)Age =0#Defining Constructors def __init__(Self, name="', age=0): Self.name=name Self.age= Age#Overloaded Proprietary method: __str__ def __str__(self):return "the __str__ proprietary method is overloaded here,"+ STR ({'name': Self.name,' Age': Self.age}) defSet_age (self, Age): Self.age= AgeclassAccount :"""Account Information""" #account balance (private attribute) __balance=0#total of all accounts __total_balance=0#Get account balance #Self must be the first parameter of a method defbalance (self):returnSelf.__balance " "There are three ways to define a class in the first way: without modification, the current instance is bound to increase the account balance" " defBalance_add (self, cost):#The self is accessing this instanceSelf.__balance+= Cost#self.__class__ can access the classSelf.__class__.__total_balance+= Cost" "the second type is modified with @classmethod, which binds to the class class method (identified by @classmethod with the first parameter CLS)" "@classmethoddeftotal_balance (CLS):returnCls.__total_balance " "Second: @staticmethod-Modified, non-binding static methods (with @staticmethod identification, no class parameters or instance parameters)" "@staticmethoddefExchange (A, b):returnB, aclassTeacher (person, account):"""Teachers""" #class name, protected properties_class_name ="' def __init__(self, name):#first Overloaded parent class __init__ () construction method #Super (subclass, Self). __init__ (parameter 1, parameter 2, ...)Super (Teacher, self).__init__(name) @classmethoddefSet_class_name (cls,class_name): Cls._class_name=class_namedefGet_info (self):#return personal information in the form of a dictionary return { 'name': Self.name,#The property value of the parent class person is accessed here ' Age': Self.age,'class_name': Self._class_name,'Balance': Self.balance (),#called here is a subclass overloaded method } #Method Overloading defbalance (self):#Account.__balance is a private property, the subclass cannot be accessed, so the parent class provides methods to access returnAccount.balance (self) * 1.1classStudent (person, account):"""Student"""_teacher_name="' def __init__(Self, name, age=18): #second Overloaded parent class __init__ () construction method #parent class name. __init__ (self, parameter 1, parameter 2, ...)Person.__init__(self, name, age) @classmethoddefSet_teacher_name (cls,teacher_name): Cls._teacher_name=Teacher_namedefGet_info (self):#return personal information in the form of a dictionary return { 'name': Self.name,#The property value of the parent class person is accessed here ' Age': Self.age,'Teacher_name': Self._teacher_name,'Balance': Self.balance (),}#Teacher JohnJohn = Teacher ('John') John.set_class_name ('CS2018') John.balance_add (20) John.set_age (66L#instances of subclasses can call methods of the parent class directlyPrint("John ' s info:", John.get_info ())#Student MaryMary = Student ('Mary', 18) Mary.balance_add (18) Mary.set_teacher_name ('John')Print("Mary ' s info:", Mary.get_info ())#Student Fakefake = Student ('Fake') Fake.balance_add (30)Print("Fake ' s info", Fake.get_info ())#three different ways to call a static methodPrint("john.exchange (' A ', ' B '):", John.exchange ('a','b'))Print('Teacher.exchange (1, 2)', Teacher.exchange (1, 2))Print('Account.exchange (Ten):', Account.exchange (10, 20))#class methods, class propertiesPrint('account.total_balance ():', Account.total_balance ())Print('teacher.total_balance ():', Teacher.total_balance ())Print('student.total_balance ():', Student.total_balance ())#Overloaded Proprietary methodsPrint(John)Print(Mary)Print(fake)
Output:
John's info: {' name ': ' John ', ' age ': $, ' balance ': 22.0, ' class_name ': ' CS2018 '}
Mary's info: {' teacher_name ': ' John ', ' name ': ' Mary ', ' age ': $, ' balance ': 18}
Fake's Info {' teacher_name ': ' John ', ' name ': ' Fake ', ' age ':, ' balance ': 30}
John.exchange (' A ', ' B '): (' B ', ' a ')
Teacher.exchange (1, 2) (2, 1)
Account.exchange (10, 20): (20, 10)
Account.total_balance (): 0
Teacher.total_balance (): 20
Student.total_balance (): 48
This overloads the __str__ proprietary method, {' name ': ' John ', ' Age ': 36}
This overloads the __str__ proprietary method, {' name ': ' Mary ', ' Age ': 18}
This overloads the __str__ proprietary method, {' name ': ' Fake ', ' Age ': 18}
Python3 for I-Object programming examples