-
定义一个狗类和一个人类:
狗拥有属性:姓名、性别和品种 拥有方法:叫唤
人类拥有属性:姓名、年龄、狗 拥有方法:遛狗
class Dogs: # 狗类的对象属性 def __init__(self): self.name1 = '旺财' self.gender1 = '公' self.kind1 = '哈士奇' def func1(self): print('叫唤') class Humans: # 人类的对象属性 def __init__(self): self.name2 = '张三' self.gender2 = '男' self.dogs = '哈士奇' def func2(self): print('遛狗')
-
定义一个矩形类,拥有属性:长、宽 拥有方法:求周长、求面积
class Rectangular: def __init__(self): self.weight = 1 self.high = 1 def perimeter(self): return self.weight * 2 + self.high * 2 def area(self): return self.weight * self.high * 2
-
定义一个二维点类,拥有属性:x坐标、y坐标 拥有方法:求当前点到另外一个点的距离
-
定义一个圆类,拥有属性:半径、圆心 拥有方法:求圆的周长和面积、判断当前圆和另一个圆是否外切
class Circle: pi = 3.1415926 def __init__(self): self.r = 11 self.center = 1 def raea(self): return Circle.pi * self.r ** 2
-
定义一个线段类,拥有属性:起点和终点, 拥有方法:获取线段的长度
class Line: def __init__(self): self.start = 1 self.end = 11 def len1(self): return self.end - self.start
-
写一个斗地主游戏类(根据生活经验添加类的内容)
from random import shuffle class Poke(): flower = ['♠','♦','♣','♥'] nums = [str(x) for x in range(2,11) ] + ['J','Q','K','A'] kings = {'big':'大王','small':'小王'} pokers = [] player1 = [] player2 = [] player3 = [] dipai = [] # 准备一副牌 def __init__(self): for x in self.flower: for i in self.nums: self.pokers.append(x+i+' ') self.pokers.append(self.kings['big']+'') self.pokers.append(self.kings['small']+'') # 洗牌 def wash(self): shuffle(Poke.pokers) # 发牌 def fa(self): deal_pokers = iter(Poke.pokers) for _ in range(17): Poke.player1.append(next(deal_pokers)) Poke.player2.append(next(deal_pokers)) Poke.player3.append(next(deal_pokers)) # 理牌 def li(self): table = {str(x): x for x in range(3,11)} table.update({'J':11,'Q':12,'K':13,'A':14,'2':15,'oker':16,'OKER':17}) Poke.player1.sort(key = lambda item: table[item[1:]],reverse = True) Poke.player2.sort(key = lambda item: table[item[1:]],reverse = True) Poke.player3.sort(key = lambda item: table[item[1:]],reverse = True) return Poke.player1,Poke.player2,Poke.player3,list(Poke.li.deal_pokers) p = Poke() p.wash() p.fa() p.li()
day17-面向对象作业
最新推荐文章于 2024-03-06 19:44:34 发布