-
#inherit #The derived class (subclass) inherits the methods and data members of the parent class #grammar: # class subclass: parent class # {......} #Subclass inherits the parent class, and the parent class derives the subclass. The parent class is also called the base class, and the child class is called the derived class class Vehicle: def __init__(self,name): self.name =''; print('The vehicle is initialized and created'); def run(self): print('The vehicle is moving...'); def show(self): print('The name of the car is %s' % self.name); class Truck(Vehicle): #Subclass (parent class); inheritance syntax def __init__(self,name,weith):#The constructor of the subclass should contain the common attributes of the parent class and the subclass #Vehicle.name = name; # 1. Call the parent class attribute and use the parent class. attribute in the __init__() method self.name = name; # 2. Call the parent class attribute, use self. attribute or parent class in the __init__() method #Vehicle.__init__(self.name);#1, call the parent class method __init__(self, parameter) # super(Vehicle, self).__init__(name);#2, 1, call the superclass method super(superclass, self).__init__(parameter) self.work = work; print('The truck is initialized and created'); # method override def run(self): Vehicle.show(self); print('%s truck load %d tons while driving...'%(self.name, self.weith)); class Trian(Vehicle): def __init__(self,name,nums): self.nums=nums; def run(self): print('The train is running on the track...'); t=Truck('Dongfeng',15); t.run();
-
class cixunfu: def run(self): print('Driving on the maglev track..') class student: def goHome(self,vehicle): vehicle.run() print('Students are on their way home...') class tui: def run(self): print('2 legs are driving...') # c=Trian('Beijing-Shanghai high-speed rail',30); # c.run(); # ok=Trian('Malaysia Airlines 730',30); daizhong=tui(); xinpeng=student(); xinpeng.goHome(daizhong);
pet doctor
-
#pets class pet: def kanbing(self): print('Treat the pet...'); class Car(pet): def kanbing(self): print('Take medicine to the cat, eat small yellow croaker..'); class Dog(pet): def kanbing(self): print('Give the dog a doctor infusion, eat bones..'); class bird(pet): def kanbing(self): print('Take a cup of medicine to the bird, eat insects..'); class pig(pet): def kanbing(self): print('Treat the little pig with acupuncture, eat pig food..'); class ShouVet: def JiuZhen(self,pet): print('Veterinary visit.. to see a pet..'); pet.kanbing(); dingdang=Car(); kala=Dog(); huangli=bird(); peiqi=pig(); syi = ShouVet (); syi.JiuZhen(dingdang); syi.JiuZhen(kala); syi.JiuZhen(huangli); syi.JiuZhen(peiqi);
-
# isinstance() function: determine whether it is an inheritance relationship print('kala is Dog:',isinstance(kala,Dog));
calculator
-
#calculator class Operation: def __init__(self,numA,numB): self.numA=numA; self.numB=numB; def GetResult(self): print('Calculate the operation result of 2 operands'); class OpeartionAdd(Operation): def __init__(self,numA,numB): super(OpeartionAdd, self).__init__(numA,numB); def GetResult(self): return self.numA + self.numB; class OpeartionSub(Operation): def __init__(self,numA,numB): # super(OpeartionSub, self).__init__(numA,numB); super (OpeartionSub, self) .__ init __ (munA, numB); def GetResult(self): return self.numA-self.numB; class OpeartionMul(Operation): def __init__(self,numA,numB): super(OpeartionMul, self).__init__(numA,numB); def GetResult(self): return self.numA * self.numB; class OpeartionDiv(Operation): def __init__(self,numA,numB): super(OpeartionDiv, self).__init__(numA,numB); def GetResult(self): return self.numA // self.numB; if __name__=='__main__': numA=int(input('Please enter the first operand: ')); numB=int(input('Please enter the second operand: ')); fu=input('Please enter the operator:'); q=None;#The unassigned variable represents a calculator if fu=='+': q=OpeartionAdd(numA,numB); elif fu=='-': q = OpeartionSub(numA, numB); elif fu == '*': q = OpeartionMul(numA, numB); elif fu == '/': q = OpeartionDiv(numA, numB); else: print('input error'); print('Calculation result:', q.GetResult())
- calculator factory
-
#Calculator factory class class Operation: def __new__(cls): pass def GetResult(self,numA, numB): print('Calculate the operation result of 2 operands') class OpeartionAdd(Operation): def __new__(cls): return object.__new__(cls); #return Operation.__new__(cls) def GetResult(self,numA,numB): return numA+numB class OperationSub(Operation): def GetResult(self,numA,numB): return numA-numB class OperationMul(Operation): def GetResult(self,numA,numB): return numA*numB class OperationDiv(Operation): def GetResult(self,numA,numB): return numA//numB class OperationFactory(object): # Functions that create different objects with different parameters def create_class(self,fu): if fu == '+': q = OpeartionAdd(); elif fu == '-': q = OperationSub(); elif fu == '*': q = OperationMul(); elif fu == '/': q = OperationDiv(); return q; if __name__== '__main__': #Factory object factory=OperationFactory(); numA=int(input('Please enter the first operand')) numB=int(input('Please enter the second operand')) fu = input('Please enter the operator:') # q represents a calculator created by the factory q=factory.create_class(fu) print('Calculation result:', q.GetResult(numA, numB))
Singleton pattern: The main purpose of this pattern is to ensure that only one instance of a class exists
-
class singleton: #With the help of third-party variables __instance=None; def __new__(cls, *args, **kwargs): if cls.__instance==None: cls.__instance=object.__new__(cls); return cls.__instance; else: return cls.__instance; s=singleton(); print(id(s)); s1=singleton(); print(id(s1));
- Change the position of two values with the help of a third-party variable
-
a,b=10,5; c=a;#Change the position of two values with the help of third-party variables a=b; b=c; print(a,b);