python画喜⽺⽺代码_PYTHON养成⽇记DAY-7
知识点 :super()获得⽗类的定义,多态,对象的浅拷贝和深拷贝,类的组合关系,设计模式:⼯⼚模式、单例模式,多重继承,⽅法重写等等...
代码 :# class Person:
# def __init__(self,name,age):
吉普指南者
# self.name = name
# self.age = age
# def say_name(self):
# print('我的名字是:{0}'.format(self.name))
# def say_age(self):燃料电池原理
# print('我的年龄是:{0}'.format(self.age))
# class Student(Person):
# def __init__(self,name,age,score):
# self.score = score
# Person.__init__(self,name,age)
华阳微投# def say_age(self):
# print('报告,我的年龄是:{0}'.format(self.age))
# def say_score(self):
# print('我的分数是:{0}'.format(self.score))
# s = Student('AA',18,100)
# s.say_age()
# s.say_score()
# class Person:
# def __init__(self,name):
# self.name = name
# def __str__(self):
# return '我是:{0}'.format(self.name)
# def sayname(self):
# print('名字是:{0}'.format(self.name))
# s = Person('郑⼜中')
# print(s)
# class A:
# def aa(self):
# print('aa')
# class B(A):
# def bb(self):
# print('bb')
# class C(B):
# def cc(self):
# print('cc')
# c = C()
# c.aa()
# c.bb()
# class A:
# def say(self):
# print('A:',self)
# class B(A):
# def say(self):
# super().say()
# print('B:',self)
# B().say()
# class Man:
# def eat(self):
特斯拉谈上海工厂将大幅减产
# print('吃饭')
# class Chinese(Man): # def eat(self):
# print('中国⼈吃饭')
# class English(Man): # def eat(self):
# print('英国⼈吃饭')
# class Korean(Man): # def eat(self):
# print('韩国⼈吃饭')
# def man_eat(m):
# if isinstance(m,Man): # m.eat()
# else:
# print('不能吃饭')
# man_eat(Chinese())
# man_eat(1)
# man_eat(English())
# man_eat(Korean())
# class Person:
# def __init__(self,name,age):
# self.name = name
# self.age = age
# def __add__(self, other):
# if isinstance(other,Person):
# print('{0},{1}'.format(self.name,other.name)) # print('{0}--{1}'.format(self.age,other.age))
# else:
# print('不能相加')
# def __mul__(self, other):
# if isinstance(other,int):
# print(self.name * other)
# else:
# print('不是同类,⽆法相乘')
# s1 = Person('AA',18)
# s2 = Person('BB',20)
# s = s1 + s2
# s1 * 2
# class Phone:
# def __init__(self,cpu,screen):
# self.cpu =cpu
# self.screen = screen
# class CPU:
# def showcpu(self):
# print('显⽰CPU')
# class Screen:
# def showscreen(self):
# print('显⽰SCREEN')
#
# c = Phone(CPU(),Screen())
# c.cpu.showcpu()
# c.screen.showscreen()
# class CarFactory:
# def CreateCar(self,brand):
# if brand == '奔驰':
# return Benz()
# elif brand == '宝马':
# return BMW()
# elif brand == '奥迪':
# return Audi()
# else:
# print('未知品牌,⽆法创建')
# class Benz:
# pass
# class BMW:
# pass
# class Audi:
# pass
# factory = CarFactory()
# c1 = factory.CreateCar('法拉利') # c2 = factory.CreateCar('奥迪')
# print(c1)
# print(c2)
# class CarFactory:
# __obj = None
# __init_flag = True
# def createcar(self,brand):
狮跑和智跑# if brand == '奥迪':
# return AUDI()
# elif brand == '宝马':
# return BMW()
# def __new__(cls, *args, **kwargs): # if cls.__obj == None:
# cls.__obj = object.__new__(cls)
# return cls.__obj
# def __init__(self):
# if CarFactory.__init_flag:
# print('')
# CarFactory.__init_flag = False
# class BMW:
# pass
# class AUDI:
# pass
# factory = CarFactory()
# c1 = atecar('奥迪')
# c2 = atecar('宝马')
# print(c1)
# print(c2)
d1
# factory2 = factory
# c3 = atecar('宝马')
# c4 = atecar('奥迪')
# print(c3)
# print(c4)
# class Rectangle:
# name = '长⽅形'
# def __init__(self,long,width):
# self.long = long
# self.width = width
# def perimeter(self):
# print('{0}的周长为:{1}'.format(self.__class__.name,self.long * 2 + self.width * 2)) # def area(self):
# print('{0}的⾯积为:{1}'.format(self.__class__.name,self.long * self.width))
# class Square:
# name = '正⽅形'
# def __init__(self,long):
# self.long =long
# def perimeter(self):