知识点 :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('init.....')

# CarFactory.__init_flag = False

# class BMW:

# pass

# class AUDI:

# pass

# factory = CarFactory()

# c1 = factory.createcar('奥迪')

# c2 = factory.createcar('宝马')

# print(c1)

# print(c2)

# factory2 = factory

# c3 = factory2.createcar('宝马')

# c4 = factory2.createcar('奥迪')

# 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):

# print('{0}的周长为:{1}'.format(self.__class__.name,self.long * 4))

# def area(self):

# print('{0}的面积为:{1}'.format(self.__class__.name,self.long ** 2))

# a = Square(10)

# a.perimeter()

# a.area()

# b = Rectangle(10,20)

# b.perimeter()

# b.area()

# 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(Rectangle):

# def perimeter(self):

# if self.long ==self.width:

# self.__class__.name = '正方形'

# super().perimeter()

# def area(self):

# if self.long == self.width:

# self.__class__.name = '正方形'

# super().area()

# class Page:

# def __init__(self,total):

# self.total = total #总记录数

# self.__pagesize = None #每页记录数

# self.__currentpage = None #当前页码

# self.maxpage = None

# @property

# def pagesize(self):

# return self.__pagesize

# @pagesize.setter

# def pagesize(self,pagesize):

# if pagesize > 0:

# self.__pagesize = pagesize

# else:

# print('输入有误!')

# @property

# def currentpage(self):

# return self.__currentpage

# @currentpage.setter

# def currentpage(self,currentpage):

# self.__currentpage = currentpage

# maxpage = self.total % self.__pagesize

# if maxpage == 0:

# maxpage = self.total // self.__pagesize

# else:

# maxpage = self.total % self.__pagesize + 1

# self.maxpage = maxpage

# if currentpage < 1 or currentpage > maxpage:

# print('输入有误!')

# def show(self):

# if self.__currentpage:

# print('当前页数为:第{}页'.format(self.__currentpage))

# start = (self.__currentpage - 1) * self.__pagesize + 1

# end = self.__currentpage * self.__pagesize

# if self.__currentpage == self.maxpage:

# end = self.total

# for i in range(start,end + 1):

# print('当前信息为:第{}条记录'.format(i))

# print('一共是{}页'.format(self.maxpage))

#

# p = Page(20)

# p.pagesize = 5

# p.currentpage = 3

# p.show()

# class MoveStore:

# def __init__(self):

# self.name = None

# def read(self):

# print('{}正在读取...'.format(self.name))

# def write(self):

# print('{}正在写入...'.format(self.name))

# class USB(MoveStore):

# def __init__(self):

# super().__init__()

# self.name = 'U盘'

# class MoveDisk(MoveStore):

# def __init__(self):

# super().__init__()

# self.name = '移动硬盘'

# class MP3(MoveStore):

# def __init__(self):

# super().__init__()

# self.name = 'MP3'

# def play(self):

# print('{}正在播放音乐'.format(self.name))

# class Connect:

# def connect(self,m):

# if isinstance(m,MoveStore):

# m.read()

# m.write()

# if isinstance(m,MP3):

# m.play()

# else:

# print('连接失败')

# u = USB()

# m1 = MoveDisk()

# m2 = MP3()

# c = Connect()

# c.connect(u)

# c.connect(m1)

# c.connect(m2)

# class A:

# print('A')

# class B:

# print('B')

# class C(A,B):

# pass

# class D(B,A):

# pass

# class E(C,D):

# pass

# C()

# d = D()

# print(D.mro())

# a = input('请输入被整除数:')

# b = input('请输入整除数:')

# a = int(a)

# b = int(b)

# try:

# a = int(a)

# b = int(b)

# c = a / b

# print('商为:%g'%c)

# except:

# print('输入格式有误!')

# import random

# class Player:

# charactor = ['美羊羊','喜羊羊','沸羊羊']

# mora = ['石头','剪刀','布']

# def __init__(self):

# self.name = ''

# self.mora = ''

# self.moradigit = ''

# self.score = 0

#

# def select_charactor(self):

# while True:

# s_charactor = input('请选择角色(1美羊羊、2喜羊羊、3沸羊羊): ')

# if s_charactor.isdigit() and 1 <= int(s_charactor) <= 3:

# self.name = self.__class__.charactor[int(s_charactor) - 1]

# print('玩家选择了{}'.format(self.name))

# break

# else:

# print('操作有误!请重新选择')

# def select_mora(self):

# while True:

# s_mora = input('请猜拳(1石头、2剪刀、3布): ')

# if s_mora.isdigit() and 1 <= int(s_mora) <= 3:

# self.mora = self.__class__.mora[int(s_mora) - 1]

# self.moradigit = int(s_mora)

# break

# else:

# print('操作有误!请重新输入')

# class Computer:

# charactor = ['美羊羊', '喜羊羊', '沸羊羊']

# mora = ['石头', '剪刀', '布']

# def __init__(self):

# self.name = ''

# self.mora = ''

# self.moradigit = ''

# self.score = 0

# def select_charactor(self):

# s_charactor = random.randint(1,3)

# self.name = self.__class__.charactor[s_charactor - 1]

# print('电脑选择了{}'.format(self.name))

# def select_mora(self):

# s_mora = random.randint(1,3)

# self.mora = self.__class__.mora[s_mora - 1]

# self.moradigit = int(s_mora)

#

# class Game:

# def __init__(self):

# self.score = 0 #平局分数

# self.player = ''

# self.computer = ''

# def start(self):

# return input('是否开始游戏(y/n):')

# def set_player(self,player,computer):

# self.player = player

# self.computer = computer

# self.player.select_charactor()

# self.computer.select_charactor()

# def set_punch(self,player,computer):

# self.player = player

# self.computer = computer

# self.player.select_mora()

# self.computer.select_mora()

# def compare(self):

# if self.player.moradigit == self.computer.moradigit:

# self.score += 1

# print('平局')

# elif (self.player.moradigit == 1 and self.computer.moradigit == 2) or (

# self.player.moradigit == 2 and self.computer.moradigit == 3) or (

# self.player.moradigit == 3 and self.computer.moradigit == 1):

# print('玩家出了{},电脑出了{},玩家赢了'.format(self.player.mora,self.computer.mora))

# self.player.score += 1

# else:

# print('玩家出了{},电脑出了{},电脑赢了'.format(self.player.mora,self.computer.mora))

# self.computer.score += 1

# def get_result(self):

#
print('玩家赢了{}局,电脑赢了{},平了{}局'.format(self.player.score,self.computer.score,self.score))

# p1 = Player()

# p2 = Computer()

# p.select_charactor()

# p.select_mora()

# g = Game()

# g.set_player(p1,p2)

# g.set_punch(p1,p2)

# g.compare()

# c = g.start()

# if c.upper() == 'Y':

# p1 = Player()

# p2 = Computer()

# g.set_player(p1,p2)

# while True:

# g.set_punch(p1,p2)

# g.compare()

# s = input('是否继续:(y/n):')

# if s.upper() == 'N':

# print('游戏结束')

# break

# g.get_result()

#

# if c.upper() == 'N':

# print('游戏结束')

总结 :通过今天的学习,对面向对象有了更深刻的了解,使用面向对象写了分页类、计算机类和猜拳游戏程序,感觉有一点难度

技术
下载桌面版
GitHub
Gitee
SourceForge
百度网盘(提取码:draw)
云服务器优惠
华为云优惠券
腾讯云优惠券
阿里云优惠券
Vultr优惠券
站点信息
问题反馈
邮箱:[email protected]
吐槽一下
QQ群:766591547
关注微信