为什么80%的码农都做不了架构师?>>>
-
#继承 #衍生的类(子类)继承父类的方法和数据成员 #语法: # class 子类:父类 # {......} #子类继承父类,父类派生子类。父类又叫基类,子类又叫派生类 class Vehicle:def __init__(self,name):self.name ='';print('交通工具被初始化 创建出来');def run(self):print('交通工具在行驶...');def show(self):print('车的名字是%s' % self.name); class Truck(Vehicle):#子类(父类);继承语法def __init__(self,name,weith):#子类的构造方法应该包含父类和子类的共同属性#Vehicle.name = name; # 1、调用父类属性,在__init__()方法中使用父类.属性self.name = name; # 2、调用父类属性,在__init__()方法中使用self.属性或父类#Vehicle.__init__(self.name);#1、调用父类方法 __init__(self,参数)# super(Vehicle, self).__init__(name);#2、1、调用父类方法 super(父类,self).__init__(参数)self.weith=weith;print('卡车被初始化 创建出来'); #方法重写def run(self):Vehicle.show(self);print('%s卡车载重%d吨 行驶中...'%(self.name,self.weith)); class Trian(Vehicle):def __init__(self,name,nums):self.nums=nums;def run(self):print('火车在轨道上行驶...');t=Truck('东风',15); t.run();
-
class cixunfu:def run(self):print('在磁悬浮轨道上行驶..')class student:def goHome(self,vehicle):vehicle.run()print('学生在回家的途中...')class tui:def run(self):print('2条腿儿在行驶...') # c=Trian('京沪高铁',30); # c.run(); # ok=Trian('马航730',30); daizhong=tui(); xinpeng=student(); xinpeng.goHome(daizhong);
宠物看病
-
#宠物看病 class pet:def kanbing(self):print('给宠物看病...'); class Car(pet):def kanbing(self):print('给猫咪看病 吃药,吃小黄鱼..'); class Dog(pet):def kanbing(self):print('给狗狗看病 输液,吃骨头..'); class bird(pet):def kanbing(self):print('给小鸟看病 拔罐,吃虫子..'); class pig(pet):def kanbing(self):print('给小猪看病 针灸,吃猪粮..'); class ShouVet:def JiuZhen(self,pet):print('兽医出诊..给宠物看病..');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()函数:判断是否是继承关系 print('kala is Dog:',isinstance(kala,Dog));
计算器
-
#计算器 class Operation:def __init__(self,numA,numB):self.numA=numA;self.numB=numB;def GetResult(self):print('计算2操作数的运算结果');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('请输入第一个操作数:'));numB=int(input('请输入第二个操作数:'));fu=input('请输入运算符:');q=None;#未赋值的变量 代表一个计算器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('输入错误');print('计算结果:',q.GetResult())
- 计算器 工厂类
-
#计算器 工厂类 class Operation:def __new__(cls):passdef GetResult(self,numA, numB):print('计算2操作数的运算结果')class OpeartionAdd(Operation):def __new__(cls):return object.__new__(cls);#return Operation.__new__(cls)def GetResult(self,numA,numB):return numA+numBclass OperationSub(Operation):def GetResult(self,numA,numB):return numA-numBclass OperationMul(Operation):def GetResult(self,numA,numB):return numA*numB class OperationDiv(Operation):def GetResult(self,numA,numB):return numA//numBclass OperationFactory(object):#不同的参数创建不同的对象的 函数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=OperationFactory();numA=int(input('请输入第一个操作数'))numB=int(input('请输入第二个操作数'))fu = input('请输入运算符:')# q代表 由 工厂创建出来的一个计算器q=factory.create_class(fu)print('计算结果:',q.GetResult(numA, numB))
单例模式:该模式主要目的是确保某一个类只有一个实例存在
-
class singleton:#借助第三方变量__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));
- 借助第三方变量更改两个值的位置
-
a,b=10,5; c=a;#借助第三方变量更改两个值的位置 a=b; b=c; print(a,b);