-
定义一个狗类和一个人类:
狗拥有属性:姓名、性别和品种 拥有方法:叫唤
人类拥有属性:姓名、年龄、狗 拥有方法:遛狗
class Dog: def __init__(self, name='二哈', sex='公', variety='哈士奇'): self.name = name self.gender = sex self.var = variety def (self): return cry_out'叫唤' dog1 = Dog('旺财', '公', '银狐') class Person: def __init__(self, name, age): self.name = name self.age = age self.dog = dog1 def owner_and_hisDog(self): return str(self.age) +'岁的' + self.name + '牵着一条正在' + self.dog.cry_out() + '的' + self.dog.name p1 = Person('小红', 16) print(p1.owner_and_hisDog())
-
定义一个矩形类,拥有属性:长、宽 拥有方法:求周长、求面积
class Rectangle:
def __init__(self, lenth=10, width=10):
self.lenth = lenth
self.width = width
def perimeter(self):
return (self.lenth +self.width) * 2
def area(self):
return self.lenth * self.width
p1 = Rectangle(12, 6)
print(p1.perimeter())
- 定义一个二维点类,拥有属性:x坐标、y坐标 拥有方法:求当前点到另外一个点的距离
class Point:
def __init__(self, x1=-4, y1=3):
self.x1 = x1
self.y1 = y1
def distance(self, x2, y2):
self.x2 = x2
self.y2 = y2
return ((self.x1 - self.x2) ** 2 + (self.y1 - self.y2) ** 2) ** 0.5
p1 = Point(3, -4)
print(p1.distance(0, 0))
- 定义一个圆类,拥有属性:半径、圆心 拥有方法:求圆的周长和面积、判断当前圆和另一个圆是否外切
def perimeter(self):
return 2 * Circle.pi * self.r
def area(self):
return Circle.pi * self.r ** 2
def distance(self, other):
return '是外切' if ((self.x - other.x) ** 2 + (self.y - other.y) ** 2) ** 0.5 == self.r + other.r else '不是外切'
c1 = Circle(5, 3, 0)
c2 = Circle(2, -4, 0)
print(c1.perimeter())
print(c1.area())
print(c1.distance(c2))
- 定义一个线段类,拥有属性:起点和终点, 拥有方法:获取线段的长度
class Line:
def __init__(self, x1, y1, x2, y2):
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
def line_lenth(self):
return ((self.x2 - self.x1) ** 2 + (self.y2 - self.y1) ** 2) ** 0.5
l = Line(0, 0, 3, 4)
print(l.line_lenth())
- 写一个斗地主游戏类(根据生活经验添加类的内容)
from random import shuffle # 导入洗牌方法
class Poker: # 扑克牌类
all_color = ['♥', '♠', '♣', '♦']
nums = list(range(2, 11)) +['J', 'Q', 'K', 'A', 'joker', 'JOKER']
# 排大小
table = {str(x): x for x in range(3, 11)}
table.update({'J': 11, 'Q': 12, 'K': 13, 'A':14, '2': 15, 'joker': 16, 'JOKER': 17})
def __init__(self, color, num):
self.color = color
self.num = num
@classmethod
def create_cards(cls):
"""创建一幅新的牌"""
cards = []
for n in cls.nums[:-2]:
for c in cls.all_color:
cards.append(Poker(c, n)) # 添加花色和数字
cards.append(Poker('', cls.nums[-2])) # 小王
cards.append(Poker('', cls.nums[-1])) # 大王
return cards
def __repr__(self):
return f'{self.color}{self.num}'
class LandOwnerGame: # 斗地主游戏类
def __init__(self):
self.cards = Poker.create_cards() # 有一副牌
def deal_cards(self): # 发牌
# 1.洗牌
shuffle(self.cards)
# 2.发牌
player1 = []
player2 = []
player3 = []
cards = iter(self.cards)
for _ in range(17):
player1.append(next(cards))
player2.append(next(cards))
player3.append(next(cards))
# 排序
player1.sort(key=lambda item: Poker.table[item.num], reverse=True)
player2.sort(key=lambda item: Poker.table[item.num], reverse=True)
player3.sort(key=lambda item: Poker.table[item.num], reverse=True)
return player1, player2, player3, list(cards)
game = LandOwnerGame()
p1, p2, p3, di = game.deal_cards()
print(p1)
print(p2)
print(p3)
print(di)