day17-面向对象作业

  1. 定义一个狗类和一个人类:

    狗拥有属性:姓名、性别和品种 拥有方法:叫唤

    人类拥有属性:姓名、年龄、狗 拥有方法:遛狗

    狗类
    class DogClass:
        def __init__(self,name,gender,variety):
            self.name = name
            self.gender = gender
            self.variety = variety
    
        def dog_skill(self):
            print('汪 汪 汪')
    
    d1 = DogClass('修狗','小公狗','柯基')
    print(d1.name)
    print(d1.gender)
    print(d1.variety)
    d1.dog_skill()
    
    
    人类
    class HumanClass:
        def __init__(self, name, age, dog):
            self.name = name
            self.age = age
            self.dog = dog
    
        def human_skill(self):
            return '遛遛修狗'
    
    person_1 = HumanClass('王大炮', '25', '臭臭')
    print(person_1.name)
    print(person_1.age)
    print(person_1.dog)
    print(person_1.human_skill())
    
  2. 定义一个矩形类,拥有属性:长、宽 拥有方法:求周长、求面积

    class Rectangle:
    
        def __init__(self, length, width):
            self.length = length
            self.width = width
    
        def rectangle_skill(self):
            perimeter = (self.length + self.width) * 2
            area = self.length * self.width
            return perimeter, area
    
    r1 = Rectangle(59, 69)
    print(r1.length)
    print(r1.width)
    print(r1.rectangle_skill())
    
  3. 定义一个二维点类,拥有属性:x坐标、y坐标 拥有方法:求当前点到另外一个点的距离

    class Coord:
        def __init__(self, x, y):
            self.x = x
            self.y = y
    
        def distance_calculation(self,i,j):
            distance = ((self.x - i) ** 2 + (self.y - j) ** 2) * 0.5
            return distance
    
    points = Coord(-69,78)
    print(points.x)
    print(points.y)
    print(points.distance_calculation(6, 9))
    
  4. 定义一个圆类,拥有属性:半径、圆心 拥有方法:求圆的周长和面积、判断当前圆和另一个圆是否外切

    class Circle:
        '''
        先输入第一个圆心位置和圆的半径
        计算圆的面积和周长
        最后用圆1的坐标和圆2的计算,并判断位置状态
        '''
        pi = 3.1415926
        def __init__(self, x, y, r1):
            self.x = x
            self.y = y
            self.r1 = r1
    
        def circle(self):
            C = 2 * Circle.pi * self.r1
            S = Circle.pi * (self.r1 ** 2)
            print('周长:{:.2f},面积:{:.2f}'.format(C, S))
    
        def distance_calculation(self, i, j, r2):
            distance = ((self.x - i) ** 2 + (self.y - j) ** 2) * 0.5
            if distance == (self.r1 + r2):
                return '外切'
            else:
                return '非外切'
    
    ci1 = Circle(5,7,3)
    print(ci1.x)
    print(ci1.y)
    print(ci1.r1)
    
    ci2 = Circle(-1, -2, 2)
    print(ci2.x)
    print(ci2.y)
    print(ci2.r1)
    
    # 计算周长和面积
    ci1.circle()
    ci2.circle()
    
    # 判断是否外切
    print(ci2.distance_calculation(-1, -2, 2))
    
  5. 定义一个线段类,拥有属性:起点和终点, 拥有方法:获取线段的长度

    class Lines:
        def __init__(self, x, y):
            self.x = x
            self.y = y
    
        def line_length(self, i, j):
            result = ((i - self.x) ** 2 + (j - self.y) ** 2) * 0.5
            return result
    
  6. 写一个斗地主游戏类(根据生活经验添加类的内容)

    def landlords():
        from random import shuffle, choice
        num_1 = [str(x) for x in range(2, 11)]
        num_2 = ['J', 'Q', 'K', 'A']
        nums = num_1 + num_2
        flowers = ['♥', '♠', '♣', '♦']
        poker = []
        for n in nums:
            for f in flowers:
                poker.append(f+n)
        poker.append('joker')
        poker.append('JOKER')
    
        shuffle(poker)
        poke = iter(poker)
    
        # 先取出地主的3张牌
        land_brand = []
        Q = 0
        for i in poke:
            land_brand.append(i)
            Q += 1
            if Q == 3:
                break
        pokes = list(poke)
        # 创造地主牌
        picks = choice(pokes)
        pokes.remove(picks)
        picks = '地' + picks
        pokes.append(picks)
        po = iter(pokes)
    
        # 创造3位玩家
        player1 = []
        player2 = []
        player3 = []
    
        for x in range(0, 17):
            player1.append(next(po))
            player2.append(next(po))
            player3.append(next(po))
    
        dict1 = {str(x): x for x in range(3, 11)}
        lis1 = {'J': 11, 'Q': 12, 'K': 13, 'A': 14, '2': 15, 'oker': 16, 'OKER': 17}
        dict1.update(lis1)
        player1.sort(key=lambda item: dict1[item[2:]] if item.startswith('地') else dict1[item[1:]])
        player2.sort(key=lambda item: dict1[item[2:]] if item.startswith('地') else dict1[item[1:]])
        player3.sort(key=lambda item: dict1[item[2:]] if item.startswith('地') else dict1[item[1:]])
    
        return player1, player2, player3, land_brand
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值